【问题标题】:Can't find classes annotated with ConditionalOnWebApplication using spring reflection使用弹簧反射找不到使用 ConditionalOnWebApplication 注释的类
【发布时间】:2016-12-16 18:14:46
【问题描述】:

在我们的 WebApplication 类中,我想找到所有用@Configuration 注释的类。 我正在使用下面的代码来做到这一点:

ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(Configuration.class));

Set<BeanDefinition> configurationBeans = scanner.findCandidateComponents("com.company");

但是findCandidateComponents 不会返回同时使用@Configuration@ConditionalOnWebApplication 注释的类。

我将过滤器更改为以下,只是为了测试:

scanner.addIncludeFilter(new `AnnotationTypeFilter(ConditionalOnWebApplication.class));`

我没有上课。

如何解决?

【问题讨论】:

    标签: java spring reflection annotations


    【解决方案1】:

    ClassPathScanningCandidateComponentProvider 使用 org.springframework.context.annotation.ConditionEvaluator 可能会排除缺少的类,导致 @ConditionalOnWebApplication 不满足。

    在 org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider#isConditionMatch 处设置断点,看看 conditionEvaluator 在那里做了什么。

    从你的问题我看不出你想用这个做什么。如果你想看看,哪些@Configuration注解的类在运行的spring上下文中,你最好使用ListableBeanfactory.getBeansWithAnnotation

    这为您提供了正在运行的应用程序的确切注释 bean,而您当前的方法会进行自己的类路径扫描,这可能会导致不同的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多