Spring注解驱动开发(三)--AOP使用

一、前言

  本篇主要讲解Spring AOP的使用

二、开启AOP

  Spring默认是不开启AOP功能,需要通过注解@EnableAspectJAutoProxy。该注解的功能是往Spring容器中注入AOP所需的组件,实现原理查看

/**
 * 开启Spring AOP
 */
@EnableAspectJAutoProxy
public class AopConfig {

    @Bean
    public Calculator calculator() {
        return new Calculator();
    }

    @Bean
    public LogAop logAop() {
        return new LogAop();
    }
}

三、定义目标对象

  目标对象的定义和其它对象没有任何区别,所以Spring AOP对业务代码是无任何侵入性

public class Calculator {

    public int div(int num1, int num2) {
        return num1 / num2;
    }
}

四、定义切面

  • @Aspect 告诉Spring容器这是一个切面
  • @Pointcut 定义需要拦截的目标方法
  • @Before 目标方法执行前调用
  • @After 目标方法执行后调用
  • @AfterReturning 目标方法执行返回后调用
  • @AfterThrowing 目标方法抛出异常后调用
  • @Around 用于@Before和@After,可以手动控制方法调用
@Aspect
public class LogAop {

    @Pointcut("execution(* indi.zqc.spring.bean.Calculator.*(..))")
    public void pointcut() {
    }

    @Before(value = "pointcut()")
    public void before(JoinPoint joinPoint) {
        System.out.println("LogAop --- before --- " + joinPoint.getSignature().getDeclaringTypeName());
    }

    @After(value = "pointcut()")
    public void after(JoinPoint joinPoint) {
        System.out.println("LogAop --- after --- " + joinPoint.getSignature().getDeclaringTypeName());
    }

    @AfterReturning(value = "pointcut()", returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result) {
        System.out.println("LogAop --- afterReturning --- " + result);
    }

    @AfterThrowing(value = "pointcut()", throwing = "exception")
    public void afterThrowing(JoinPoint joinPoint, Exception exception) {
        System.out.println("LogAop --- afterThrowing --- " + exception);
    }

    @Around(value = "pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("LogAop --- around --- before");
        // 调用目标方法
        Object proceed = joinPoint.proceed();
        System.out.println("LogAop --- around --- after");
        return proceed;
    }
}

五、将目标对象和切面注入到Spring容器中

  必须把切面和目标对象都注入到Spring容器中,才能生效

六、调用

public class AopConfigTest {

    @Test
    public void test() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AopConfig.class);
        Calculator calculator = applicationContext.getBean(Calculator.class);
        calculator.div(1, 1);
    }

}

猜你喜欢

转载自www.cnblogs.com/zhuqianchang/p/11422551.html