Reject multiple primary candidates in ancestor factory as well
Closes gh-26612
This commit is contained in:
		
							parent
							
								
									ad2e95be4b
								
							
						
					
					
						commit
						889c4e0ff5
					
				|  | @ -1805,7 +1805,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto | |||
| 				if (primaryBeanName != null) { | ||||
| 					boolean candidateLocal = containsBeanDefinition(candidateBeanName); | ||||
| 					boolean primaryLocal = containsBeanDefinition(primaryBeanName); | ||||
| 					if (candidateLocal && primaryLocal) { | ||||
| 					if (candidateLocal == primaryLocal) { | ||||
| 						throw new NoUniqueBeanDefinitionException(requiredType, candidates.size(), | ||||
| 								"more than one 'primary' bean found among candidates: " + candidates.keySet()); | ||||
| 					} | ||||
|  |  | |||
|  | @ -2163,9 +2163,39 @@ class DefaultListableBeanFactoryTests { | |||
| 		lbf.registerBeanDefinition("test", bd); | ||||
| 		lbf.registerBeanDefinition("spouse", bd2); | ||||
| 
 | ||||
| 		assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> | ||||
| 				lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true)) | ||||
| 			.withCauseExactlyInstanceOf(NoUniqueBeanDefinitionException.class); | ||||
| 		assertThatExceptionOfType(UnsatisfiedDependencyException.class) | ||||
| 				.isThrownBy(() -> lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true)) | ||||
| 				.withCauseExactlyInstanceOf(NoUniqueBeanDefinitionException.class); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	void autowireBeanByTypeWithTwoPrimaryCandidatesInOneAncestor() { | ||||
| 		DefaultListableBeanFactory parent = new DefaultListableBeanFactory(); | ||||
| 		RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); | ||||
| 		bd.setPrimary(true); | ||||
| 		RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); | ||||
| 		bd2.setPrimary(true); | ||||
| 		parent.registerBeanDefinition("test", bd); | ||||
| 		parent.registerBeanDefinition("spouse", bd2); | ||||
| 		DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(parent); | ||||
| 
 | ||||
| 		assertThatExceptionOfType(UnsatisfiedDependencyException.class) | ||||
| 				.isThrownBy(() -> lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true)) | ||||
| 				.withCauseExactlyInstanceOf(NoUniqueBeanDefinitionException.class); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	void autowireBeanByTypeWithTwoPrimaryFactoryBeans(){ | ||||
| 		DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); | ||||
| 		RootBeanDefinition bd1 = new RootBeanDefinition(LazyInitFactory.class); | ||||
| 		RootBeanDefinition bd2 = new RootBeanDefinition(LazyInitFactory.class); | ||||
| 		bd1.setPrimary(true); | ||||
| 		bd2.setPrimary(true); | ||||
| 		lbf.registerBeanDefinition("bd1", bd1); | ||||
| 		lbf.registerBeanDefinition("bd2", bd2); | ||||
| 		assertThatExceptionOfType(UnsatisfiedDependencyException.class) | ||||
| 				.isThrownBy(() -> lbf.autowire(FactoryBeanDependentBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true)) | ||||
| 				.withCauseExactlyInstanceOf(NoUniqueBeanDefinitionException.class); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue