国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Spring AOP技術(shù)(基于AspectJ)的XML開發(fā)

2019-11-11 02:56:46
字體:
供稿:網(wǎng)友

SPRing AOP技術(shù)(基于aspectJ)的xml開發(fā)

@(Spring)[aop, spring, xml, Spring, annotation, aspectJ]

Spring AOP技術(shù)基于AspectJ的XML開發(fā)Spring AOP的XML的開發(fā)AOP的概述什么是AOPSpring中的AOPSpring的AOP的底層實(shí)現(xiàn)Spring的底層實(shí)現(xiàn)之JDK動態(tài)代理Spring的底層實(shí)現(xiàn)之CGLIB動態(tài)代理Spring的AOP的術(shù)語Spring的AOP開發(fā)分成兩類AOP開發(fā)中的術(shù)語Spring的基于AspectJ的AOP的XML開發(fā)第一步引入jar包第二步創(chuàng)建配置文件第三步創(chuàng)建需增強(qiáng)包和類第四步將類交給Spring管理第五步編寫測試第六步編寫切面類和通知第七步 配置切面類第八步通過配置實(shí)現(xiàn)AOP第九步執(zhí)行測試切入點(diǎn)表達(dá)式的定義切入點(diǎn)表達(dá)式定義語法通知類型前置通知創(chuàng)建需增強(qiáng)類和方法創(chuàng)建切面和通知配置切面和通知單元測試后置通知創(chuàng)建需增強(qiáng)類和方法創(chuàng)建切面和通知配置切面和通知單元測試環(huán)繞通知創(chuàng)建需增強(qiáng)類和方法創(chuàng)建切面和通知配置切面和通知單元測試異常拋出通知創(chuàng)建需增強(qiáng)類和方法創(chuàng)建切面和通知配置切面和通知單元測試最終通知創(chuàng)建需增強(qiáng)類和方法創(chuàng)建切面和通知配置切面和通知單元測試Spring AOP小項(xiàng)目

Spring AOP的XML的開發(fā)

AOP的概述

什么是AOP

在軟件業(yè),AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預(yù)編譯方式和運(yùn)行期動態(tài)代理實(shí)現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù)。AOP是OOP的延續(xù),是軟件開發(fā)中的一個熱點(diǎn),也是Spring框架中的一個重要內(nèi)容,是函數(shù)式編程的一種衍生范型。利用AOP可以對業(yè)務(wù)邏輯的各個部分進(jìn)行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發(fā)的效率。

將日志記錄,性能統(tǒng)計,安全控制,事務(wù)處理,異常處理等代碼從業(yè)務(wù)邏輯代碼中劃分出來,通過對這些行為的分離,我們希望可以將它們獨(dú)立到非指導(dǎo)業(yè)務(wù)邏輯的方法中,進(jìn)而改變這些行為的時候不影響業(yè)務(wù)邏輯的代碼。

從最開始的面向機(jī)器編程到面向過程編程到面向?qū)ο髮ο缶幊讨敝连F(xiàn)在的面向切面編程可以看出,隨著計算機(jī)軟件的不斷發(fā)展,其越來越符合人的思維習(xí)慣,同樣的代碼量所能實(shí)現(xiàn)的功能也越來越多。而AOP則是在不增業(yè)務(wù)邏輯的代碼上,增加新功能。而AOP一般用于框架開發(fā)。在實(shí)際項(xiàng)目中,使用AOP也是一個趨勢。

AOP面向切面編程,是OOP擴(kuò)展和延伸,使用的是橫向抽取機(jī)制取代傳統(tǒng)的縱向繼承體系對程序進(jìn)行擴(kuò)展。解決OOP中遇到問題。

Spring中的AOP

Spring的AOP的底層實(shí)現(xiàn)

Spring提供兩種代理機(jī)制實(shí)現(xiàn)AOP JDK動態(tài)代理 :JDK只能對實(shí)現(xiàn)了接口的類產(chǎn)生代理。CGLIB動態(tài)代理 :可以對沒有實(shí)現(xiàn)接口的類產(chǎn)生代理,用的是底層字節(jié)碼增強(qiáng)技術(shù)。產(chǎn)生類繼承父類。
Spring的底層實(shí)現(xiàn)之JDK動態(tài)代理
package com.pc.aop;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;/** * JDK動態(tài)代理實(shí)現(xiàn) * 需要提供類的接口 * * @author Switch * @data 2016年11月23日 * @version V1.0 */public class JDKProxy<T> implements InvocationHandler { // 持有需要被增強(qiáng)的對象的引用 T target; /** * 構(gòu)造方法必須提供被代理對象 * * @param target */ public JDKProxy(T target) { this.target = target; } /** * 創(chuàng)建代理對象 * * @return */ public T createProxy() { T proxy = (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this); return proxy; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // 增強(qiáng),這里實(shí)現(xiàn)前置通知 System.out.println("前置通知。。。。。"); // 調(diào)用原有接口方法 return method.invoke(target, args); }}package com.pc.test;import org.junit.Test;import com.pc.aop.JDKProxy;import com.pc.service.UserService;import com.pc.service.impl.UserServiceImpl;/** * 代理測試類 * * @author Switch * @data 2016年11月23日 * @version V1.0 */public class ProxyTest { @Test public void testJDKProxy() { // 創(chuàng)建被代理對象 UserService userService = new UserServiceImpl(); // 未增強(qiáng)之前的方法 // 輸出:保存用戶 userService.save(); JDKProxy<UserService> jdkProxy = new JDKProxy<>(userService); // 創(chuàng)建代理對象 userService = jdkProxy.createProxy(); // 增強(qiáng)后的方法 // 輸出: // 前置通知。。。。。 // 保存用戶 userService.save(); }}
Spring的底層實(shí)現(xiàn)之CGLIB動態(tài)代理
package com.pc.aop;import java.lang.reflect.Method;import org.springframework.cglib.proxy.Enhancer;import org.springframework.cglib.proxy.MethodInterceptor;import org.springframework.cglib.proxy.MethodProxy;/** * CGLIB實(shí)現(xiàn)代理 * 使用字節(jié)碼增強(qiáng)技術(shù),生成代理類的子類 * * @author Switch * @data 2016年11月23日 * @version V1.0 */public class CGLIBProxy<T> implements MethodInterceptor { // 持有需要被增強(qiáng)的對象的引用 T target; /** * 構(gòu)造方法必須提供被代理對象 * * @param target */ public CGLIBProxy(T target) { this.target = target; } /** * 創(chuàng)建代理對象 * @return */ public T createProxy() { // 創(chuàng)建代理核心對象 Enhancer enhancer = new Enhancer(); // 設(shè)置父類 enhancer.setSuperclass(target.getClass()); // 設(shè)置回調(diào) enhancer.setCallback(this); // 創(chuàng)建代理對象 T proxy = (T) enhancer.create(); return proxy; } @Override public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { // 增強(qiáng),這里實(shí)現(xiàn)后置通知 // 調(diào)用原有類方法,獲得返回值 Object result = methodProxy.invokeSuper(proxy, args); // 增強(qiáng) System.out.println("后置通知。。。。。"); return result; }}package com.pc.test;import org.junit.Test;import com.pc.aop.CGLIBProxy;import com.pc.service.UserService;import com.pc.service.impl.UserServiceImpl;/** * 代理測試類 * * @author Switch * @data 2016年11月23日 * @version V1.0 */public class ProxyTest { @Test public void testCGLIBProxy() { // 創(chuàng)建被代理對象 UserService userService = new UserServiceImpl(); // 未增強(qiáng)之前的方法 // 輸出:保存用戶 userService.save(); CGLIBProxy<UserService> cglibProxy = new CGLIBProxy<>(userService); // 創(chuàng)建代理對象 userService = cglibProxy.createProxy(); // 增強(qiáng)后的方法 // 輸出: // 保存用戶 // 后置通知。。。。。 userService.save(); }}

Spring的AOP的術(shù)語

Spring的AOP開發(fā)分成兩類

傳統(tǒng)AOP開發(fā)基于AspectJ的AOP的開發(fā)(XML和注解)

AOP開發(fā)中的術(shù)語

這里寫圖片描述

切面(aspect):要實(shí)現(xiàn)的交叉功能,是系統(tǒng)模塊化的一個切面或領(lǐng)域。如日志記錄。連接點(diǎn)(join point):應(yīng)用程序執(zhí)行過程中插入切面的地點(diǎn),可以是方法調(diào)用,異常拋出,或者要修改的字段。通知(advice):切面的實(shí)際實(shí)現(xiàn),它通知系統(tǒng)新的行為。如在日志通知包含了實(shí)現(xiàn)日志功能的代碼,如向日志文件寫日志。通知在連接點(diǎn)插入到應(yīng)用系統(tǒng)中。切入點(diǎn)(pointcut):定義了通知應(yīng)該應(yīng)用在哪些連接點(diǎn),通知可以應(yīng)用到AOP框架支持的任何連接點(diǎn)。引介(introduction):為類添加新方法和屬性。目標(biāo)對象(target):被通知的對象。既可以是自己編寫的類也可以是第三方類。代理(proxy):將通知應(yīng)用到目標(biāo)對象后創(chuàng)建的對象,應(yīng)用系統(tǒng)的其他部分不用為了支持代理對象而改變。織入(Weaving):將切面應(yīng)用到目標(biāo)對象從而創(chuàng)建一個新代理對象的過程。織入發(fā)生在目標(biāo)對象生命周期的多個點(diǎn)上: 編譯期:切面在目標(biāo)對象編譯時織入。這需要一個特殊的編譯器。 類裝載期:切面在目標(biāo)對象被載入JVM時織入。這需要一個特殊的類載入器。 運(yùn)行期:切面在應(yīng)用系統(tǒng)運(yùn)行時織入。不需要特殊的編譯器。

PS:spring只支持方法連接點(diǎn),不提供屬性接入點(diǎn),spring的觀點(diǎn)是屬性攔截破壞了封裝。面向?qū)ο蟮母拍钍菍ο笞约禾幚砉ぷ鳎渌麑ο笾荒芡ㄟ^方法調(diào)用的得到的結(jié)果。

Spring的基于AspectJ的AOP的XML開發(fā)

第一步引入jar包

這里寫圖片描述

第二步創(chuàng)建配置文件

引入AOP的約束<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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> </beans>

第三步創(chuàng)建需增強(qiáng)包和類

創(chuàng)建接口package com.pc.aop.dao;/** * 客戶持久層接口 * * @author Switch * @data 2016年11月23日 * @version V1.0 */public interface CustomerDao { /** * 保存用戶 */ public void save();}創(chuàng)建實(shí)現(xiàn)類package com.pc.aop.dao.impl;import com.pc.aop.dao.CustomerDao;/** * 用戶持久層實(shí)現(xiàn)類 * * @author Switch * @data 2016年11月23日 * @version V1.0 */public class CustomerDaoImpl implements CustomerDao { @Override public void save() { System.out.println("保存用戶了。。。"); }}

第四步將類交給Spring管理

<bean id="customerDao" class="com.pc.aop.dao.impl.CustomerDaoImpl" />

第五步編寫測試

package com.pc.test;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.pc.aop.dao.CustomerDao;/** * 面向切面編程測試類 * * @author Switch * @data 2016年11月23日 * @version V1.0 */// 配置Spring單元測試環(huán)境@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class SpringAOPTest { // 注入依賴 @Resource(name = "customerDao") private CustomerDao customerDao; // 測試Spring單元測試集成 @Test public void testSpring() { customerDao.save(); }}

輸出

保存用戶了。。。

PS:這時候沒用使用AOP進(jìn)行任何增強(qiáng)

第六步編寫切面類和通知

package com.pc.aop.advice;import org.aspectj.lang.ProceedingJoinPoint;/** * 切面類 * * @author Switch * @data 2016年11月23日 * @version V1.0 */public class MyAspect { // 通知:校驗(yàn)權(quán)限 public void check() { System.out.println("校驗(yàn)權(quán)限。。。。。。"); }}

第七步 配置切面類

<!-- 配置切面類 --><bean id="myAspect" class="com.pc.aop.advice.MyAspect"/>

第八步通過配置實(shí)現(xiàn)AOP

<!-- 面向切面配置,基于aspectJ --><aop:config> <!-- 配置切入點(diǎn) ,切入點(diǎn)是通過表達(dá)式來實(shí)現(xiàn)的--> <aop:pointcut expression="execution(* com.pc.aop.dao.impl.CustomerDaoImpl.save(..))" id="pointcut1"/> <!-- 配置切面,切面是由具體的切入點(diǎn)和通知組成的 --> <aop:aspect ref="myAspect"> <!-- 增強(qiáng):前置通知 --> <aop:before method="check" pointcut-ref="pointcut1"/> </aop:aspect></aop:config>

第九步執(zhí)行測試

package com.pc.test;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.pc.aop.dao.CustomerDao;/** * 面向切面編程測試類 * * @author Switch * @data 2016年11月23日 * @version V1.0 */// 配置Spring單元測試環(huán)境@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class SpringAOPTest { // 注入依賴 @Resource(name = "customerDao") private CustomerDao customerDao; // 測試Spring單元測試集成 @Test public void testSpring() { customerDao.save(); }}

輸出

校驗(yàn)權(quán)限。。。。。。保存用戶了。。。

切入點(diǎn)表達(dá)式的定義

切入點(diǎn)表達(dá)式定義語法

切入點(diǎn)表達(dá)式基于execution之類的方法來實(shí)現(xiàn)的。在方法內(nèi)部就可以編寫切入點(diǎn)表達(dá)式: 表達(dá)式語法:[方法訪問修飾符] 方法返回值 [包名.類名.]方法名(參數(shù)) [異常類型]

這里寫圖片描述

舉例:

// 所有的public方法public * *(..)// 所有service中的public方法public * com.pc.service.*.*(..)// 所有以find開頭的方法* find*(..)// public修飾符,void返回類型,全路徑匹配public void com.pc.aop.dao.impl.CustomerDaoImpl.save(..)// 任意返回類型,全路徑匹配* com.pc.aop.dao.impl.CustomerDaoImpl.save(..)// 任意返回類型,類名以Dao結(jié)尾下的任意方法* com.pc.dao.*Dao.*(..)// 任意返回類型,CustomerDao及其子類下的任意方法* com.pc.dao.CustomerDao+.*(..)// 任意返回類型,com.pc.dao包下的任意方法* com.pc.dao..*.*(..)

通知類型

這里寫圖片描述

前置通知

前置通知:在目標(biāo)方法執(zhí)行之前完成的增強(qiáng)。獲得切入點(diǎn)信息。 PS:接入點(diǎn)信息,其他類型的增強(qiáng)也可以通過這種方法使用。

創(chuàng)建需增強(qiáng)類和方法
public class CustomerDaoImpl implements CustomerDao { @Override public void save() { System.out.println("保存用戶了。。。"); }}
創(chuàng)建切面和通知
public class MyAspect { // 通知:校驗(yàn)權(quán)限 public void check(JoinPoint joinPoint) { System.out.println("校驗(yàn)權(quán)限。。。。。。"); // 輸出接入點(diǎn)信息 System.out.println(joinPoint.toString()); }}
配置切面和通知
<!-- 配置切面類 --><bean id="myAspect" class="com.pc.aop.advice.MyAspect"/><!-- 面向切面配置,基于aspectJ --><aop:config> <!-- 配置切入點(diǎn) ,切入點(diǎn)是通過表達(dá)式來實(shí)現(xiàn)的--> <aop:pointcut expression="execution(* com.pc.aop.dao.impl.CustomerDaoImpl.save(..))" id="pointcut1"/> <!-- 配置切面,切面是由具體的切入點(diǎn)和通知組成的 --> <aop:aspect ref="myAspect"> <!-- 增強(qiáng):前置通知 --> <aop:before method="check" pointcut-ref="pointcut1"/> </aop:aspect></aop:config>
單元測試
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class SpringAOPTest { // 注入依賴 @Resource(name = "customerDao") private CustomerDao customerDao; // 測試前置通知 @Test public void testBefore() { customerDao.save(); }}

輸出

校驗(yàn)權(quán)限。。。。。。execution(void com.pc.aop.dao.CustomerDao.save())保存用戶了。。。

后置通知

后置通知:在目標(biāo)方法執(zhí)行之后完成的增強(qiáng)。獲得方法的返回值。

創(chuàng)建需增強(qiáng)類和方法
public class CustomerDaoImpl implements CustomerDao { @Override public String delete() { System.out.println("刪除用戶了。。。"); return "delete"; }}
創(chuàng)建切面和通知
public class MyAspect { // 通知:打印日志 public void printLog(String retVal) { System.out.println("打印日志。。。。。"); System.out.println("返回值為:" + retVal); }}
配置切面和通知
<!-- 配置切面類 --><bean id="myAspect" class="com.pc.aop.advice.MyAspect"/><!-- 面向切面配置,基于aspectJ --><aop:config> <!-- 配置切入點(diǎn) ,切入點(diǎn)是通過表達(dá)式來實(shí)現(xiàn)的--> <aop:pointcut expression="execution(* com.pc.aop.dao.impl.*.delete(..))" id="pointcut2"/> <!-- 配置切面,切面是由具體的切入點(diǎn)和通知組成的 --> <aop:aspect ref="myAspect"> <!-- 增強(qiáng):后置通知 --> <aop:after-returning method="printLog" pointcut-ref="pointcut2" returning="retVal"/> </aop:aspect></aop:config>
單元測試
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class SpringAOPTest { // 注入依賴 @Resource(name = "customerDao") private CustomerDao customerDao; // 測試后置通知 @Test public void testAfterRunning() { String delete = customerDao.delete(); System.out.println(delete); }}

輸出

刪除用戶了。。。打印日志。。。。。返回值為:deletedelete

環(huán)繞通知

環(huán)繞通知:在目標(biāo)方法執(zhí)行前和執(zhí)行后完成的增強(qiáng)。阻止目標(biāo)方法的執(zhí)行,獲得方法參數(shù)。

創(chuàng)建需增強(qiáng)類和方法
public class CustomerDaoImpl implements CustomerDao { @Override public void update(Integer id) { System.out.println("更新用戶了。。。"); }}
創(chuàng)建切面和通知
public class MyAspect { // 通知:計算方法耗時 public void calTime(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("方法執(zhí)行前。。。。。。"); // 打印參數(shù) System.out.println("參數(shù)為:" + joinPoint.getArgs()[0]); // 執(zhí)行目標(biāo)方法 joinPoint.proceed(); System.out.println("方法執(zhí)行后。。。。。。"); }}
配置切面和通知
<!-- 配置切面類 --><bean id="myAspect" class="com.pc.aop.advice.MyAspect"/><!-- 面向切面配置,基于aspectJ --><aop:config> <!-- 配置切入點(diǎn) ,切入點(diǎn)是通過表達(dá)式來實(shí)現(xiàn)的--> <aop:pointcut expression="execution(* com.pc.aop.dao.impl.*.update(..))" id="pointcut3"/> <!-- 配置切面,切面是由具體的切入點(diǎn)和通知組成的 --> <aop:aspect ref="myAspect"> <!-- 增強(qiáng):環(huán)繞通知 --> <aop:around method="calTime" pointcut-ref="pointcut3"/> </aop:aspect></aop:config>
單元測試
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class SpringAOPTest { // 注入依賴 @Resource(name = "customerDao") private CustomerDao customerDao; // 測試環(huán)繞通知 @Test public void testAround() { customerDao.update(6166); }}

輸出

方法執(zhí)行前。。。。。。參數(shù)為:6166更新用戶了。。。方法執(zhí)行后。。。。。。

異常拋出通知

異常拋出通知:在目標(biāo)方法執(zhí)行出現(xiàn)異常的時候完成的增強(qiáng)。獲得異常的信息。

創(chuàng)建需增強(qiáng)類和方法
public class CustomerDaoImpl implements CustomerDao { @Override public void find() { System.out.println("查詢用戶了。。。"); int i = 1 / 0; }}
創(chuàng)建切面和通知
public class MyAspect { // 通知:異常處理 public void throwHandler(Throwable ex) { System.out.println("異常處理。。。。。。" + ex.getMessage()); }}
配置切面和通知
<!-- 配置切面類 --><bean id="myAspect" class="com.pc.aop.advice.MyAspect"/><!-- 面向切面配置,基于aspectJ --><aop:config> <!-- 配置切入點(diǎn) ,切入點(diǎn)是通過表達(dá)式來實(shí)現(xiàn)的--> <aop:pointcut expression="execution(* com.pc.aop.dao.impl.*.find())" id="pointcut4"/> <!-- 配置切面,切面是由具體的切入點(diǎn)和通知組成的 --> <aop:aspect ref="myAspect"> <!-- 增強(qiáng):異常通知 --> <aop:after-throwing method="throwHandler" pointcut-ref="pointcut4" throwing="ex"/> </aop:aspect></aop:config>
單元測試
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class SpringAOPTest { // 注入依賴 @Resource(name = "customerDao") private CustomerDao customerDao; // 測試異常通知 @Test public void testAfterThrowing() { customerDao.find(); }}

輸出

查詢用戶了。。。異常處理。。。。。。/ by zero

最終通知

最終通知:無論目標(biāo)方法是否出現(xiàn)異常總是執(zhí)行的增強(qiáng)。

PS:該案例和異常測試案例對同一個target進(jìn)行增強(qiáng)。

創(chuàng)建需增強(qiáng)類和方法
public class CustomerDaoImpl implements CustomerDao { @Override public void find() { System.out.println("查詢用戶了。。。"); int i = 1 / 0; }}
創(chuàng)建切面和通知
public class MyAspect { // 通知:關(guān)閉資源 public void close() { System.out.println("關(guān)閉資源。。。。。。"); }}
配置切面和通知
<!-- 配置切面類 --><bean id="myAspect" class="com.pc.aop.advice.MyAspect"/><!-- 面向切面配置,基于aspectJ --><aop:config> <!-- 配置切入點(diǎn) ,切入點(diǎn)是通過表達(dá)式來實(shí)現(xiàn)的--> <aop:pointcut expression="execution(* com.pc.aop.dao.impl.*.find())" id="pointcut4"/> <!-- 配置切面,切面是由具體的切入點(diǎn)和通知組成的 --> <aop:aspect ref="myAspect"> <!-- 增強(qiáng):最終通知 --> <aop:after method="close" pointcut-ref="pointcut4"/> </aop:aspect></aop:config>
單元測試
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class SpringAOPTest { // 注入依賴 @Resource(name = "customerDao") private CustomerDao customerDao; // 測試最終通知 @Test public void testFinally() { customerDao.find(); }}

輸出

查詢用戶了。。。關(guān)閉資源。。。。。。異常處理。。。。。。/ by zero

PS:之后會在《Spring AOP技術(shù)(基于AspectJ)的Annotation開發(fā)中》中會介紹怎么使用注解進(jìn)行AOP開發(fā),用注解進(jìn)行AOP開發(fā)相對于XML來說更便捷,也更容易理解。這兩種掌握一種就可以了,但是如果要對AOP進(jìn)行集中管理,最好還是使用XML方式。

Spring AOP小項(xiàng)目

GitHub:Spring AOP小項(xiàng)目 GitHub:MyStore-netease


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 香河县| 土默特右旗| 荆州市| 松阳县| 恩平市| 平罗县| 永兴县| 临沧市| 二手房| 应用必备| 邳州市| 屏南县| 台中县| 鹿邑县| 壶关县| 武宁县| 会东县| 涞源县| 临沧市| 交城县| 垫江县| 桦南县| 宜州市| 黄陵县| 彩票| 怀安县| 富锦市| 平顶山市| 永年县| 巴里| 区。| 武功县| 宜兰市| 盈江县| 台江县| 蕉岭县| 沾益县| 孟连| 兰溪市| 盐亭县| 黄陵县|