Spring源码窥探之:AOP注解

AOP也就是咱们平常说的@面向切面编程,看概念比较晦涩难懂,难懂的是设计理念,以及这样设计的好处是什么。在Spring的AOP中,经常使用的几个注解以下:@Aspect,@Before,@After,@AfterReturning,@AfterThrowing,@Around,@PointCut以及@EnableAspectJAutoProxyspring

1. 模拟业务类CalculateService编程

/**
 * description:模拟业务类
 *
 * @author 70KG
 * @date 2018/12/17
 */
public class CalculateService {

    public int calculate(int i, int j) {
        int result = i / j;
        System.out.println("---->CalculateService-业务方法执行。。。。。。");
        return result;
    }

}

2. 编写切面类,切面类必须标注它为切面类(@Aspect),关于切入点表达式,能够参考官方文档,写法不少种,还有关于JoinPoint这个参数必须放在第一位,不然报错。测试

/**
 * description:日志切面类,JoinPoint必须在参数的第一位
 *
 * @author 70KG
 * @date 2018/12/17
 */
@Aspect // ---> 声明此类是一个切面类
public class LogAdvice {

    @Pointcut("execution(* com.nmys.story.springCore.springaop.sample01.CalculateService.*(..))")
    public void pointCut() {
    }

    /**
     * 目标方法执行前执行
     */
    @Before("pointCut()")
    public void logBefore(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        System.out.println("---->" + signature + "方法执行前,传入的参数是:" + Arrays.asList(args));
    }

    /**
     * 目标方法执行后执行
     */
    @After("pointCut()")
    public void logAfter(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        System.out.println("---->" + signature + "方法执行后,传入的参数是:" + Arrays.asList(args));

    }

    /**
     * 方法发生异常时执行
     */
    @AfterThrowing(value = "pointCut()", throwing = "ex")
    public void logException(JoinPoint joinPoint, Exception ex) {
        Signature signature = joinPoint.getSignature();
        System.out.println("---->" + signature + "方法执行后,抛出了异常信息:" + ex.getMessage());
    }


    /**
     * 正常返回时执行
     */
    @AfterReturning(value = "pointCut()", returning = "result")
    public void logReturn(JoinPoint joinPoint, Object result) {
        Signature signature = joinPoint.getSignature();
        System.out.println("---->" + signature + "方法执行后,返回的结果是:" + result);
    }

}

3. 配置类spa

/**
 * description
 *
 * @author 70KG
 * @date 2018/12/17
 */
@Configuration
@EnableAspectJAutoProxy // ---> 开启切面的自动代理
public class MyConfig {

    @Bean
    public CalculateService calculateService() {
        return new CalculateService();
    }

    // 将切面类也交给容器管理
    @Bean
    public LogAdvice logAdvice() {
        return new LogAdvice();
    }

}

4. 测试类设计

/**
 * description
 *
 * @author 70KG
 * @date 2018/12/17
 */
public class Test01 {

    public static void main(String[] args) {

        // 加载配置文件
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MyConfig.class);
        CalculateService calc = (CalculateService) ac.getBean("calculateService");
        calc.calculate(4,2);
    }

}

5. 结果代理

---->int com.nmys.story.springCore.springaop.sample01.CalculateService.calculate(int,int)方法执行前,传入的参数是:[4, 2]
---->CalculateService-业务方法执行。。。。。。
---->int com.nmys.story.springCore.springaop.sample01.CalculateService.calculate(int,int)方法执行后,传入的参数是:[4, 2]
---->int com.nmys.story.springCore.springaop.sample01.CalculateService.calculate(int,int)方法执行后,返回的结果是:2
相关文章
相关标签/搜索