以配置的方式去註冊aop

<bean id="securityHandler" class="com.zqblogs.spring.handle.SecurityHandler"></bean><!-- 切面對象 -->
    <bean id="userManager" class="com.zqblogs.spring.UserManagerImpl"/><!-- 目標對象 -->
    <aop:aspectj-autoproxy/>
    <aop:config><!-- 用配置方式去註冊aop -->
       <aop:aspect id="security" ref="securityHandler"><!-- 指明是切面對象 ,找到advice-->
           <!-- 切面的 pointcut,expression指明瞭advice的作用範圍->
           <aop:pointcut id="allAddMethod" expression="execution(* com.zqblogs.spring.bean.add*(..))"></aop:pointcut>
           <!--定義切面對象的advice,並指明該advice的pointcut-->
           <aop:before method="checkSecurity" pointcut-ref="allAddMethod"></aop:before>           
       </aop:aspect>

    </aop:config>

     param-pattern:指定方法參數(聲明的類型),(..)代表所有參數,(*)代表一個參數,(*,String)代表第一個參數爲任何值,第二個爲String類型.

    表達式例子如下:

  任意公共方法的執行:
    execution(public * *(..))
  任何一個以“set”開始的方法的執行:
    execution(* set*(..))
  AccountService 接口的任意方法的執行:
    execution(* com.xyz.service.AccountService.*(..))
  定義在service包裏的任意方法的執行:
    execution(* com.xyz.service.*.*(..))
  定義在service包和所有子包裏的任意類的任意方法的執行:
    execution(* com.xyz.service..*.*(..))
  定義在pointcutexp包和所有子包裏的JoinPointObjP2類的任意方法的執行:
    execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")

     另外cglib動態代理配置和jdk動態代理一樣,spring會根據目標類是否實現了接口來決定使用哪一種方式。

發佈了108 篇原創文章 · 獲贊 8 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章