@annotation?AOP編程的pointcut定義方式
更新時間:2025年08月16日 10:07:27 作者:RR1335
文章介紹通過注解簡化AOP復雜結構定義,避免繁瑣的邏輯組合,提升開發(fā)效率,并展示IDE如何輔助查看切入點匹配
@annotation AOP編程的pointcut定義
通過
@Pointcut("execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))")在定義復雜結構的時候通過 || 或 && 等關系定義,會很繁瑣。
- 直接看代碼
@Slf4j
@Component
@Aspect
public class TryLoggingAspect {
// 切入點表達式,引用
@Pointcut("@annotation(biz.baijing.aop.TryLogging)")
public void poct() {}
@Before("poct()") // * 任意方法 , .. 形參任意
public void before(){
log.info("Before ...");
}
@After("poct()")
public void after(){
log.info("After ...");
}
}通過
@Pointcut("@annotation(biz.baijing.aop.TryLogging)")定義
- TryLogging 的代碼
package biz.baijing.aop;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TryLogging {
// 示例
}
定義的 「注解」
定義到 service 方法上

m 又出現了 (
又 的前面出現在這里 AOP編程的基本概念與idea編輯器的配合體驗

能看到作用到這個方法上的 pointcut 切入點的方法。
只要增加注解,就能匹配復雜的「切入點表達式」。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
詳解ConcurrentHashMap如何保證線程安全及底層實現原理
這篇文章主要為大家介紹了ConcurrentHashMap如何保證線程安全及底層實現原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
org.springframework.web.client.ResourceAccessException資源訪問錯誤
本文主要介紹了org.springframework.web.client.ResourceAccessException資源訪問錯誤的解決方法,首先需要分析異常的詳細信息,以確定具體的錯誤原因,感興趣的可以了解一下2024-05-05

