spring Aop中aop:advisor 與 aop:aspect的區別 【愛玩酷java php 學習路徑】

來源:互聯網
上載者:User
在spring的配置中,會用到這兩個標籤.那麼他們的區別是什麼呢?
    <bean id="testAdvice" class="com.myspring.app.aop.MyAdvice"/> //切面代碼     使用<aop:aspect>配置時,     如果切面代碼是自動注入的bean,那麼<aop:aspect>的ref屬性直接寫bean的注入名字就可以了!     <aop:config>         <aop:aspect ref="testAdvice" id="testAspect">             <aop:pointcut expression="(execution(* com.myspring.app.aop.TestPoint.*(..)))" id="testPointcut"/>             <aop:before  method="doBefore" pointcut-ref="testPointcut"/>         </aop:aspect>     </aop:config>
    <aop:config>         <aop:pointcut expression="(execution(* com.myspring.app.aop.TestPoint.*(..)))"  id="mypoint"/>         <aop:advisor advice-ref="testAdvice" pointcut-ref="mypoint"/>     </aop:config> 注意:2種格式的書寫次序. ========================================================================= package com.myspring.app.aop;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint; import org.springframework.aop.MethodBeforeAdvice;
/**  * 方法前置通知  * @author Michael  *  */ @Component("myAdvice")//如果是自動裝配,在定義切面的時候直接寫在ref屬性裡就可以了 public class MyAdvice implements MethodBeforeAdvice{     //如果使用aop:advisor配置,那麼切面邏輯必須要實現advice介面才行!否則會失敗!     @Override     public void before(Method method, Object[] args, Object target) throws Throwable {         System.out.println("前置通知");     }
    //如果是<aop:aspect>配置,編寫一般的方法就可以了,然後在切面配置中指定具體的方法名稱!     public void doBefore(JoinPoint point) throws Throwable {
    } }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.