Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 324
Nazaret K. Avatar asked Jan 21 '15 11:01

Nazaret K.


1 Answers

Since Spring 5.1 , you can configure autowire-candidate in @Bean through autowireCandidate attribute:

@Bean(autowireCandidate = false)
public FooBean foo() {
      return newFooBean();
}
like image 144
Ken Chan Avatar answered Oct 19 '22 05:10

Ken Chan