简体   繁体   English

@Conditional对继承的类不起作用?

[英]@Conditional not working on inherited classes?

I have a simple class annotated with @Conditional(MyCondition.class) 我有一个用@Conditional(MyCondition.class)注释的简单类

eg: 例如:

@Conditional(MyCondition.class)
public class MyBean {
  ...
}

Then I have a configuration bean which extends this bean: 然后,我有一个配置bean扩展了这个bean:

@Configuration
public class MyConfig extends MyBean {
  ...
}

It seems that the @Conditional is being ignored and the @Configuration is always processed no matter how the condition would resolve (it's not even called) 似乎@Conditional被忽略,并且@Configuration总是被处理,无论条件如何解决(甚至都没有调用)

As soon as I move the @Conditional annotation to the MyConfig everything works as expected. 一旦将@Conditional批注移动到MyConfig一切就会按预期进行。

From the doc: 从文档:

The @Conditional annotation may be used in any of the following ways: @Conditional批注可以通过以下任何一种方式使用:

  • as a type-level annotation on any class directly or indirectly annotated with @Component, including @Configuration classes 作为任何直接或间接用@Component注释的类的类型级别注释,包括@Configuration类
  • as a meta-annotation, for the purpose of composing custom stereotype annotations 作为元注释,目的是组成自定义构造型注释
  • as a method-level annotation on any @Bean method 作为任何@Bean方法上的方法级注释

I would assume the first point applies here, ie MyConfig is indirectly annotated with @Configuration . 我认为这里适用第一点,即MyConfig间接用@Configuration注释。 Or does the indirectly refer to other annotations which are annotated with @Conditional ? 还是间接引用了@Conditional注释的其他注释?

Thanks for the insight 感谢您的见解

According to the javadoc of @Conditional 根据@Conditionaljavadoc

NOTE: @Conditional annotations are not inherited; 注意:@Conditional注释不会被继承; any conditions from superclasses or from overridden methods are not being considered. 没有考虑来自超类或重写方法的任何条件。

The @Conditional annotation isn't @Inherited and as such it isn't visible on sub classes. @Conditional注释不是@Inherited ,因此在子类上不可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM