|
15 | 15 | */ |
16 | 16 | package org.springframework.security.config.annotation.authentication.configuration; |
17 | 17 |
|
18 | | -import java.util.Arrays; |
19 | | -import java.util.Collections; |
20 | | -import java.util.List; |
21 | | -import java.util.Map; |
22 | | -import java.util.concurrent.atomic.AtomicBoolean; |
23 | | - |
24 | 18 | import org.apache.commons.logging.Log; |
25 | 19 | import org.apache.commons.logging.LogFactory; |
26 | | - |
27 | 20 | import org.springframework.aop.framework.ProxyFactoryBean; |
28 | 21 | import org.springframework.aop.target.LazyInitTargetSource; |
29 | 22 | import org.springframework.beans.factory.BeanFactoryUtils; |
30 | 23 | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
31 | 24 | import org.springframework.beans.factory.annotation.Autowired; |
32 | 25 | import org.springframework.context.ApplicationContext; |
| 26 | +import org.springframework.context.ConfigurableApplicationContext; |
33 | 27 | import org.springframework.context.annotation.Bean; |
34 | 28 | import org.springframework.context.annotation.Configuration; |
35 | 29 | import org.springframework.context.annotation.Import; |
|
49 | 43 | import org.springframework.security.crypto.password.PasswordEncoder; |
50 | 44 | import org.springframework.util.Assert; |
51 | 45 |
|
| 46 | +import java.util.Arrays; |
| 47 | +import java.util.Collections; |
| 48 | +import java.util.List; |
| 49 | +import java.util.Map; |
| 50 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 51 | +import java.util.stream.Collectors; |
| 52 | + |
52 | 53 | /** |
53 | 54 | * Exports the authentication {@link Configuration} |
54 | 55 | * |
@@ -151,10 +152,23 @@ private <T> T lazyBean(Class<T> interfaceName) { |
151 | 152 | if (beanNamesForType.length == 0) { |
152 | 153 | return null; |
153 | 154 | } |
154 | | - Assert.isTrue(beanNamesForType.length == 1, |
155 | | - () -> "Expecting to only find a single bean for type " + interfaceName |
156 | | - + ", but found " + Arrays.asList(beanNamesForType)); |
157 | | - lazyTargetSource.setTargetBeanName(beanNamesForType[0]); |
| 155 | + String beanName; |
| 156 | + if (beanNamesForType.length > 1) { |
| 157 | + List<String> primaryBeanNames = Arrays.stream(beanNamesForType) |
| 158 | + .filter(i -> applicationContext instanceof ConfigurableApplicationContext) |
| 159 | + .filter(n -> ((ConfigurableApplicationContext) applicationContext).getBeanFactory().getBeanDefinition(n).isPrimary()) |
| 160 | + .collect(Collectors.toList()); |
| 161 | + |
| 162 | + Assert.isTrue(primaryBeanNames.size() != 0, () -> "Found " + beanNamesForType.length |
| 163 | + + " beans for type " + interfaceName + ", but none marked as primary"); |
| 164 | + Assert.isTrue(primaryBeanNames.size() == 1, () -> "Found " + primaryBeanNames.size() |
| 165 | + + " beans for type " + interfaceName + " marked as primary"); |
| 166 | + beanName = primaryBeanNames.get(0); |
| 167 | + } else { |
| 168 | + beanName = beanNamesForType[0]; |
| 169 | + } |
| 170 | + |
| 171 | + lazyTargetSource.setTargetBeanName(beanName); |
158 | 172 | lazyTargetSource.setBeanFactory(applicationContext); |
159 | 173 | ProxyFactoryBean proxyFactory = new ProxyFactoryBean(); |
160 | 174 | proxyFactory = objectPostProcessor.postProcess(proxyFactory); |
|
0 commit comments