How to declare a Spring bean autowire-candidate="false" when using annotations?

Nazaret K. picture Nazaret K. · Jan 21, 2015 · Viewed 8.4k times · Source

I am using @ComponentScan and @Component to define my spring beans. What I would like is to declare one of these beans to be autowire-candidate=false.

This could be done with this attribute in xml. Isn't there the equivalent in annotations?

The reason I want this is because I have 2 implementations of the same interface and I don't want to use @Qualifier.

EDIT: Using @Primary is a valid work-around, but autowire-candidate seems to me like a useful feature with its own semantics.

Thanks

Answer

Sergey Bespalov picture Sergey Bespalov · Oct 13, 2016

Looks like Spring refused autowire-candidate=false concept and it no longer supported. There is no analogue with annotations, so @Primary is the best work-around as you noticed.

Another way is to use custom org.springframework.beans.factory.support.AutowireCandidateResolver, which is used in DefaultListableBeanFactory, with logic that exclude undesirable beans from autowire candidates. In such case, the technology will be similar to that used for autowire-candidate=false in SimpleAutowireCandidateResolver.