Spring整合aspectj框架實現的aop
aspectj框架的配置我就不說了,大家可以自己百度一下,挺簡單的
給大家講一下怎麼樣使用
用aspectj框架來實現
該框架的特點:
1,可以不用寫生成的動態代理的對象.
2,可以寫多個切點和多個通知
3,aspectj有六種通知類型
1. 前置通知Before 相當於BeforeAdvice
2. 後置通知AfterReturning 相當於AfterReturningAdvice
3. 環繞通知 Around 相當於MethodInterceptor
4. 拋出通知AfterThrowing 相當於ThrowAdvice
5. 引介通知DeclareParents 相當於IntroductionInterceptor
6. 最終通知After 不管是否異常,該通知都會執行
並且aspectj框架,可以用通知可以不用實現接口
Aspect框架的xml編寫
只需要三步
1,定義一個通知對象
2,定義一個目標對象
3,用aop標籤(切面),在中間定義切點和添加通知對象,然後就可以了
Aspectj框架會自己創建動態代理
<!-- 目標對象 -->
<bean id="userService" class="cmo.demo.aspectj.UserService"></bean>
<!-- 通知對象 -->
<bean id="userServiceAdvice" class="cmo.demo.aspectj.UserHapler"></bean>
<!-- 聲明切面和切點 -->
<!-- 使用aop:config來聲明 使用aop:aspect來配置切面, 設置true不管使用什麼都用cglib,false不使用接口的時候會用jdkproxy來代理 -->
<aop:config proxy-target-class="true">
<aop:aspect ref="userServiceAdvice">
<aop:pointcut expression="execution(**Test(..))" id="TestPointCut"/>
<aop:before method="before"pointcut-ref="TestPointCut"/>
</aop:aspect>
</aop:config>
由標籤決定 aop:before前置通知 aop:around環繞通知 aop:after後置通知等 ..... 記住一定要配置的屬性:method要和通知方法名字相同, pointcut-ref:設置的切點
關於切點表達式寫法
1. execution(public * *()) 所有的public的方法
2. execution(*cn.itheima.aop.*(..)) 所有的aop包下的所有類的方法(不包含子包)
3. execution(*cn.itheima.aop..*(..)) 所有的aop包及其子包下的所有類的方法
4. execution(*cn.itheima.aop.IOrderService.*(..)) IOrderService接口中定義的所有方法
5. execution(*cn.itheima.aop.IOrderService+.*(..)) 匹配實現特定接口所有類的方法
6. execution(*save*(..)) 區配所有的以save開頭的方法