【问题标题】:How can Spring AOP matches annotation on interface method?Spring AOP如何匹配接口方法上的注解?
【发布时间】:2017-09-14 08:49:34
【问题描述】:

我用的是mybatis。我的问题是 Spring AOP 如何匹配接口方法上的注释?因为我想在注解中放一些参数,然后在 afterReturning 方法中处理它们。

我的注释:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CacheClear {
    String key() default "";
}

在映射器类中:

@CacheClear
List<BizInst> selectAllBizInsts();

在我看来:

当使用“执行...”时它可以工作

@AfterReturning("execution(public * com.dao.*.select*(..))")
public void doAfterReturning(){
    System.out.println("after returning");
}

但是当使用“@annotation(...)”时它不起作用

@AfterReturning("@annotation(com.annotation.CacheClear)")
public void doAfterReturning(){
    System.out.println("after returning");
}

【问题讨论】:

    标签: java spring spring-mvc annotations spring-aop


    【解决方案1】:

    您可以执行类似的操作来选择带有 CacheClear 注释的公共 dao 方法:

    @Pointcut("execution(@com.yourPackage.CacheClear * *(..))")
    public void methodAnnotatedWithCacheClear( ) {}
    
    @Pointcut("execution(public * com.dao.*.select*(..))")
    public void publicDAOMethod() {}
    
    @AfterReturning(pointcut = "methodAnnotatedWithCacheClear() && publicDAOMethod()", returning = "result")
    public void doStuff(JoinPoint joinPoint, Object result) {
    

    【讨论】:

      猜你喜欢
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      • 2011-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多