Spring aop

1 jar:

aopalliance-1.0.0.jar
aspectjweaver-1.6.8.jar
commons-logging-1.2.jar
spring-aop-4.3.3.RELEASE.jar
spring-aspects-4.3.3.RELEASE.jar
spring-beans-4.3.3.RELEASE.jar
spring-context-4.3.3.RELEASE.jar
spring-core-4.3.3.RELEASE.jar
spring-expression-4.3.3.RELEASE.jar

2 在配置文件中加入aop命名空间 基于注解的方式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        <!-- configure the InternalResourceViewResolver -->
    
    <context:component-scan base-package="com.sgcc"></context:component-scan>
      <!-- 使AspectJ注解起作用:自动为匹配的类生成代理对象 -->
      <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

 

package com.sgcc.aop.impl;

public interface ArithmeticCalculator {
    public int add(int i,int j);
    
    public int sub(int i,int j);
    
    public int mul(int i,int j);
    
    public int div(int i,int j);
}
package com.sgcc.aop.impl;

import org.springframework.stereotype.Component;

@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator{

    @Override
    public int add(int i, int j) {
        // TODO Auto-generated method stub
        int result = i + j;
        return result;
    }

    @Override
    public int sub(int i, int j) {
        // TODO Auto-generated method stub
        int result = i - j;
        return result;
    }

    @Override
    public int mul(int i, int j) {
        // TODO Auto-generated method stub
        int result = i * j;
        return result;
    }

    @Override
    public int div(int i, int j) {
        // TODO Auto-generated method stub
        int result = i / j;
        return result;
    }

}
package com.sgcc.aop.impl;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

//把这个类声明为一个切面,把该类放入到IOC容器中,再声明一个切面
@Component
@Aspect
public class LoggingAspect {
    //声明该方法是一个前置通知:在目标方法开始之前执行
    @Before("execution( * com.sgcc.aop.impl.*.*(int , int ))")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("the method"+methodName+" begins"+args);
    }
    
}
package com.sgcc.aop.impl;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("appliicationContext.xml");
        
        ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);
        
        int result = arithmeticCalculator.add(3, 6);
        System.out.println("result:"+result);
        result = arithmeticCalculator.div(5, 5);
        System.out.println("result:"+result);
    }

}

 

posted @ 2016-11-23 10:38  alloevil  阅读(217)  评论(0编辑  收藏  举报