Spring?Aop常見注解與執(zhí)行順序詳解
Spring 一開始最強大的就是 IOC / AOP 兩大核心功能,我們今天一起來學習一下 Spring AOP 常見注解和執(zhí)行順序。
Spring Aop 的常用注解
首先我們一起來回顧一下 Spring Aop 中常用的幾個注解:
- @Before 前置通知:目標方法之前執(zhí)行
- @After 后置通知:目標方法之后執(zhí)行(始終執(zhí)行)
- @AfterReturning 返回之后通知:執(zhí)行方法結束之前執(zhí)行(異常不執(zhí)行)
- @AfterThrowing 異常通知:出香異常后執(zhí)行
- @Around 環(huán)繞通知:環(huán)繞目標方法執(zhí)行
常見問題
1、你肯定知道 Spring , 那說說 Aop 的去全部通知順序, Spring Boot 或者 Spring Boot 2 對 aop 的執(zhí)行順序影響?
2、說說你在 AOP 中遇到的那些坑?
示例代碼
下面我們先快速構建一個 spring aop 的 demo 程序來一起討論 spring aop 中的一些細節(jié)。
配置文件
為了方便我直接使用 spring-boot 進行快速的項目搭建,大家可以使用 idea 的spring-boot 項目快速創(chuàng)建功能,或者去 start.spring.io 上面去快速創(chuàng)建spring-boot 應用。(因為本人經常手動去網上貼一些依賴導致,依賴沖突服務啟動失敗等一些問題)。
plugins {
id 'org.springframework.boot' version '2.6.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group 'io.zhengsh'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
dependencies {
# 其實這里也可以不增加 web 配置,為了試驗簡單,大家請忽略
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-aop'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}接口類
首先我們需要定義一個接口。我們這里可以再來回顧一下 JDK 的默認代理實現(xiàn)的選擇:
如果目標對象實現(xiàn)了接口,則默認采用JDK動態(tài)代理
如果目標對象沒有實現(xiàn)接口,則采用進行動態(tài)代理
如果目標對象實現(xiàn)了接口,且強制Cglib,則使用cglib代理
這塊的邏輯在 DefaultAopProxyFactory 大家有興趣可以去看看。
public interface CalcService {
public int div(int x, int y);
}實現(xiàn)類
這里我門就簡單一點做一個除法操作,可以模擬正常也可以很容易的模擬錯誤。
@Service
public class CalcServiceImpl implements CalcService {
@Override
public int div(int x, int y) {
int result = x / y;
System.out.println("====> CalcServiceImpl 被調用了,我們的計算結果是:" + result);
return result;
}
}aop 攔截器
申明一個攔截器我們要為當前對象增加 @Aspect 和 @Component ,筆者之前也是才踩過這樣的坑,只加了一個。
其實這塊我剛開始也不是很理解,但是我看了 Aspect 注解的定義我就清楚了

這里面根本就沒有 Bean 的定義。所以我們還是乖乖的加上兩個注解。 還有就是如果當測試的時候需要開啟Aop 的支持為配置類上增加 @EnableAspectJAutoProxy 注解。
其實 Aop 使用就三個步驟:
1、定義 Aspect 定義切面
2、定義 Pointcut 就是定義我們切入點
3、定義具體的通知,比如: @After, @Before 等。
@Aspect
@Component
public class MyAspect {
@Pointcut("execution(* io.zhengsh.spring.service.impl..*.*(..))")
public void divPointCut() {
}
@Before("divPointCut()")
public void beforeNotify() {
System.out.println("----===>> @Before 我是前置通知");
}
@After("divPointCut")
public void afterNotify() {
System.out.println("----===>> @After 我是后置通知");
}
@AfterReturning("divPointCut")
public void afterReturningNotify() {
System.out.println("----===>> @AfterReturning 我是前置通知");
}
@AfterThrowing("divPointCut")
public void afterThrowingNotify() {
System.out.println("----===>> @AfterThrowing 我是異常通知");
}
@Around("divPointCut")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object retVal;
System.out.println("----===>> @Around 環(huán)繞通知之前 AAA");
retVal = proceedingJoinPoint.proceed();
System.out.println("----===>> @Around 環(huán)繞通知之后 BBB");
return retVal;
}
}測試類
其實我這個測試類,雖然用了 @Test 注解,但是我這個類更加像一個 main 方法把:如下所示:

執(zhí)行結論
結果記錄:spring 4.x, spring-boot 1.5.9
無法現(xiàn)在依賴,所以無法試驗
我直接說一下結論: Spring 4 中環(huán)繞通知是在最里面執(zhí)行的
結果記錄:spring 版本5.3.15 springboot 版本2.6.3

多切面的情況
多個切面的情況下,可以通過@Order指定先后順序,數(shù)字越小,優(yōu)先級越高。 如下圖所示:

代理失效場景
下面一種場景會導致 aop 代理失效,因為我們在執(zhí)行 a 方法的時候其實本質是執(zhí)行 AServer#a 的方法攔截器(MethodInterceptor)鏈, 當我們在 a 方法內直接執(zhí)行b(), 其實本質就相當于 this.b() , 這個時候由執(zhí)行 a方法是調用到 a 的原始對象相當于是 this 調用,那么會導致 b() 方法的代理失效。這個問題也是我們開發(fā)者在開發(fā)過程中最常遇到的一個問題。
@Service
public class AService {
public void a() {
System.out.println("...... a");
b();
}
public void b() {
System.out.println("...... b");
}
}總結
到此這篇關于Spring Aop常見注解與執(zhí)行順序的文章就介紹到這了,更多相關Spring Aop常見注解內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java使用JDBC動態(tài)創(chuàng)建數(shù)據(jù)表及SQL預處理的方法
這篇文章主要介紹了java使用JDBC動態(tài)創(chuàng)建數(shù)據(jù)表及SQL預處理的方法,涉及JDBC操作數(shù)據(jù)庫的連接、創(chuàng)建表、添加數(shù)據(jù)、查詢等相關實現(xiàn)技巧,需要的朋友可以參考下2017-08-08
詳解Mybatis是如何把數(shù)據(jù)庫數(shù)據(jù)封裝到對象中的
這篇文章主要介紹了Mybatis是如何把數(shù)據(jù)庫數(shù)據(jù)封裝到對象中的,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12
IDEA插件Statistic統(tǒng)計代碼快速分辨爛項目
這篇文章主要為大家介紹了使用IDEA插件Statistic來統(tǒng)計項目代碼,幫助大家快速識別出爛項目,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-01-01
Java和C語言分別實現(xiàn)水仙花數(shù)及拓展代碼
這篇文章主要介紹了分別用Java和C語言實現(xiàn)水仙花數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-11-11

