@@ -4280,14 +4280,89 @@ any). These types must be 'wired up' explicitly via XML or using a Spring `@Bean
42804280==== 
42814281
42824282
4283+ [[beans-autowired-annotation-primary]]
4284+ === Fine-tuning annotation-based autowiring with primary
4285+ Because autowiring by type may lead to multiple candidates, it is often necessary to
4286+ have more control over the selection process. One way to accomplish this is with
4287+ Spring's `@Primary` annotation. `@Primary` indicates that a bean should be given
4288+ preference when multiple candidates are qualified to autowire a single-valued dependency.
4289+ If exactly one 'primary' bean exists among the candidates, it will be the autowired
4290+ value.
4291+ 
4292+ Let's assume the following configuration that define `firstMovieCatalog` as the _primary_
4293+ `MovieCatalog`
4294+ 
4295+ [source,java,indent=0]
4296+ [subs="verbatim,quotes"]
4297+ ---- 
4298+ 	@Configuration 
4299+ 	public class MovieConfiguration { 
4300+ 
4301+ 		@Bean 
4302+ 		**@Primary** 
4303+ 		public MovieCatalog firstMovieCatalog() { ... } 
4304+ 
4305+ 		@Bean 
4306+ 		public MovieCatalog secondMovieCatalog() { ... } 
4307+ 
4308+ 		// ... 
4309+ 
4310+ 	} 
4311+ ---- 
4312+ 
4313+ With such configuration, `MovieRecommender` will use `firstMovieCatalog`
4314+ 
4315+ [source,java,indent=0]
4316+ [subs="verbatim,quotes"]
4317+ ---- 
4318+ 	public class MovieRecommender { 
4319+ 
4320+ 		@Autowired 
4321+ 		private MovieCatalog movieCatalog; 
4322+ 
4323+ 		// ... 
4324+ 
4325+ 	} 
4326+ ---- 
4327+ 
4328+ 
4329+ The corresponding bean definitions appear as follows.
4330+ 
4331+ [source,xml,indent=0]
4332+ [subs="verbatim,quotes"]
4333+ ---- 
4334+ 	<?xml version="1.0" encoding="UTF-8"?> 
4335+ 	<beans xmlns="http://www.springframework.org/schema/beans" 
4336+ 		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
4337+ 		xmlns:context="http://www.springframework.org/schema/context" 
4338+ 		xsi:schemaLocation="http://www.springframework.org/schema/beans 
4339+ 			http://www.springframework.org/schema/beans/spring-beans.xsd 
4340+ 			http://www.springframework.org/schema/context 
4341+ 			http://www.springframework.org/schema/context/spring-context.xsd"> 
4342+ 
4343+ 		<context:annotation-config/> 
4344+ 
4345+ 		<bean class="example.SimpleMovieCatalog" **primary=true**> 
4346+ 			<!-- inject any dependencies required by this bean --> 
4347+ 		</bean> 
4348+ 
4349+ 		<bean class="example.SimpleMovieCatalog"> 
4350+ 			<!-- inject any dependencies required by this bean --> 
4351+ 		</bean> 
4352+ 
4353+ 		<bean id="movieRecommender" class="example.MovieRecommender"/> 
4354+ 
4355+ 	</beans> 
4356+ ---- 
4357+ 
42834358
42844359[[beans-autowired-annotation-qualifiers]]
42854360=== Fine-tuning annotation-based autowiring with qualifiers
4286- Because autowiring by type may lead  to multiple candidates, it is often necessary to 
4287- have  more control over the selection process. One way to accomplish this is with 
4288- Spring's `@Qualifier` annotation. You can associate qualifier values with specific 
4289- arguments, narrowing the set of type matches so that a specific bean is chosen for each 
4290- argument. In the simplest case, this can be a plain descriptive value:
4361+ `@Primary` is an effective way  to use autowiring by type with several instances when one 
4362+ primary candidate can be determined. When  more control over the selection process is 
4363+ required,  Spring's `@Qualifier` annotation can be used . You can associate qualifier values
4364+ with specific  arguments, narrowing the set of type matches so that a specific bean is
4365+ chosen for each  argument. In the simplest case, this can be a plain descriptive value:
42914366
42924367[source,java,indent=0]
42934368[subs="verbatim,quotes"]
0 commit comments