Spring AOP 中@annotation的兩種寫法使用
在 Spring AOP 的開發(fā)中,我們經(jīng)??吹竭@樣的切面寫法:
@Before("@annotation(com.example.annotation.PayLog)")
public void beforeAnnotation() {
System.out.println("通過 @annotation 攔截帶 @PayLog 注解的方法");
}
或者這樣:
@Before("@annotation(payLog)")
public void beforeAnnotation(PayLog payLog) {
System.out.println("檢測(cè)到支付操作:" + payLog.value());
}
看起來很相似,但又不太一樣 ??
到底有什么區(qū)別?為什么都能生效?
這篇文章幫你徹底搞清楚!
一、什么是 @annotation
在 AOP(Aspect-Oriented Programming,面向切面編程)中,
我們通過切點(diǎn)表達(dá)式定義要攔截的方法。
其中,@annotation(...) 是一種非常常見的表達(dá)式,用來匹配:
“所有帶有指定注解的方法”。
舉個(gè)例子
假設(shè)我們定義了一個(gè)自定義注解 @PayLog:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PayLog {
String value() default "默認(rèn)支付";
}
二、寫法一:只攔截方法,不讀取注解內(nèi)容
@Aspect
@Component
public class PayAspect {
@Before("@annotation(com.example.annotation.PayLog)")
public void beforeAnnotation() {
System.out.println("通過 @annotation 攔截帶 @PayLog 注解的方法");
}
}
這段切面表示:
“在執(zhí)行任何帶有 @PayLog 注解的方法之前,先執(zhí)行我這個(gè)方法。”
示例:
@Service
public class OrderService {
@PayLog
public void pay() {
System.out.println("執(zhí)行支付邏輯...");
}
}
輸出:
通過 @annotation 攔截帶 @PayLog 注解的方法
執(zhí)行支付邏輯...
?? 解釋:
@annotation(com.example.annotation.PayLog)表示攔截所有被該注解標(biāo)記的方法;beforeAnnotation()方法執(zhí)行時(shí),并不會(huì)接收到注解對(duì)象;- 這種寫法適合只想攔截,不需要讀取注解里的內(nèi)容。
三、寫法二:攔截 + 獲取注解對(duì)象
@Aspect
@Component
public class PayAspect {
@Before("@annotation(payLog)")
public void beforeAnnotation(PayLog payLog) {
System.out.println("檢測(cè)到支付操作:" + payLog.value());
}
}
這段代碼看起來只多了一個(gè)參數(shù) payLog,
其實(shí)功能更強(qiáng)大——它可以直接獲取目標(biāo)方法上的注解實(shí)例。
示例
@Service
public class OrderService {
@PayLog("微信支付")
public void payWx() {
System.out.println("執(zhí)行微信支付邏輯...");
}
@PayLog("支付寶支付")
public void payAli() {
System.out.println("執(zhí)行支付寶支付邏輯...");
}
}
輸出結(jié)果:
檢測(cè)到支付操作:微信支付
執(zhí)行微信支付邏輯...
檢測(cè)到支付操作:支付寶支付
執(zhí)行支付寶支付邏輯...
?? 解釋:
@annotation(payLog)告訴 AOP:
攔截時(shí)把目標(biāo)方法上的@PayLog實(shí)例賦值給payLog參數(shù);- 于是我們可以直接通過
payLog.value()獲取注解中的值。
四、兩種寫法的對(duì)比總結(jié)
| 對(duì)比項(xiàng) | 寫法一 | 寫法二 |
|---|---|---|
| 表達(dá)式 | @annotation(com.example.annotation.PayLog) | @annotation(payLog) |
| 方法參數(shù) | 無 | 有(注解類型參數(shù)) |
| 是否能讀取注解內(nèi)容 | ? 否 | ? 可以 |
| 主要用途 | 只做攔截、執(zhí)行前后邏輯 | 需要讀取注解參數(shù)(如描述、類型等) |
| 示例應(yīng)用 | 簡(jiǎn)單記錄日志 | 根據(jù)注解參數(shù)執(zhí)行不同邏輯 |
五、完整運(yùn)行示例
1、自定義注解
package com.example.annotation;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PayLog {
String value() default "默認(rèn)支付類型";
}
2、AOP 切面類
package com.example.aspect;
import com.example.annotation.PayLog;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class PayAspect {
// 寫法一:只攔截
@Before("@annotation(com.example.annotation.PayLog)")
public void onlyIntercept() {
System.out.println("攔截到帶 @PayLog 注解的方法");
}
// 寫法二:攔截 + 獲取注解
@Before("@annotation(payLog)")
public void beforeWithAnnotation(PayLog payLog) {
System.out.println("檢測(cè)到支付操作:" + payLog.value());
}
}
3、被攔截的業(yè)務(wù)類
package com.example.service;
import com.example.annotation.PayLog;
import org.springframework.stereotype.Service;
@Service
public class OrderService {
@PayLog("微信支付")
public void payWx() {
System.out.println("執(zhí)行微信支付邏輯...");
}
@PayLog("支付寶支付")
public void payAli() {
System.out.println("執(zhí)行支付寶支付邏輯...");
}
}
4、輸出結(jié)果
攔截到帶 @PayLog 注解的方法
檢測(cè)到支付操作:微信支付
執(zhí)行微信支付邏輯...攔截到帶 @PayLog 注解的方法
檢測(cè)到支付操作:支付寶支付
執(zhí)行支付寶支付邏輯...
六、總結(jié)
@annotation 有兩種寫法:
- @annotation(注解類路徑):只攔截,不讀參數(shù);
- @annotation(變量名) + 方法參數(shù):攔截并獲取注解對(duì)象。
兩者都對(duì),只是用途不同。
| 場(chǎng)景 | 推薦寫法 |
|---|---|
| 只想攔截注解方法,不關(guān)心內(nèi)容 | @annotation(com.xxx.PayLog) |
| 需要讀取注解參數(shù)(如 type、desc) | @annotation(payLog) + PayLog payLog |
| 想統(tǒng)一記錄操作日志 | 推薦帶參數(shù)寫法,靈活性更強(qiáng) |
到此這篇關(guān)于Spring AOP 中@annotation的兩種寫法使用的文章就介紹到這了,更多相關(guān)Spring AOP @annotation內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
布隆過濾器(Bloom Filter)的Java實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄悸∵^濾器(Bloom Filter)的Java實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12
spring boot環(huán)境抽象的實(shí)現(xiàn)方法
在實(shí)際開發(fā)中,開發(fā)人員在編寫springboot的時(shí)候通常要在本地環(huán)境測(cè)試然后再部署到Production環(huán)境,這兩種環(huán)境一般來講是不同的,最主要的區(qū)別就是數(shù)據(jù)源的不同。本文主要介紹了這兩種,感興趣的可以了解一下2019-04-04
使用通過ARP類似P2P終結(jié)者實(shí)現(xiàn)數(shù)據(jù)封包
目前網(wǎng)絡(luò)上類似P2P終結(jié)者這類軟件,主要都是基于ARP欺騙實(shí)現(xiàn)的,網(wǎng)絡(luò)上到處都有關(guān)于ARP的介紹,不過為了本文讀者不需要再去查找,我就在這里大概講解一下2012-12-12
Spring全家桶中@CacheEvict無效情況的問題解決
本文主要介紹了SpringBoot中@CacheEvict注解刪除緩存無效的情況,主要包括Controller直接調(diào)用、key值不匹配、方法組織、返回值類型和代理機(jī)制,具有一定的參考價(jià)值,感興趣的可以了解一下2025-10-10
SpringBoot整合JPA詳細(xì)代碼實(shí)例
這篇文章主要給大家介紹了關(guān)于SpringBoot整合JPA的相關(guān)資料,JPA(Java Persistence API)是Sun官方提出的Java持久化規(guī)范,它為Java開發(fā)人員提供了一種對(duì)象/關(guān)聯(lián)映射工具來管理Java應(yīng)用中的關(guān)系數(shù)據(jù),需要的朋友可以參考下2024-05-05
java.math包下計(jì)算浮點(diǎn)數(shù)和整數(shù)的類的實(shí)例
這篇文章主要介紹了java.math包下計(jì)算浮點(diǎn)數(shù)和整數(shù)的類的實(shí)例代碼,本文通過使用BigDecimal進(jìn)行浮點(diǎn)數(shù)比較給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
關(guān)于Java中的實(shí)體類要?implements?Serializable的原因分析
這篇文章主要介紹了Java中的實(shí)體類為什么要?implements?Serializable,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06

