4

I have created my new custom annotation @MyCustomAnnotation

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

I applied that annotation on component and bean. Here is the code,

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

And

@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
}

Now i want all the beans annotated with @MyCustomAnnotation. So here is the code,

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.

    }
}

My question is why I am not getting Bean with name 'INCOMING_PROCESS_CHANNEL' as it has @MyCustomAnnotation ? If I want to get bean with name 'INCOMING_PROCESS_CHANNEL' what code changes should I do ?

Rohan Mulay
  • 349
  • 3
  • 12

2 Answers2

1

This happens because your bean does not receive this annotation as you have placed it on a "bean definition configuration". So it is available but only through BeanFactory and beanDefinitions. You can either put the annotation on your bean class or write a custom method that would do the searching using bean factory.

See the accepted answer here.

Rafal G.
  • 4,252
  • 1
  • 25
  • 41
1

You have to use AnnotationConfigApplicationContext instead of ApplicationContext

Here is a working example:

@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";
}
Avinash
  • 4,115
  • 2
  • 22
  • 41
  • Hey there did what you said. Application won't start: Field ctx in de.iis.databaseHub.DatabaseHubApplication required a bean of type 'org.springframework.context.annotation.AnnotationConfigApplicationContext' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'org.springframework.context.annotation.AnnotationConfigApplicationContext' in your configuration. – CptDayDreamer Jan 09 '20 at 15:41