Spring?AOP的注解實現(xiàn)方式實例詳解
一、AOP 與 Spring AOP
AOP:Aspect Oriented Programming(?向方?編程)。是一種對某一類事情集中處理的思想。
Spring AOP:就是對AOP思想的一種實現(xiàn)。
二、Spring AOP簡單實現(xiàn)
我們簡單實現(xiàn)一個統(tǒng)計每個接口的用時。
引入依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
寫AOP實現(xiàn):
- 類使用注解@Aspect修飾
- 方法參數(shù)為ProceedingJoinPoint 類,代表要實現(xiàn)的方法(只能在Around通知下寫)
- 方法使用注解@Around,參數(shù)是對應(yīng)的路徑的切點
- ProceedingJoinPoint 的參數(shù)執(zhí)行proceed方法。
package com.example.library.aspect;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
@Slf4j
public class TimeAspect {
@Around("execution(* com.example.library.controller.*.*(..) )")
public Object recordTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//開始時間
long start = System.currentTimeMillis();
//執(zhí)行方法
Object result = proceedingJoinPoint.proceed();
//結(jié)束時間
long end = System.currentTimeMillis();
log.info("執(zhí)行時間:"+ (end-start) + "ms");
return result;
}
}三、詳解Spring AOP
3.1 Spring AOP 核心概念
Spring AOP 核心概念:切點,連接點,通知,切面。
我們以上面的代碼來介紹。
3.1.1 切點(Pointcut)
切點:就是告訴程序哪些方法需要使用到接下來的功能。
上面的@Around注解的參數(shù)就是切點表達(dá)式。

3.1.2 連接點(Join Point)
連接點:滿?切點表達(dá)式規(guī)則的?法,就是連接點。也就是可以AOP控制的?法。
就像上面的代碼的連接點就是:com.example.library.controller路徑下的所有方法。
切點和連接點的關(guān)系:
- 連接點是滿?切點表達(dá)式的元素。
- 切點可以看做是保存了眾多連接點的?個集合。
3.1.3 通知(Advice)
通知:這個Spring AOP方法要實現(xiàn)的功能就是通知。
就像上面的實現(xiàn)一個統(tǒng)計每個接口的用時的需求,就是通知。

3.1.4 切面(Aspect)
切?(Aspect) = 切點(Pointcut) + 通知(Advice)。
通過切?就能夠描述當(dāng)前AOP程序需要針對于哪些?法,在什么時候執(zhí)?什么樣的操作。
切?既包含了通知邏輯的定義,也包括了連接點的定義。

3.2 通知類型
Spring中AOP的通知類型有以下?種:
- @Around:環(huán)繞通知,此注解標(biāo)注的通知?法在?標(biāo)?法前,后都被執(zhí)?。
- @Before:前置通知,此注解標(biāo)注的通知?法在?標(biāo)?法前被執(zhí)?。
- @After:后置通知,此注解標(biāo)注的通知?法在?標(biāo)?法后被執(zhí)?,?論是否有異常都會執(zhí)?。
- @AfterReturning:返回后通知,此注解標(biāo)注的通知?法在?標(biāo)?法后被執(zhí)?,有異常不會執(zhí)?。
- @AfterThrowing:異常后通知,此注解標(biāo)注的通知?法發(fā)?異常后執(zhí)?。
效果:
package com.example.demoaop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
@Slf4j
@Aspect
public class TestAspect {
//前置通知
@Before("execution(* com.example.demoaop.*.*(..) )")
public void testBefore() {
log.info("Before 方法執(zhí)行前");
}
//后置通知
@After("execution(* com.example.demoaop.*.*(..) )")
public void testAfter() {
log.info("After 方法執(zhí)行后");
}
//返回后通知
@AfterReturning("execution(* com.example.demoaop.*.*(..) )")
public void testAfterReturning() {
log.info("AfterReturning 返回后通知");
}
//拋出異常后通知
@AfterThrowing("execution(* com.example.demoaop.*.*(..) )")
public void testAfterThrowing() {
log.info("AfterThrowing 拋出異常后通知");
}
//環(huán)繞通知
@Around("execution(* com.example.demoaop.*.*(..) )")
public void testAround(ProceedingJoinPoint pjp) throws Throwable {
log.info("Around 方法執(zhí)行前");
Object proceed = pjp.proceed();
log.info("Around 方法執(zhí)行后");
}
}
3.3 公共切點引用@PointCut
當(dāng)我們的切點表達(dá)式是一樣的時候,像上面我們還是在每一個通知類型的注解中,都使用了相同的表達(dá)式。
我們就可以使用方法注解@PointCut將切點表達(dá)式提取出來,然后后面使用只需要寫方法名即可。
在其他切點類中也可以調(diào)用,需要將@PointCut注解所在類的路徑寫出來。
package com.example.demoaop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Slf4j
@Aspect
public class TestAspect {
@Pointcut("execution(* com.example.demoaop.*.*(..) )")
public void pc(){}
//前置通知
@Before("pc()")
public void testBefore() {
log.info("TestAspect Before 方法執(zhí)行前");
}
//后置通知
@After("pc()")
public void testAfter() {
log.info("TestAspect After 方法執(zhí)行后");
}
}package com.example.demoaop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Slf4j
@Aspect
public class TestAspect2 {
//前置通知
@Before("com.example.demoaop.TestAspect.pc()")
public void testBefore() {
log.info("TestAspect2 Before 方法執(zhí)行前");
}
//后置通知
@After("pc()")
public void testAfter() {
log.info("TestAspect2 After 方法執(zhí)行后");
}
}結(jié)果:
可以看見生效了,而且在其他切點類中只有加上了路徑的才生效了。

3.4 切點優(yōu)先級@Order
我們定義3個一樣的切點類,看他們的輸出順序:

存在多個切?類時,默認(rèn)按照切?類的類名字?排序:
- @Before 通知:字?排名靠前的先執(zhí)?
- @After 通知:字?排名靠前的后執(zhí)?
但這種?式不?便管理,我們的類名更多還是具備?定含義的。
Spring 給我們提供了?個新的注解,來控制這些切?通知的執(zhí)?順序:@Order
我們將切點類的優(yōu)先級換一下:
@Component
@Slf4j
@Aspect
@Order(1)
public class TestAspect3 {
//前置通知
@Before("com.example.demoaop.TestAspect.pc()")
public void testBefore() {
log.info("TestAspect3 Before 方法執(zhí)行前");
}
//后置通知
@After("com.example.demoaop.TestAspect.pc()")
public void testAfter() {
log.info("TestAspect3 After 方法執(zhí)行后");
}
}@Component
@Slf4j
@Aspect
@Order(2)
public class TestAspect2 {
//前置通知
@Before("com.example.demoaop.TestAspect.pc()")
public void testBefore() {
log.info("TestAspect2 Before 方法執(zhí)行前");
}
//后置通知
@After("com.example.demoaop.TestAspect.pc()")
public void testAfter() {
log.info("TestAspect2 After 方法執(zhí)行后");
}
}@Component
@Slf4j
@Aspect
@Order(3)
public class TestAspect {
@Pointcut("execution(* com.example.demoaop.*.*(..) )")
public void pc(){}
//前置通知
@Before("pc()")
public void testBefore() {
log.info("TestAspect Before 方法執(zhí)行前");
}
//后置通知
@After("pc()")
public void testAfter() {
log.info("TestAspect After 方法執(zhí)行后");
}
}執(zhí)行結(jié)果:

規(guī)律:
@Order 注解標(biāo)識的切?類,執(zhí)?順序如下:
- @Before 通知:數(shù)字越?先執(zhí)?
- @After 通知:數(shù)字越?先執(zhí)?
像下圖的表示,箭頭代表執(zhí)行過程:

3.5 切點表達(dá)式
切點表達(dá)式用來描述切點,常有以下兩種類型的切點表達(dá)式:execution 和 @annotation
3.5.1 execution
語法:
execution(<訪問修飾限定符> <返回類型> <包名.類名.方法名(方法參數(shù))> <異常>)
含義:
- 訪問修飾限定符:表示切點對應(yīng)的方法的訪問修飾限定符
- 返回類型:表示切點對應(yīng)的方法的返回類型
- 包名.類名.方法名(方法參數(shù)):表示切點對應(yīng)的方法的路徑及參數(shù)
- 異常:表示切點對應(yīng)的方法拋出的異常
- 訪問修飾限定符 和 異常 可以省略
切點表達(dá)式?持通配符表達(dá):
:* 匹配任意字符,只匹配?個元素(返回類型,包,類名,?法或者?法參數(shù))
1.1. 包名使? * 表?任意包(?層包使??個 * )
1.2. 類名使? * 表?任意類
1.3. 返回值使? * 表?任意返回值類型
1.4. ?法名使? * 表?任意?法
1.5. 參數(shù)使? * 表??個任意類型的參數(shù): 兩個點 . . 匹配多個連續(xù)的任意符號,可以通配任意層級的包,或任意類型,任意個數(shù)的參數(shù)
2.1. 使? . . 配置包名,標(biāo)識此包以及此包下的所有?包
2.2. 可以使? . . 配置參數(shù),任意個任意類型的參數(shù)
例子:
TestController 下的 public修飾,返回類型為String ?法名為t1的?參?法
execution(public String com.example.demo.TestController.t1())
匹配 TestController 下的所有?參?法
execution(* com.example.demo.TestController.*())
匹配controller包下所有的類的所有?法
execution(* com.example.demo.controller.*.*(..))
3.5.2 @annotation
當(dāng)我們要落實到不同類下個幾個方法,用上面的execution就有點捉襟見肘。
我們就可以使用?定義注解的?式以及另?種切點表達(dá)式 @annotation 來描述這?類的切點。
自定義注解:
在自定義類的時候選擇annotation:

然后就跟我們前面使用的注解一樣包含,生命周期@Retention,作用范圍@Target,交給Spring管理。
@Component
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAspect {
}定義切面類:
- 使用@Aspect注解修飾,
- 交給Spring管理
- 在通知類型的注解中使用:@annotation(自定義注解路徑) 作為參數(shù)。
@Slf4j
@Component
@Aspect
public class MyAspectDemo {
@Around("@annotation( com.example.demoaop.MyAspect)")
public void around(ProceedingJoinPoint pjp) throws Throwable {
log.info("annotation 運行前");
pjp.proceed();
log.info("annotation 運行后");
}
}通過上面的方法,使用了自定義注解修飾的方法,就可以添加切面類的通知。
@RequestMapping("/test")
@RestController
@Slf4j
public class Test {
@RequestMapping("/f1")
public String f1() {
log.info("f1");
return "s1";
}
@MyAspect
@RequestMapping("/f2")
public Integer f2() {
log.info("f2");
return 1;
}
@RequestMapping("/f3")
public Boolean f3() {
log.info("f3");
return false;
}
}訪問f2 f1 f3 的結(jié)果:

除了上面講的基于注解的方式實現(xiàn)Spring AOP 還有遠(yuǎn)古的通過xml和代理的方式實現(xiàn)。參考Spring AOP其它實現(xiàn)方式
到此這篇關(guān)于Spring AOP的注解實現(xiàn)的文章就介紹到這了,更多相關(guān)Spring AOP注解實現(xiàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis基于pagehelper實現(xiàn)分頁原理及代碼實例
這篇文章主要介紹了MyBatis基于pagehelper實現(xiàn)分頁原理及代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Spring Cloud Consul實現(xiàn)選舉機制的代碼工程
Spring Cloud Consul 是 Spring Cloud 提供的對 HashiCorp Consul 的支持,它是一種基于服務(wù)網(wǎng)格的工具,用于實現(xiàn)服務(wù)注冊、發(fā)現(xiàn)、配置管理和健康檢查,本文給大家介紹了如何用Spring Cloud Consul實現(xiàn)選舉機制,需要的朋友可以參考下2024-11-11
SpringBoot使用Redis Stream實現(xiàn)輕量消息隊列的示例代碼
Redis Stream 是 Redis 5.0 引入的一種數(shù)據(jù)結(jié)構(gòu),用于處理日志類型的數(shù)據(jù),它提供了高效、可靠的方式來處理和存儲時間序列數(shù)據(jù),如事件、消息等,本文介紹了SpringBoot使用Redis Stream實現(xiàn)輕量消息隊列,需要的朋友可以參考下2024-08-08
Struts2中validate數(shù)據(jù)校驗的兩種方法詳解附Struts2常用校驗器
這篇文章主要介紹了Struts2中validate數(shù)據(jù)校驗的兩種方法及Struts2常用校驗器,本文介紹的非常詳細(xì),具有參考借鑒價值,感興趣的朋友一起看看吧2016-09-09

