【问题标题】:Custom annotation is not working on spring Beans自定义注释不适用于spring Beans
【发布时间】:2018-02-06 11:07:47
【问题描述】:

我创建了新的自定义注释@MyCustomAnnotation

@Target({ElementType.METHOD, ElementType.TYPE, ElementType.FIELD})
@Retention(RUNTIME)
public @interface MyCustomAnnotation{
}

我在组件和 bean 上应用了该注释。这是代码,

@MyCustomAnnotation
@Component
public class CoreBussinessLogicHandler implements GenericHandler<BussinessFile> {
//some bussiness logic
}

@Configuration
public class BussinessConfig {

    @Autowired
    private CoreIntegrationComponent coreIntegrationComponent;

    @MyCustomAnnotation
    @Bean(name = INCOMING_PROCESS_CHANNEL)
    public MessageChannel incomingProcessChannel() {
        return coreIntegrationComponent.amqpChannel(INCOMING_PROCESS_CHANNEL);
    }

    //some other configurations
}

现在我想要使用@MyCustomAnnotation 注释的所有bean。所以这里是代码,

import org.springframework.context.ApplicationContext;

@Configuration
public class ChannelConfig {

      @Autowired
      private ApplicationContext applicationContext;

      public List<MessageChannel> getRecipientChannel(CoreIntegrationComponent coreIntegrationComponent) {

      String[] beanNamesForAnnotation = applicationContext.getBeanNamesForAnnotation(MyCustomAnnotation.class);
      //Here in output I am getting bean name for CoreBussinessLogicHandler Only.

    }
}

我的问题是为什么我没有得到名称为 'INCOMING_PROCESS_CHANNEL' 的 Bean,因为它有 @MyCustomAnnotation ?如果我想获得名称为“INCOMING_PROCESS_CHANNEL”的 bean,我应该做哪些代码更改?

【问题讨论】:

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


    【解决方案1】:

    发生这种情况是因为您的 bean 没有收到此注释,因为您已将其放置在“bean 定义配置”中。所以它是可用的,但只能通过 BeanFactory 和 beanDefinitions。您可以将注释放在您的 bean 类上,也可以编写一个自定义方法来使用 bean 工厂进行搜索。

    请参阅accepted answer here

    【讨论】:

      【解决方案2】:

      你必须使用 AnnotationConfigApplicationContext 而不是 ApplicationContext

      这是一个工作示例:

      @SpringBootApplication
      @Configuration
      public class DemoApplication {
      
          @Autowired
          public AnnotationConfigApplicationContext ctx;
      
          @Bean
          public CommandLineRunner CommandLineRunner() {
              return (args) -> {
                  Stream.of(ctx.getBeanNamesForAnnotation(MyCustomAnnotation.class))
                          .map(data -> "Found Bean with name : " + data)
                          .forEach(System.out::println);
              };
          }
      
          public static void main(String[] args) {
              SpringApplication.run(DemoApplication.class, args);
          }
      }
      
      @Target({ElementType.METHOD, ElementType.TYPE, ElementType.FIELD})
      @Retention(RetentionPolicy.RUNTIME)
      @interface MyCustomAnnotation{
      }
      
      @MyCustomAnnotation
      @Component
      class Mycomponent {
          public String str = "MyComponentString";
      }
      

      【讨论】:

      • 嘿,照你说的做了。应用程序无法启动:de.iis.databaseHub.DatabaseHubApplication 中的字段 ctx 需要找不到类型为“org.springframework.context.annotation.AnnotationConfigApplicationContext”的 bean。注入点有以下注解: - @org.springframework.beans.factory.annotation.Autowired(required=true) 行动:考虑在你的配置中定义一个 'org.springframework.context.annotation.AnnotationConfigApplicationContext' 类型的 bean。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      • 2012-01-03
      • 1970-01-01
      • 2021-11-15
      • 2014-08-31
      • 2016-05-01
      • 2015-09-25
      相关资源
      最近更新 更多