在Super类中具有自定义注释的AOP不起作用 [英] AOP with custom annotation in Super class doesn't work

查看:89
本文介绍了在Super类中具有自定义注释的AOP不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自定义注释

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation {
}

自定义注释处理程序

@Aspect
public class TestAspectHandler {
    @Around("execution(@com.test.project.annotaion.CustomAnnotation * *(..)) && @annotation(customAnnotation)")
    public Object testAnnotation(ProceedingJoinPoint joinPoint, CustomAnnotation customAnnotation) throws Throwable {
        System.out.println("TEST");
        return result;
    }
}

超一流

public class AbstractDAO {
     @CustomAnnotation
     protected int selectOne(Object params){
          // .... something
     }
}

子类

public class SubDAO extends AbstractDAO {
    public int selectSub(Object params) {
         return selectOne(params);
    }
}

子类调用超类方法selectOne,但在TestAspectHandler.class中不调用testAnnotation(...)

当我将@CustomAnnotation移到子类selectSub(..)方法时,AspectHandler可以获取joinPoint

when i move @CustomAnnotation to subclass selectSub(..) method AspectHandler can get joinPoint

如何在超类保护的方法中使用自定义注释?

how to work with Custom annotation in super class protected method?

added

更改TestAspectHandler.testAnnotation(...) method

@Around("execution(* *(..)) && @annotation(customAnnotation)")
public Object testAnnotation(ProceedingJoinPoint joinPoint, CustomAnnotation customAnnotation) throws Throwable {
            System.out.println("TEST");
            return result;
}

但仍然不起作用

所以我喜欢在代码下更改我的SubDAO

so i chnage my SubDAO like under code

public class SubDAO extends AbstractDAO {
    @Autowired
    private AbstractDAO abstractDAO;
    public int selectSub(Object params) {
         return abstractDAO.selectOne(params);
    }
}

这不是完美的解决方案,但它可以工作

this is not the perfect solution but it work

  • 情况1:从子类方法调用父类方法不起作用
  • 情况2:制作Super类实例并从实例中调用

推荐答案

首先,我希望您的DAO类是实际的Spring组件,否则Spring AOP找不到它们,只有AspectJ可以找到.

First of all, I hope that your DAO classes are actual Spring components, otherwise Spring AOP does not find them, only AspectJ could.

但从本质上讲,这不是Spring或AspectJ问题.在Java中,

But at the core of it this is not a Spring or AspectJ problem. In Java, annotations on

  • 界面
  • 其他注释或
  • 方法

从不继承

  • 实现类,
  • 使用带注释的注释的类或
  • 覆盖方法.

注释继承仅从类到子类有效,但仅当超类中使用的注释类型带有元注释@Inherited时,请参见

Annotation inheritance only works from classes onto subclasses, but only if the annotation type used in the superclass bears the meta annotation @Inherited, see JDK JavaDoc.

因为我之前已经回答过几次这个问题,所以我已经记录了问题,并且还在

Because I have answered this question several times before, I have just documented the problem and also a workaround in Emulate annotation inheritance for interfaces and methods with AspectJ.

更新:抱歉,我只是更彻底地检查了您的代码.您没有覆盖超类的带注释的方法(至少您的代码没有显示您覆盖了方法selectOne),因此我上面描述的内容不适用于此.您的方面应该起作用.但是也许您在完全限定的类名@com.test.project.annotaion.CustomAnnotation中仅包含一个错字:annotaion(请注意错字!)程序包名称可能应该是annotation.正如我在第一句话中所说:DAO必须是Spring @Component s.

Update: Sorry, I just inspected your code more thoroughly. You are not overriding the superclass's annotated method (at least your code does not show that you override method selectOne), so what I described above does not apply here. Your aspect should work. But maybe you just have a typo in the fully qualified class name @com.test.project.annotaion.CustomAnnotation: The annotaion (note the typo!) package name should probably rather be annotation. And as I said in my first sentence: The DAOs must be Spring @Components.

顺便说一句:您可以完全避免切入点的这一部分,因为您已经将注释绑定到参数了.只需使用

And by the way: You can avoid that part of the pointcut altogether because you already bind the annotation to a parameter. Just use

@Around("execution(* *(..)) && @annotation(customAnnotation)")

这篇关于在Super类中具有自定义注释的AOP不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆