@AfterThrowing:This advice gets executed only when join point method throws exception, we can use it to rollback the transaction declaratively. We use @AfterThrowing annotation for this type of advice.
Invocation of the advices of different type applied on the same jointpoint(core business related modules) as follows :
1.Around
2.Before and/or After
3.AfterReturning or AfterThrowing
Suppose we applied all five types of advices on the same jointpoint then the flow will be like :
Around type advice will be invoked and code before pjp.proceed() of Around type advice will be executed where pjp is the reference variable of ProceedingJoinPointinterface.
Before type advice will be invoked and executed fully.
code inside jointpoint will be executed fully.
Code after pjp.proceed() of Around type advice will be executed if jointpoint executes successfully otherwise skip this step and go to step 5. If it's modified return value then this new return value will be effected to the followings advice or method invocation.
After type advice will be invoked and executed fully.
AfterReturning type advice will be invoked and executed fully if jointpoint executes successfully else if jointpoint throws any error then AfterThrowing type advice will be invoked and executed fully.
@Override
public int invalidOp(int a, int b) {
throw new IllegalArgumentException("ArithmeticCalculatorServiceImpl.invalidOp");
}
返回值为null,和int不匹配抛异常
Exception in thread "main" org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract int club.cser.springroad.aop.service.ArithmeticCalculatorService.invalidOp(int,int)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:226)
at com.sun.proxy.$Proxy37.invalidOp(Unknown Source)
at club.cser.springroad.aop.client.AspectJClient.main(AspectJClient.java:18)
@Pointcut("@annotation(com.xys.demo1.AuthChecker)")
public void pointcut() {
}
// 匹配指定包中的所有的方法
execution(* com.xys.service.*(..))
// 匹配当前包中的指定类的所有方法
execution(* UserService.*(..))
// 匹配指定包中的所有 public 方法
execution(public * com.xys.service.*(..))
// 匹配指定包中的所有 public 方法, 并且返回值是 int 类型的方法
execution(public int com.xys.service.*(..))
// 匹配指定包中的所有 public 方法, 并且第一个参数是 String, 返回值是 int 类型的方法
execution(public int com.xys.service.*(String name, ..))