@annotation AOP编程的pointcut定义方式
更新时间:2025年08月16日 10:07:27 作者:RR1335
文章介绍通过注解简化AOP复杂结构定义,避免繁琐的逻辑组合,提升开发效率,并展示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


最新评论