SpringAOP 設(shè)置注入的實(shí)現(xiàn)步驟
AOP_面向切面編程初步了解
讓我們先想象一個(gè)場(chǎng)景,你正在編寫(xiě)一個(gè)項(xiàng)目,在開(kāi)發(fā)過(guò)程中的多個(gè)模塊都有某段重復(fù)的代碼,于是你選擇將其抽象成一個(gè)方法,然后在需要的地方調(diào)用這個(gè)方法,當(dāng)需要修改這段代碼時(shí)只需要修改這個(gè)方法就行。有一天,你的Boss給了新的需求,需要再抽象出一個(gè)方法,然后再在各個(gè)需要這個(gè)方法的模塊調(diào)用這個(gè)方法,這可能就讓你頭疼了,需要修改大量的代碼,于是會(huì)想,能不能不修改源代碼為系統(tǒng)業(yè)務(wù)添加某種功能呢?幸運(yùn)的是,AOP可以很好的解決這個(gè)問(wèn)題。
簡(jiǎn)單介紹
AOP:保證開(kāi)發(fā)者不修改源代碼的前提下,去為系統(tǒng)中的業(yè)務(wù)組件添加某種通用功能,本質(zhì)是由AOP框架修改業(yè)務(wù)組件的多個(gè)方法的源代碼,我們將其分為兩類:
- 靜態(tài)AOP
AOP 框架在編譯階段對(duì)程序源代碼進(jìn)行修改,生成了靜態(tài)的 AOP 代理類(生成的*.class文件已經(jīng)被改掉了,需要使用特定的編譯器),比如 AspectJ。
- 動(dòng)態(tài)AOP:
AOP 框架在運(yùn)行階段對(duì)動(dòng)態(tài)生成代理對(duì)象(在內(nèi)存中以 JDK 動(dòng)態(tài)代理,或 CGlib 動(dòng)態(tài)地生成 AOP 代理類),如 SpringAOP。
詳細(xì)說(shuō)明
Spring 的通知類型
名稱 | 標(biāo)簽 | 說(shuō)明 |
---|---|---|
前置通知 | < aop:before > | 用于配置前置通知。指定增強(qiáng)的方法在切入點(diǎn)方法之前執(zhí)行 |
后置通知 | < aop:after-returning > | 用于配置后置通知。指定增強(qiáng)的方法在切入點(diǎn)方法之后執(zhí)行 |
環(huán)繞通知 | < aop:around > | 用于配置環(huán)繞通知。指定增強(qiáng)的方法在切入點(diǎn)方法之前和之后都執(zhí)行 |
異常通知 | < aop:throwing > | 用于配置異常拋出通知。指定增強(qiáng)的方法在出現(xiàn)異常時(shí)執(zhí)行 |
最終通知 | < aop:after > | 用于配置最終通知。無(wú)論增強(qiáng)方式執(zhí)行是否有異常都會(huì)執(zhí)行 |
實(shí)戰(zhàn)演練
導(dǎo)入依賴包
<dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.3.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.6</version> </dependency> <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance --> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
創(chuàng)建一個(gè)增強(qiáng)類以及其接口
增強(qiáng)類接口:
public interface VisitService { //用于實(shí)現(xiàn)前置通知,后置通知,異常通知,最終通知 void visit(String str) throws Exception; //用于實(shí)現(xiàn)環(huán)繞通知 void around(); }
增強(qiáng)類:
public class VisitServiceImpl implements VisitService { //前置,后置,最終,異常通知的增強(qiáng)類 public void visit(String str) throws Exception{ System.out.println(str); if(!str.equalsIgnoreCase("agree")){ throw new Exception("非法訪問(wèn)"); } } //環(huán)繞通知的增強(qiáng)類 public void around() { System.out.println("環(huán)繞通知"); } }
創(chuàng)建一個(gè)切面類
public class VisitAspect { //前置通知 public void visitBefore(JoinPoint joinPoint){ System.out.println("口令:"); } //最終通知,無(wú)論是否報(bào)錯(cuò),都執(zhí)行 public void visitAfter(JoinPoint joinPoint){ System.out.println("輸入完成"); } //后置通知報(bào)錯(cuò)不執(zhí)行 public void visitSuccess(JoinPoint joinPoint){ System.out.println("請(qǐng)求成功,歡迎"); } //異常通知,報(bào)錯(cuò)后執(zhí)行 public void visitThrow(JoinPoint joinPoint, Throwable ex){ System.out.println("請(qǐng)求失敗,拒絕"); } //環(huán)繞通知,如果報(bào)錯(cuò)只執(zhí)行前一句 public Object visitAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{ System.out.println("-------環(huán)繞-------"); Object obj = proceedingJoinPoint.proceed(); System.out.println("-------環(huán)繞-------"); return obj; } }
配置xml文件,需要添加第三方約束
<bean id="userDao" class="Spring_AOP.service.impl.VisitServiceImpl"></bean> <bean id="aspect" class="Spring_AOP.service.VisitAspect"></bean> <aop:config> <aop:pointcut id="pointcut" expression="execution(* Spring_AOP.service.impl.VisitServiceImpl.visit(..))"/> <aop:pointcut id="pointcut1" expression="execution(* Spring_AOP.service.impl.VisitServiceImpl.around())"/> <aop:aspect ref="aspect"> <aop:before method="visitBefore" pointcut-ref="pointcut"></aop:before> <aop:after method="visitAfter" pointcut-ref="pointcut"></aop:after> <aop:after-returning method="visitSuccess" pointcut-ref="pointcut"></aop:after-returning> <aop:around method="visitAround" pointcut-ref="pointcut1"></aop:around> <!-- 報(bào)錯(cuò)后執(zhí)行aop:after-throwing --> <aop:after-throwing method="visitThrow" pointcut-ref="pointcut" throwing="ex"></aop:after-throwing> </aop:aspect> </aop:config>
注,對(duì)于execution()
1、execution(): 表達(dá)式主體 (必須加上execution)。
2、第一個(gè)* 號(hào):表示返回值類型,* 號(hào)表示所有的類型。
3、包名:表示需要攔截的包名,后面的兩個(gè)句點(diǎn)表示當(dāng)前包和當(dāng)前包的所有子包,cn.smd.service.impl包、子孫包下所有類的方法。
4、第二個(gè)* 號(hào):表示類名,* 號(hào)表示所有的類。
5、* (..):最后這個(gè)星號(hào)表示方法名,* 號(hào)表示所有的方法,后面括弧里面表示方法的參數(shù),兩個(gè)句點(diǎn)表示任何參數(shù)。
書(shū)寫(xiě)的注意事項(xiàng):execution(* cn.smd.service.impl..(..))
創(chuàng)建一個(gè)測(cè)試類
public class visitTest { @Test public void VisitTest(){ ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext_AOP.xml"); VisitService visitService = app.getBean(VisitService.class); try { visitService.visit("agree"); } catch (Exception e) { e.printStackTrace(); } try { visitService.visit("ok"); } catch (Exception e) { e.printStackTrace(); } visitService.around(); } }
測(cè)試運(yùn)行
口令:
agree
請(qǐng)求成功,歡迎
輸入完成
口令:
ok
請(qǐng)求失敗,拒絕
輸入完成
-------環(huán)繞-------
環(huán)繞通知
-------環(huán)繞-------
總結(jié)
- SpringAOP進(jìn)一步降低組件的耦合,實(shí)現(xiàn)解耦合
- 可以更好的監(jiān)控程序,進(jìn)行權(quán)限攔截
- 注:學(xué)習(xí)AOP設(shè)置注入時(shí)需要注意出現(xiàn)報(bào)錯(cuò)時(shí)各個(gè)通知的狀態(tài)
以上就是SpringAOP 設(shè)置注入的實(shí)現(xiàn)步驟的詳細(xì)內(nèi)容,更多關(guān)于SpringAOP 設(shè)置注入的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot解決jar包沖突的問(wèn)題,簡(jiǎn)單有效
這篇文章主要介紹了SpringBoot解決jar包沖突的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09springboot攔截器Interceptor的使用,你都了解嗎
springmvc 中的攔截器可以對(duì)請(qǐng)求進(jìn)行判別,在請(qǐng)求到達(dá)控制器之前,把非法的請(qǐng)求給攔截掉下面來(lái)說(shuō)一說(shuō), 它在springboot中的使用,感興趣的朋友一起看看吧2021-07-07Springboot中使用Redisson+AOP+自定義注解實(shí)現(xiàn)訪問(wèn)限流與黑名單攔截
本文主要介紹了Springboot中使用Redisson+AOP+自定義注解實(shí)現(xiàn)訪問(wèn)限流與黑名單攔截,包含針對(duì)用戶IP限流,整個(gè)接口的訪問(wèn)限流,以及對(duì)某個(gè)參數(shù)字段的限流,并且支持請(qǐng)求限流后處理回調(diào),感興趣的可以了解一下2024-02-02Spring注解@Value在controller無(wú)法獲取到值的解決
這篇文章主要介紹了Spring注解@Value在controller無(wú)法獲取到值的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11SpringMVC獲取請(qǐng)求參數(shù)和域?qū)ο蠊蚕頂?shù)據(jù)的示例代碼
這篇文章主要給大家介紹了SpringMVC獲取請(qǐng)求參數(shù)和域?qū)ο蠊蚕頂?shù)據(jù)的示例代碼,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-12-12