

Spring - Autowiring multiple beans of the same type and @Primary annotation
source link: http://www.java-allandsundry.com/2013/08/spring-autowiring-multiple-beans-of.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Spring - Autowiring multiple beans of the same type and @Primary annotation
Consider a simple Spring annotation based context, with two beans with @Service annotation, but of the same type:
@Service
public
class
CustomerServiceImpl1
implements
CustomerService{
@Override
public
Customer getCustomer(
long
id) {
return
new
Customer(
1
,
"Test1"
);
}
}
@Service
public
class
CustomerServiceImpl2
implements
CustomerService{
@Override
public
Customer getCustomer(
long
id) {
return
new
Customer(
1
,
"Test1"
);
}
}
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [pkg.CustomerService] is defined: expected single matching bean but found
2
: customerServiceImpl2,customerServiceImpl1
public
class
ConfigTest
{
@Autowired
@Qualifier
(
"customerServiceImpl1"
)
private
CustomerService customerService;
...
}
However there is another way to disambiguate which specific bean to inject in, and this is with the @Primary annotation. This annotation indicates that the target bean should be given precedence when autowiring by type. This begs the question, what happens if more than one bean of the same type is annotated with @Primary, well Spring will raise an exception like before. @Primary is expected to be applied on only bean of a specific type.
@Primary is also supported in Java Configuration, so @Bean methods can also be tagged with @Primary to indicate the higher precedence.
@Configuration
public
class
TestConfiguration
{
@Bean
@Primary
public
CustomerService customerService1() {
return
new
CustomerServiceImpl1();
}
@Bean
public
CustomerService customerService2() {
return
new
CustomerServiceImpl2();
}
}
Recommend
-
10
Spring beans with same name and @Configuration One of the important features when testing an application is being able to replace some of the real services with test doubles. With a Spring based application, this has typically be...
-
17
java-version.com: What's new in Java 16? 15? Keep up to date! Basic Spring MVC example with Thymeleaf and no XML (annotation based conf...
-
7
With profiles, Spring (Boot) provides a very powerful feature to configure our applications. Spring also offers the @Profile annotation to add beans to the application context only when a certain profile is active. This article i...
-
13
Testing with Spring Boot's @TestConfiguration AnnotationA unit test is used to verify the smallest part of an application (a “unit”) independent of other parts. This makes the verification process easy and fast since the scope of the testing...
-
6
“The primary use case of AIOps is to ingest multiple monitoring data feeds” June 10, 2021JAXenter Editorial Team...
-
7
Reading Time: 2 minutes Spring IoC container is the core of the Spring Framework.In spring-based applications, objects live inside a spring containerThe container instant...
-
13
Join 2 primary key columns to 1 foreign key column in the same table advertisements I have a jobs...
-
4
Spring: Make an Externally Created Object Available to Beans in applicationContext.xml October 11, 2011 If your Spring beans...
-
10
关于Spring Beans 的理解 精选 原创 Spring Beans 1.什 么 是 Spring beans? S...
-
0
Autowiring in Spring FrameworkSkip to content
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK