当我引入AOP时,Spring Class.getAnnotationsByType无法正常工作
我有一个带有自定义注释的Spring Bean当我引入AOP时,Spring Class.getAnnotationsByType无法正常工作,spring,aop,Spring,Aop,我有一个带有自定义注释的Spring Bean @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface CliCommand { Slice slice(); } 在运行时(启动)期间,我希望加载具有此注释的所有类 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface CliCo
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CliCommand {
Slice slice();
}
在运行时(启动)期间,我希望加载具有此注释的所有类
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CliCommand {
Slice slice();
}
在这个过程中,我打电话
CliCommand[] cliAnnotations = myclass.getAnnotationsByType(CliCommand.class);
在我介绍AOP之前,它工作得很好
但是现在它返回NULL
我的AOP看起来像这样
@Pointcut("within(com.companyx.cli..*)")
public void cliLayer() {
}
@Before("cliLayer()")
public void injectCLI(JoinPoint jp){
MDC.put(ConnectApplicationContext.LOG_LAYER_NAME, Layer.CLI);
}
我不知道为什么会这样。以前有人见过这个吗。这与cgLib有关吗?cgLib在创建代理时生成类的子级。因此,如果示例中的“myclass”是通过
someCglibObject.getClass()
获得的,那么您将获得cglib子类,而不是您的子类
在这种情况下,要使父类上声明的批注通过
childClass.getAnnotationsByType可用,您应该在创建代理时使用@Inherited
为自定义批注添加批注。因此,如果示例中的“myclass”是通过someCglibObject.getClass()
获得的,那么您将获得cglib子类,而不是您的子类
在这种情况下,要使父类上声明的批注通过childClass.getAnnotationsByType可用,您应该使用@Inherited
对自定义批注进行批注。我注意到了一些事情。非常感谢。我开始使用类构造来检查类,结果一切顺利。我通过调用classcliprototypeclass=applicationContext.getType(beanName)实现了这一点
而不是Map beansOfType=applicationContext.getBeansOfType(CliCommand.class)代码>同意,尽管你的答案是正确的。谢谢,非常感谢。我开始使用类构造来检查类,结果一切顺利。我通过调用classcliprototypeclass=applicationContext.getType(beanName)实现了这一点
而不是Map beansOfType=applicationContext.getBeansOfType(CliCommand.class)代码>同意,尽管你的答案是正确的。谢谢