基於XML的聲明式AspectJ

基於xml的AspectJ:一個AOP框架:通過xml文件配置:通過aop:config元素定義切面點,通知等。
常用元素與子元素嵌套關係:

aop:config的子元素:aop:aspect
aop:aspect的子元素:(id:唯一標識,ref:引用普通的spring bean)
1.aop:pointcut(配置全局切入點):(id:唯一標識,expression:寫切入點表達式)
2.aop:before(配置前置通知)(method:想要增強的方法,pointcut-ref:把配置的切入點的id寫進來)
3.aop:after-returning(配置後置通知)(method:同上,pointcut-ref:同上,returning:通過該形參訪問目標方法返回值)
4.aop:around(配置環繞通知)(method:同上,pointcut-ref:同上)
5.aop:after-throwing(配置異常通知)(method:同上,pointcut-ref:同上,throwing:通過該形參訪問方法返回值)
6.aop:after(配置最終通知)(method:同上,pointcut-ref:同上)


結構:
aop:config
aop:aspect(配置切面)

<aop:pointcut />(配置全局切入點)
<aop:befor />
<aop:after-returning />
<aop:around />
<aop:after-throwing />
<aop:after />

</aop:aspect>
</aop:config>


例如:
在這裏插入圖片描述


切入點:通俗易懂的說,你想在哪個方法的前或者後增加通知/增強處理。那個位置(那個方法)就是切入點,你在那個切入點前添加了通知,就是前置通知。你在那個切入點後添加通知,就是後置通知。
切入點表達式:execution表達式:
訪問修飾符 返回值類型 類路徑 目標方法 參數 異常類型(加粗爲必須值)
例如:public void com.meicong.jdk.UserDaoImpl.adduser()(…)
訪問修飾符的位置填寫一個 *,代表隨意,任意都可以。


//定義切面類
public class MyAspect { 
//前置通知:
public void myBefore(JoinPoint joinPoint) {       //用JoinPoint接口作爲參數獲取目標對象的信息,方法,類名等。(記住這麼寫,獲取信息用) 
System.out.print(" 前置通知:模擬執行權限檢查. . . , " ) ;   
System.out.print(" 目標類是: "+joinPoint.getTarget() );    //joinPoint.getTarget:目標類,也就是要增加通知的那個目標類。Target:目標。
System.out.println(" ,被織入增強處理的目標方法爲: " +joinPoint.getSignature().getName()) ;  //獲取目標方法。 
}
//後置通知:
public void myAfterReturning(JoinPoint joinPoint) { 
System.out.print(" 後置通知:模擬記錄日誌. . . ," ); 
System.out.println(" 被植入增強處理的目標方法爲: " + joinPoint.getSignature() .getName()); 
}
//環繞通知:前和後都有,返回值爲Object,參數這裏有改變,這是JoinPoint的子接口,必須有異常處理。
public Object myAround(ProceedingJoinPoint proceedingJoinPoint) 
throws Throwable { 
System.out.println("環繞開始:執行目標方法之前,模擬開啓事務. . . ") ; 
Object obj = proceedingJoinPoint.proceed() ;     //執行當前目標方法,也就是你準備要添加通知的那個方法,他的前後都加了通知。
System out println("環繞結束 執行門標方法之後 模擬關閉事務 . . ") ; 
return obj;     //返回obj
}
//異常通知:出現異常時的通知
public void myAfterThrowing(JoinPoint joinPoint, Throwable e ) { 
System out println("異常通知:"+"出錯了" + e.getMessage()); 
}
//最終通知:最後一定會執行的通知
public void myAfter() { 
System out println("最終通知 模擬方法結束後的釋放資源 . . ") ; 
}
}
//xml配置文件主要代碼:

<bean id="userDao" class=" com.itheima.jdk.UserDaolmpl" />    //配置目標類
<bean id="myAspect" class="com.itheima.aspectj.xml.MyAspect" />   //配置切面類
<aop:config>
<aop:aspect ref="myAspect " >     //配置切面,ref指向的就是切面類的id 
<aop:pointcut expression="execution(* com.itheima.jdk.*.*(..))"   //配置切入點,就是要增強什麼方法,用切入點表達式 
id="myPointCut" /> 
<aop:before method="myBefore" pointcut-ref="myPointCut" />      //前置,對用的那個前置方法,把這個前置方法加入到目標對象上。   
<aop:after-returning method="myAfterReturning"             //指向切入點。
pointcut-ref="myPointCut" returning="returnVal" />         //同理   後置通知可返回方法返回值,returning。
<aop:around method="myAround" pointcut-ref="myPointCut" /> 
<aop:after-throwing method="myAfterThrowing" 
pointcut-ref="PointCut" throwing="e" />    //異常處理
<aop:after method="myAfter" pointcut-ref="myPointCut" />
</aop:aspect>
</aop:config> 
</beans>

public class TestXmlAspectj {
public static void main(String args[)) {
String xmlPath =
"com/itheima/aspectj/xml/applicationContext . xml"; 
ApplicationContext applicationContext = 
new ClassPath nlApplicationContext(xmlPath);
UserDao userDao = (UserDao) applicationContext.getBean("userDao"); 
userDao.addUser();
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章