@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 切入点的方法。
只要增加注解,就能匹配复杂的「切入点表达式」。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论