SpringAOP 構(gòu)造注入的實現(xiàn)步驟
AOP_面向切面編程初步了解
讓我們先想象一個場景,你正在編寫一個項目,在開發(fā)過程中的多個模塊都有某段重復的代碼,于是你選擇將其抽象成一個方法,然后在需要的地方調(diào)用這個方法,當需要修改這段代碼時只需要修改這個方法就行。有一天,你的Boss給了新的需求,需要再抽象出一個方法,然后再在各個需要這個方法的模塊調(diào)用這個方法,這可能就讓你頭疼了,需要修改大量的代碼,于是會想,能不能不修改源代碼為系統(tǒng)業(yè)務(wù)添加某種功能呢?幸運的是,AOP可以很好的解決這個問題。
簡單介紹
AOP:保證開發(fā)者不修改源代碼的前提下,去為系統(tǒng)中的業(yè)務(wù)組件添加某種通用功能,本質(zhì)是由AOP框架修改業(yè)務(wù)組件的多個方法的源代碼,我們將其分為兩類:
- 靜態(tài)AOP
AOP 框架在編譯階段對程序源代碼進行修改,生成了靜態(tài)的 AOP 代理類(生成的*.class文件已經(jīng)被改掉了,需要使用特定的編譯器),比如 AspectJ。
- 動態(tài)AOP:
AOP 框架在運行階段對動態(tài)生成代理對象(在內(nèi)存中以 JDK 動態(tài)代理,或 CGlib 動態(tài)地生成 AOP 代理類),如 SpringAOP。
詳細說明
Spring 的通知類型
| 名稱 | 標簽 | 說明 |
|---|---|---|
| 前置通知 | @Before | 用于配置前置通知。指定增強的方法在切入點方法之前執(zhí)行 |
| 后置通知 | @AfterReturning | 用于配置后置通知。指定增強的方法在切入點方法之后執(zhí)行 |
| 環(huán)繞通知 | @Around | 用于配置環(huán)繞通知。指定增強的方法在切入點方法之前和之后都執(zhí)行 |
| 異常通知 | @AfterThrowing | 用于配置異常拋出通知。指定增強的方法在出現(xiàn)異常時執(zhí)行 |
| 最終通知 | @After | 用于配置最終通知。無論增強方式執(zhí)行是否有異常都會執(zhí)行 |
| 切面類注解 | @Aspect | 標注該當前類是一個切面類 |
| 斷點注解 | @Pointcut | 使用一個返回值為 void 、方法體為空的方法來命名切入點 |
實戰(zhàn)演練
導入依賴包
<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)建一個增強類以及其接口
增強類接口:
public interface VisitService {
//用于實現(xiàn)前置通知,后置通知,異常通知,最終通知
void visit(String str) throws Exception;
//用于實現(xiàn)環(huán)繞通知
void around();
}
增強類:
public class VisitServiceImpl implements VisitService {
//前置,后置,最終,異常通知的增強類
public void visit(String str) throws Exception{
System.out.println(str);
if(!str.equalsIgnoreCase("agree")){
throw new Exception("非法訪問");
}
}
//環(huán)繞通知的增強類
public void around() {
System.out.println("環(huán)繞通知");
}
}
創(chuàng)建一個切面類
@Component("VisitAspect")
@Aspect //標注當前myAspect是一個切面類
public class VisitAspect_anno {
// 定義切入點表達式
// 使用一個返回值為 void 、方法體為空的方法來命名切入點
@Pointcut("execution(* Spring_AOP.service.impl.VisitServiceImpl.visit(..))")
private void v1() {
}
//前置通知
@Before("v1()")
public void visitBefore(JoinPoint joinPoint) {
System.out.println("口令:");
}
@After("v1()")
//最終通知,無論是否報錯,都執(zhí)行
public void visitAfter(JoinPoint joinPoint) {
System.out.println("輸入完成");
}
@AfterReturning("v1()")
//后置通知報錯不執(zhí)行
public void visitSuccess(JoinPoint joinPoint) {
System.out.println("請求成功,歡迎");
}
@AfterThrowing(value = "v1()",throwing = "ex")
//異常通知,報錯后執(zhí)行
public void visitThrow(JoinPoint joinPoint, Throwable ex) {
System.out.println("請求失敗,拒絕");
}
@Around("execution(* Spring_AOP.service.impl.VisitServiceImpl.around())")
//環(huán)繞通知,如果報錯只執(zhí)行前一句
public Object visitAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("-------環(huán)繞-------");
Object obj = proceedingJoinPoint.proceed();
System.out.println("------------------");
return obj;
}
}
配置xml文件
<!-- 基于注解的聲明式 AspectJ -->
<context:component-scan base-package="Spring_AOP" />
<!-- 啟動基于注解的聲明式 AspectJ 支持一 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
創(chuàng)建一個測試類
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();
}
}
測試運行
口令:
agree
請求成功,歡迎
輸入完成
口令:
ok
請求失敗,拒絕
輸入完成
-------環(huán)繞-------
環(huán)繞通知
-------環(huán)繞-------
總結(jié)
使用構(gòu)造注入可以更方便的實現(xiàn)AOP模式,但是同樣與設(shè)置注入相比各有千秋。
以上就是以注解實現(xiàn)SpringAOP框架構(gòu)造注入的實現(xiàn),如有錯誤,麻煩指出,感謝耐心到現(xiàn)在的朋友ᕕ( ᐛ )ᕗ
到此這篇關(guān)于SpringAOP_構(gòu)造注入的實現(xiàn)步驟的文章就介紹到這了,更多相關(guān)SpringAOP_構(gòu)造注入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實現(xiàn)devtools實現(xiàn)熱部署過程解析
這篇文章主要介紹了SpringBoot實現(xiàn)devtools實現(xiàn)熱部署過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03
解析Java線程編程中的線程安全與synchronized的使用
這篇文章主要介紹了Java線程編程中的線程安全與synchronized的使用,synchronized多線程使用時一定要注意線程之間的沖突問題,需要的朋友可以參考下2015-12-12
詳解SpringCloudGateway內(nèi)存泄漏問題
這篇文章主要介紹了詳解SpringCloudGateway內(nèi)存泄漏問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
解決java字符串轉(zhuǎn)換成時間Unparseable date出錯的問題
這篇文章主要介紹了解決java字符串轉(zhuǎn)換成時間Unparseable date出錯的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring Boot配置線程池拒絕策略的場景分析(妥善處理好溢出的任務(wù))
本文通過實例代碼給大家介紹下如何為線程池配置拒絕策略、如何自定義拒絕策略。對Spring Boot配置線程池拒絕策略的相關(guān)知識感興趣的朋友一起看看吧2021-09-09

