Pointcut例子
@Pointcut("execution(* com.imooc.aop.aspectj.biz.*Biz.*(..))")
public void pointcut() {}
@Pointcut("within(com.imooc.aop.aspectj.biz.*)")
public void bizPointcut() {}
共享调用Pointcut方法:
第一种
@Before("execution(* com.imooc.aop.aspectj.biz.*Biz.*(..))")
public void before() {
System.out.println("Before.");
}
第二种
@Before("pointcut()")
public void before() {
System.out.println("Before.");
}
第一种和第二种是等价的
Advice的@AfterReturning注解方法
带返回值
@AfterReturning(pointcut="bizPointcut()", returning="returnValue")
public void afterReturning(Object returnValue) {
System.out.println("AfterReturning : " + returnValue);
}
Advice的@AfterThrowing注解方法
带抛出异常值
@AfterThrowing(pointcut="pointcut()", throwing="e")
public void afterThrowing(RuntimeException e) {
System.out.println("AfterThrowing : " + e.getMessage());
}
After(finally)advice
最终通知必须准备处理正常和异常两种返回情况,它通常用于释放资源
@After("pointcut()")
public void after() {
System.out.println("After.");
}
Around advice
@Around("pointcut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("Around 1.");
Object obj = pjp.proceed();
System.out.println("Around 2.");
System.out.println("Around : " + obj);
return obj;
}
Before After AfterReturning AfterThrowing顺序问题
try{
try{
//@Before
method.invoke(..);
}finally{
//@After
}
//@AfterReturning
}catch(){
//@AfterThrowing
}