11-springAop 使用

Aop

一、切面方法

# 前置通知(@Before):目标方法之前之前执行
# 后置通知(@After):目标方法执行之后一定会执行,不管是否发生异常
# 返回通知(@AfterReturning):目标方法正常返回后执行
# 异常通知(@AfterThrowing):目标方法抛出异常时执行
# 环绕通知(@Around):动态代理,需要手动调用目标方法

二、示例

  • 通过Aop的各个通知方法拦截目标方法。

2.1 目标方法

public class Calculator {
	/**
	 * 业务逻辑方法
	 * */
	public int div(int i, int j)  {
		System.out.println("div execute...");
		return i/j;
	}
}

2.2 切面类

@Aspect
public class LogAspects {
    @Pointcut("execution(public int com.intellif.aop.Calculator.*(..))")
    public void myPointCut(){};

    @Before("myPointCut()")
    public void logStart(JoinPoint joinPoint) {
        System.out.println("@Before execute , Args:{" + Arrays.asList(joinPoint.getArgs()) + "}");
    }

    @After("myPointCut()")
    public void logEnd(JoinPoint joinPoint) {
        System.out.println("@After execute finished ......");
    }

    @AfterReturning(value = "myPointCut()", returning = "result")
    public void logReturn(Object result) {
        System.out.println("@AfterReturning execute... Return successfully:{" + result + "}");
    }

    @AfterThrowing(value = "myPointCut()", throwing = "exception")
    public void logException(Exception exception) {
        System.out.println(" @AfterThrowing execute .... Exception:{" + exception + "}");
    }

    @Around("myPointCut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("@Around:Before...");
        //相当于开始调目标方法
        Object obj = proceedingJoinPoint.proceed();
        System.out.println("@Around:After...");
        return obj;
    }
}

2.3 配置类

@Configuration
@EnableAspectJAutoProxy
public class AopConfig {
	@Bean
	public Calculator calculator(){
		return new Calculator();
	}

	@Bean
	public LogAspects logAspects(){
		return new LogAspects();
	}
}

2.4 测试

public class AopTest {

    @Test
    public void test01() {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(AopConfig.class);
        Calculator c = app.getBean(Calculator.class);
        int result = c.div(4, 2);
        System.out.println(result);
        app.close();
    }
}

2.5 输出

@Around:Before...
@Before execute , Args:{[4, 2]}
div execute...
@Around:After...
@After execute finished ......
@AfterReturning execute... Return successfully:{2}
2
  • 异常输出
@Around:Before...
@Before execute , Args:{[4, 0]}
div execute...
@After execute finished ......
 @AfterThrowing execute .... Exception:{java.lang.ArithmeticException: / by zero}

java.lang.ArithmeticException: / by zero

2.6 小结

  • 我们看到,正常情况下,Aop的切面方法执行顺序是:
@Around开始 ->@Before-> 目标方法 -> @Around完毕 -> @After ->@AfterReturning 
  • 异常情况下,Aop的切面方法执行顺序是:
@Around开始 ->@Before-> 目标方法 ->  @After ->@AfterThrowing 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值