在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 {
}
}