Spring基于AspectJ的AOP開發(fā)案例解析
- 使用AspectJ實(shí)現(xiàn)AOP
- 注解方式
- XML方式
AspectJ簡介
- AspectJ是一個基于Java語言的AOP框架
- Spring2.0以后新增了對AspectJ切點(diǎn)表達(dá)式支持
- @AspectJ是AspectJ1.5新增的功能,通過JDK5注解技術(shù),允許直接在Bean類中定義切面
- 新版本Spring框架,建議使用AspectJ方式來開發(fā)AOP
- 使用AspectJ需要導(dǎo)入Spring AOP和AspectJ相關(guān)jar包
- Spring-aop
- springsource.org.aopalliance
- spring-aspects
- springsource.org.aspectj.weaver
注解開發(fā)
環(huán)境準(zhǔn)備
- 應(yīng)入相關(guān)的
- jar包junit,javax.annotation-api,spring-core,spring-beans,spring-context,spring-expression,aopalliance,spring-aop(Spring基礎(chǔ)包)
- aspectjweaver,spring-aspects(AspectJ使用的)
- spring-test(測試使用)
- XML引入相應(yīng)配置
<?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" 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"> <!-- bean definitions here --> <!--需要單獨(dú)引入--> <aop:aspectj-autoproxy/> </beans>
不同的通知類型
- @Before前置通知,相當(dāng)于BeforeAdvice
- @AfterReturning后置通知,相當(dāng)于AfterReturningAdvice
- @Around環(huán)繞通知,相當(dāng)于MethodInterceptor(可以阻止方法的進(jìn)行,功能最強(qiáng)。列:事務(wù)管理)
- @AfterThrowing異常拋出通知,相當(dāng)于ThrowAdvice
- @After最終final通知,不管是否異常,該通知都會執(zhí)行
- @DeclarwParents引介通知,相當(dāng)于IntroductionInterceptor(不要求掌握)
最通知中通過value屬性定義切點(diǎn)
- 通過execution函數(shù),可以定義切點(diǎn)的方法切入
- 語法: --execution(<訪問修飾符>?<返回類型><方法名>(<參數(shù)>)<異常>)(訪問修飾符可以省略)
- 列如
--匹配所有類public方法 execution(publice * * (..))---第一個*:任意返回值 第二個*:任意名稱 (..):任意參數(shù) 相當(dāng)于所有以public開頭的方法加了一個前置通知的話都會執(zhí)行 --匹配指定包下所有類方法 execution(* top.odliken.demo.(..)) 不包含子包 .:包 --execution(* top.odliken.demo..(..)) ..便是包、子酸包下所有類 --匹配指定類所有方法 execution( top.odlien.demo.UserService.(..)) 第一個*:任意返回值 UserService.:UserService類下面的所有方法 --匹配實(shí)現(xiàn)特定接口所有方法 execution( top.odliken.demo.GenericDao+.(..)) +:子類 .:方法名 --匹配所有save開頭的方法 execution(* save*(..))
入門案列
============XML========== <!--配置目標(biāo)類=================--> <bean id="customerDao" class="com.imooc.aspectJ.demo2.CustomerDaoImpl"/> <!--配置切面類--> <bean id="myAspectXml" class="com.imooc.aspectJ.demo2.MyAspectXml"/> ===========ProductDao=========== public class ProductDao { public void save() { System.out.println("保存商品....."); } public void findOne() { System.out.println("查找一個商品....."); } public void findAll() { System.out.println("查找所有商品....."); } public void update() { System.out.println("修改商品....."); } public void delete() { System.out.println("刪除商品....."); } } ===========MyAspectAnno=========== @Aspect public class MyAspectAnno { @Before(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.save(..))") public void before(){ System.out.println("=========前置通知========="); } } ===========Springdemo1=========== @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:afterglow.xml") public class Springdemo1 { @Resource(name = "productDao") private ProductDao productDao; @Test public void demo1(){ productDao.save(); productDao.findOne(); productDao.findAll(); productDao.update(); productDao.delete(); } }
@Before前置通知
- 可以在方法中傳入JoinPoint對象,用來獲取切點(diǎn)信息
public void before(JoinPoint joinPoint){ System.out.println("=========前置通知========="+joinPoint); }
@AfterReturning后置通知
- 通過returning屬性可以定義返回值,作為參數(shù)
@AfterReturning(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.update(..))",returning = "result") public void afterReturing(Object result){ System.out.println("==========后置通知=========="+result); }
@Around環(huán)繞通知
- Around方法的返回值就是目標(biāo)代理方法執(zhí)行返回值
- 參數(shù)ProceedingJoinPoint 可以調(diào)用攔截目標(biāo)方法執(zhí)行
- 如果不調(diào)用 ProceedingJoinPoint的 proceed方法,那么目標(biāo)方法就背攔截了
@Around(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.delete(..)))") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("環(huán)繞通知======前"); Object obj = joinPoint.proceed();//執(zhí)行目標(biāo)方法 不調(diào)用就不執(zhí)行 System.out.println("環(huán)繞通知======后"); return obj; }
@AfterThrowing 異常拋出通知
- 通過設(shè)置throwing屬性,可以設(shè)置發(fā)生異常對象參數(shù)
@AfterThrowing(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.findOne(..)))",throwing = "e") public void afterThrowing(Throwable e){ System.out.println("==========異常通知=========="+e.getMessage()); }
@After 最終通知
- 無論是否出現(xiàn)異常,最終通知總是會被執(zhí)行的。就像Java異常中的 finall塊一樣
@After(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.findAll(..))") public void after(){ System.out.println("==========最終通知=========="); }
通過@Pointcut為切點(diǎn)命名
- 在每個通知內(nèi)定義切點(diǎn),會造成工作量大,不易維護(hù),對于重復(fù)的切點(diǎn),可以使用@Pointcut進(jìn)行定義
- 切點(diǎn)方法:private void 無參方法,方法名為切點(diǎn)名
- 當(dāng)通知多個切點(diǎn)是,可以使用||連接
@Before(value = "myPointcut1()") public void before(JoinPoint joinPoint){ System.out.println("=========前置通知========="+joinPoint); } @Pointcut(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.save(..))") private void myPointcut1(){}
AspectJ的XML方式的AOP開發(fā)
==========CustomerDao========== public interface CustomerDao { public void save(); public String update(); public void delete(); public void findOne(); public void findAll(); } ==========CustomerDaoImpl========== public class CustomerDaoImpl implements CustomerDao { public void save() { System.out.println("保存客戶..."); } public String update() { System.out.println("修改客戶..."); return "spring"; } public void delete() { System.out.println("刪除客戶..."); } public void findOne() { System.out.println("查詢一個客戶..."); // int a = 1/ 0; } public void findAll() { System.out.println("查詢多個客戶..."); // int b = 1/0; } } ==========MyAspectXml========== public class MyAspectXml { // 前置通知 public void before(JoinPoint joinPoint) { System.out.println("XML方式的前置通知==============" + joinPoint); } // 后置通知 public void afterReturing(Object result) { System.out.println("XML方式的后置通知==============" + result); } // 環(huán)繞通知 public Object around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("XML方式的環(huán)繞前通知=============="); Object obj = joinPoint.proceed(); System.out.println("XML方式的環(huán)繞后通知=============="); return obj; } // 異常拋出通知 public void afterThrowing(Throwable e) { System.out.println("XML方式的異常拋出通知=============" + e.getMessage()); } // 最終通知 public void after() { System.out.println("XML方式的最終通知================="); } } ==========SpringDemo2========== @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value="classpath:applicationContext2.xml") public class SpringDemo2 { @Resource(name="customerDao") private CustomerDao customerDao; @Test public void demo1(){ customerDao.save(); customerDao.update(); customerDao.delete(); customerDao.findOne(); customerDao.findAll(); } } ==========XML========== <!--XML的配置方式完成AOP的開發(fā)===============--> <!--配置目標(biāo)類=================--> <bean id="customerDao" class="com.imooc.aspectJ.demo2.CustomerDaoImpl"/> <!--配置切面類--> <bean id="myAspectXml" class="com.imooc.aspectJ.demo2.MyAspectXml"/> <!--aop的相關(guān)配置=================--> <aop:config> <!--配置切入點(diǎn)--> <aop:pointcut id="pointcut1" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.save(..))"/> <aop:pointcut id="pointcut2" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.update(..))"/> <aop:pointcut id="pointcut3" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.delete(..))"/> <aop:pointcut id="pointcut4" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findOne(..))"/> <aop:pointcut id="pointcut5" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findAll(..))"/> <!--配置AOP的切面--> <aop:aspect ref="myAspectXml"> <!--配置前置通知--> <aop:before method="before" pointcut-ref="pointcut1"/> <!--配置后置通知--> <aop:after-returning method="afterReturing" pointcut-ref="pointcut2" returning="result"/> <!--配置環(huán)繞通知--> <aop:around method="around" pointcut-ref="pointcut3"/> <!--配置異常拋出通知--> <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" throwing="e"/> <!--配置最終通知--> <aop:after method="after" pointcut-ref="pointcut5"/> </aop:aspect> </aop:config>
到此這篇關(guān)于Spring的基于AspectJ的AOP開發(fā)的文章就介紹到這了,更多相關(guān)Spring基于AspectJ的AOP開發(fā)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC自定義攔截器登錄檢測功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了SpringMVC自定義攔截器登錄檢測功能的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08Java數(shù)據(jù)結(jié)構(gòu)之對象的比較
比較對象是面向?qū)ο缶幊陶Z言的一個基本特征,下面這篇文章主要給大家介紹了關(guān)于Java數(shù)據(jù)結(jié)構(gòu)之對象的比較,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02Springboot和Jpa實(shí)現(xiàn)學(xué)生CRUD操作代碼實(shí)例
這篇文章主要介紹了Springboot和Jpa實(shí)現(xiàn)學(xué)生CRUD操作代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03Spring Boot實(shí)現(xiàn)模塊化的幾種方法
模塊可以是業(yè)務(wù)模塊,為應(yīng)用程序提供一些業(yè)務(wù)服務(wù),或者為幾個其他模塊或整個應(yīng)用程序提供跨領(lǐng)域關(guān)注的技術(shù)模塊。這篇文章主要介紹了Spring Boot實(shí)現(xiàn)模塊化,需要的朋友可以參考下2018-07-07對Java ArrayList的自動擴(kuò)容機(jī)制示例講解
今天小編就為大家分享一篇對Java ArrayList的自動擴(kuò)容機(jī)制示例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10Mybatis實(shí)戰(zhàn)教程之入門到精通(經(jīng)典)
MyBatis是支持普通SQL查詢,存儲過程和高級映射的優(yōu)秀持久層框架,通過本文給大家介紹Mybatis實(shí)戰(zhàn)教程之入門到精通,對mybatis實(shí)戰(zhàn)教程相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-01-01