1.AOP註解配置流程
A.開啓AOP配置支持註解@aspectj
核心配置文件中添加以下配置,功能等同於註解配置Bean的自動掃描路徑 <aop:aspectj-autoproxy/>
B.將所有參與AOP配置的類聲明爲Spring控制的Bean
可以使用XML配置格式或註解格式
C.在切面類的類定義上方添加切面的聲明
@Aspect
public class MyAdvice {…}
D.將切面類中的方法配置爲指定類型的通知,配置時指定其切入點
@Before("execution(* cn.itcast.aop.annotation.UserImpl.add())")
public void before(JoinPoint jp) {
System.out.println("before");
}
2.配置公共的切入點
A.在切面類中聲明一個方法(私有的),將該方法配置爲切入點
@Pointcut("execution(* cn.itcast.aop.annotation.UserImpl.add())") private void pt(){}
B.使用配置的切入點
@Before("引用切入點")
格式:切面類名.方法名()
範例:@Before("MyAdvice. pt ()")
3.註解開發通知的通知類別
前置通知 @Before(value="execution(* *..*.*(..))")
後置通知 @After(value="execution(* *..*.*(..))")
拋出異常通知 @AfterThrowing(value="execution(* *..*.*(..))",throwing="ex") 返回後通知 @AfterReturning(value="execution(* *..*.*(..))",returning="ret") 環繞通知 @Around(value="execution(* *..*.*(..))")
4.註解格式AOP順序
總體順序由上到下爲下列描述順序
around before
before
around after
after
afterReturning
實際開發以最終運行順序爲準
5.返回值與異常對象的獲取方式
@AfterReturning(value="MyAdvice.pt()",returning="aa")
public void afterReturning(JoinPoint jp,Object aa){
System.out.println("afterReturning......."+aa);
}