詳解Spring Aop實(shí)例之xml配置
AOP的配置方式有2種方式:xml配置和AspectJ注解方式。今天我們就來(lái)實(shí)踐一下xml配置方式。
我采用的jdk代理,所以首先將接口和實(shí)現(xiàn)類代碼附上
package com.tgb.aop; public interface UserManager { public String findUserById(int userId); } package com.tgb.aop; public class UserManagerImpl implements UserManager { public String findUserById(int userId) { System.out.println("---------UserManagerImpl.findUserById()--------"); if (userId <= 0) { throw new IllegalArgumentException("該用戶不存在!"); } return "張三"; } }
單獨(dú)寫一個(gè)Advice通知類進(jìn)行測(cè)試。這個(gè)通知類可以換成安全性檢測(cè)、日志管理等等。
package com.tgb.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; /** * Advice通知類 * 測(cè)試after,before,around,throwing,returning Advice. * @author Admin * */ public class XMLAdvice { /** * 在核心業(yè)務(wù)執(zhí)行前執(zhí)行,不能阻止核心業(yè)務(wù)的調(diào)用。 * @param joinPoint */ private void doBefore(JoinPoint joinPoint) { System.out.println("-----doBefore().invoke-----"); System.out.println(" 此處意在執(zhí)行核心業(yè)務(wù)邏輯前,做一些安全性的判斷等等"); System.out.println(" 可通過(guò)joinPoint來(lái)獲取所需要的內(nèi)容"); System.out.println("-----End of doBefore()------"); } /** * 手動(dòng)控制調(diào)用核心業(yè)務(wù)邏輯,以及調(diào)用前和調(diào)用后的處理, * * 注意:當(dāng)核心業(yè)務(wù)拋異常后,立即退出,轉(zhuǎn)向After Advice * 執(zhí)行完畢After Advice,再轉(zhuǎn)到Throwing Advice * @param pjp * @return * @throws Throwable */ private Object doAround(ProceedingJoinPoint pjp) throws Throwable { System.out.println("-----doAround().invoke-----"); System.out.println(" 此處可以做類似于Before Advice的事情"); //調(diào)用核心邏輯 Object retVal = pjp.proceed(); System.out.println(" 此處可以做類似于After Advice的事情"); System.out.println("-----End of doAround()------"); return retVal; } /** * 核心業(yè)務(wù)邏輯退出后(包括正常執(zhí)行結(jié)束和異常退出),執(zhí)行此Advice * @param joinPoint */ private void doAfter(JoinPoint joinPoint) { System.out.println("-----doAfter().invoke-----"); System.out.println(" 此處意在執(zhí)行核心業(yè)務(wù)邏輯之后,做一些日志記錄操作等等"); System.out.println(" 可通過(guò)joinPoint來(lái)獲取所需要的內(nèi)容"); System.out.println("-----End of doAfter()------"); } /** * 核心業(yè)務(wù)邏輯調(diào)用正常退出后,不管是否有返回值,正常退出后,均執(zhí)行此Advice * @param joinPoint */ private void doReturn(JoinPoint joinPoint) { System.out.println("-----doReturn().invoke-----"); System.out.println(" 此處可以對(duì)返回值做進(jìn)一步處理"); System.out.println(" 可通過(guò)joinPoint來(lái)獲取所需要的內(nèi)容"); System.out.println("-----End of doReturn()------"); } /** * 核心業(yè)務(wù)邏輯調(diào)用異常退出后,執(zhí)行此Advice,處理錯(cuò)誤信息 * @param joinPoint * @param ex */ private void doThrowing(JoinPoint joinPoint,Throwable ex) { System.out.println("-----doThrowing().invoke-----"); System.out.println(" 錯(cuò)誤信息:"+ex.getMessage()); System.out.println(" 此處意在執(zhí)行核心業(yè)務(wù)邏輯出錯(cuò)時(shí),捕獲異常,并可做一些日志記錄操作等等"); System.out.println(" 可通過(guò)joinPoint來(lái)獲取所需要的內(nèi)容"); System.out.println("-----End of doThrowing()------"); } }
只有Advice還不行,還需要在application-config.xml中進(jìn)行配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="userManager" class="com.tgb.aop.UserManagerImpl"/> <!--<bean id="aspcejHandler" class="com.tgb.aop.AspceJAdvice"/>--> <bean id="xmlHandler" class="com.tgb.aop.XMLAdvice" /> <aop:config> <aop:aspect id="aspect" ref="xmlHandler"> <aop:pointcut id="pointUserMgr" expression="execution(* com.tgb.aop.*.find*(..))"/> <aop:before method="doBefore" pointcut-ref="pointUserMgr"/> <aop:after method="doAfter" pointcut-ref="pointUserMgr"/> <aop:around method="doAround" pointcut-ref="pointUserMgr"/> <aop:after-returning method="doReturn" pointcut-ref="pointUserMgr"/> <aop:after-throwing method="doThrowing" throwing="ex" pointcut-ref="pointUserMgr"/> </aop:aspect> </aop:config> </beans>
編一個(gè)客戶端類進(jìn)行測(cè)試一下:
package com.tgb.aop; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) { BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); UserManager userManager = (UserManager)factory.getBean("userManager"); //可以查找張三 userManager.findUserById(1); System.out.println("=====我==是==分==割==線====="); try { // 查不到數(shù)據(jù),會(huì)拋異常,異常會(huì)被AfterThrowingAdvice捕獲 userManager.findUserById(0); } catch (IllegalArgumentException e) { } } }
結(jié)果如圖:
值得注意的是Around與Before和After的執(zhí)行順序。3者的執(zhí)行順序取決于在xml中的配置順序。圖中標(biāo)記了3塊,分別對(duì)應(yīng)Before,Around,After。其中②中包含有③。這是因?yàn)閍op:after配置到了aop:around的前面,如果2者調(diào)換一下位置,這三塊就會(huì)分開(kāi)獨(dú)立顯示。如果配置順序是aop:after -> aop:around ->aop:before,那么①和③都會(huì)包含在②中。這種情況的產(chǎn)生是由于Around的特殊性,它可以做類似于Before和After的操作。當(dāng)安全性的判斷不通過(guò)時(shí),可以阻止核心業(yè)務(wù)邏輯的調(diào)用,這是Before做不到的。
使用xml可以對(duì)aop進(jìn)行集中配置。很方便而簡(jiǎn)單。可以對(duì)所有的aop進(jìn)行配置,當(dāng)然也可以分開(kāi)到單獨(dú)的xml中進(jìn)行配置。當(dāng)需求變動(dòng)時(shí),不用修改代碼,只要重新配置aop,就可以完成修改操作。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)excel導(dǎo)入數(shù)據(jù)的工具類
這篇文章主要介紹了java實(shí)現(xiàn)的excel導(dǎo)入數(shù)據(jù)的工具類,需要的朋友可以參考下2014-03-03Java編程實(shí)現(xiàn)數(shù)組轉(zhuǎn)成list及l(fā)ist轉(zhuǎn)數(shù)組的方法
這篇文章主要介紹了Java編程實(shí)現(xiàn)數(shù)組轉(zhuǎn)成list及l(fā)ist轉(zhuǎn)數(shù)組的方法,結(jié)合實(shí)例形式較為詳細(xì)的總結(jié)分析了java實(shí)現(xiàn)數(shù)組與list之間相互轉(zhuǎn)換的操作技巧,需要的朋友可以參考下2017-09-09Springboot集成ClickHouse及應(yīng)用場(chǎng)景分析
這篇文章主要介紹了Springboot集成ClickHouse的實(shí)例代碼,本文通過(guò)應(yīng)用場(chǎng)景實(shí)例代碼介紹了整合springboot的詳細(xì)過(guò)程,感興趣的朋友跟隨小編一起看看吧2022-02-02HttpServletRequest對(duì)象簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了HttpServletRequest對(duì)象簡(jiǎn)介的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07詳解Java中IO字節(jié)流基本操作(復(fù)制文件)并測(cè)試性能
這篇文章主要介紹了Java中IO字節(jié)流基本操作(復(fù)制文件)并測(cè)試性能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04elasticsearch通過(guò)guice注入Node組裝啟動(dòng)過(guò)程
這篇文章主要為大家介紹了?elasticsearch通過(guò)guice注入Node組裝啟動(dòng)過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04