@@ -202,25 +202,19 @@ private ConditionOutcome evaluateConditionalOnMissingBean(Spec<ConditionalOnMiss
202202 }
203203
204204 protected final MatchResult getMatchingBeans (Spec <?> spec ) {
205+ ConfigurableListableBeanFactory beanFactory = getSearchBeanFactory (spec );
205206 ClassLoader classLoader = spec .getContext ().getClassLoader ();
206- ConfigurableListableBeanFactory beanFactory = spec .getContext ().getBeanFactory ();
207207 boolean considerHierarchy = spec .getStrategy () != SearchStrategy .CURRENT ;
208208 Set <Class <?>> parameterizedContainers = spec .getParameterizedContainers ();
209- if (spec .getStrategy () == SearchStrategy .ANCESTORS ) {
210- BeanFactory parent = beanFactory .getParentBeanFactory ();
211- Assert .isInstanceOf (ConfigurableListableBeanFactory .class , parent ,
212- "Unable to use SearchStrategy.ANCESTORS" );
213- beanFactory = (ConfigurableListableBeanFactory ) parent ;
214- }
215209 MatchResult result = new MatchResult ();
216210 Set <String > beansIgnoredByType = getNamesOfBeansIgnoredByType (classLoader , beanFactory , considerHierarchy ,
217211 spec .getIgnoredTypes (), parameterizedContainers );
218212 for (String type : spec .getTypes ()) {
219213 Map <String , BeanDefinition > typeMatchedDefinitions = getBeanDefinitionsForType (classLoader ,
220214 considerHierarchy , beanFactory , type , parameterizedContainers );
221215 Set <String > typeMatchedNames = matchedNamesFrom (typeMatchedDefinitions ,
222- (name , definition ) -> isCandidate (name , definition , beansIgnoredByType )
223- && ! ScopedProxyUtils . isScopedTarget ( name ));
216+ (name , definition ) -> ! ScopedProxyUtils . isScopedTarget (name )
217+ && isCandidate ( beanFactory , name , definition , beansIgnoredByType ));
224218 if (typeMatchedNames .isEmpty ()) {
225219 result .recordUnmatchedType (type );
226220 }
@@ -232,7 +226,7 @@ protected final MatchResult getMatchingBeans(Spec<?> spec) {
232226 Map <String , BeanDefinition > annotationMatchedDefinitions = getBeanDefinitionsForAnnotation (classLoader ,
233227 beanFactory , annotation , considerHierarchy );
234228 Set <String > annotationMatchedNames = matchedNamesFrom (annotationMatchedDefinitions ,
235- (name , definition ) -> isCandidate (name , definition , beansIgnoredByType ));
229+ (name , definition ) -> isCandidate (beanFactory , name , definition , beansIgnoredByType ));
236230 if (annotationMatchedNames .isEmpty ()) {
237231 result .recordUnmatchedAnnotation (annotation );
238232 }
@@ -252,6 +246,17 @@ protected final MatchResult getMatchingBeans(Spec<?> spec) {
252246 return result ;
253247 }
254248
249+ private ConfigurableListableBeanFactory getSearchBeanFactory (Spec <?> spec ) {
250+ ConfigurableListableBeanFactory beanFactory = spec .getContext ().getBeanFactory ();
251+ if (spec .getStrategy () == SearchStrategy .ANCESTORS ) {
252+ BeanFactory parent = beanFactory .getParentBeanFactory ();
253+ Assert .isInstanceOf (ConfigurableListableBeanFactory .class , parent ,
254+ "Unable to use SearchStrategy.ANCESTORS" );
255+ beanFactory = (ConfigurableListableBeanFactory ) parent ;
256+ }
257+ return beanFactory ;
258+ }
259+
255260 private Set <String > matchedNamesFrom (Map <String , BeanDefinition > namedDefinitions ,
256261 BiPredicate <String , BeanDefinition > filter ) {
257262 Set <String > matchedNames = new LinkedHashSet <>(namedDefinitions .size ());
@@ -263,9 +268,25 @@ private Set<String> matchedNamesFrom(Map<String, BeanDefinition> namedDefinition
263268 return matchedNames ;
264269 }
265270
266- private boolean isCandidate (String name , BeanDefinition definition , Set <String > ignoredBeans ) {
267- return (!ignoredBeans .contains (name ))
268- && (definition == null || (definition .isAutowireCandidate () && isDefaultCandidate (definition )));
271+ private boolean isCandidate (ConfigurableListableBeanFactory beanFactory , String name , BeanDefinition definition ,
272+ Set <String > ignoredBeans ) {
273+ return (!ignoredBeans .contains (name )) && (definition == null
274+ || isAutowireCandidate (beanFactory , name , definition ) && isDefaultCandidate (definition ));
275+ }
276+
277+ private boolean isAutowireCandidate (ConfigurableListableBeanFactory beanFactory , String name ,
278+ BeanDefinition definition ) {
279+ return definition .isAutowireCandidate () || isScopeTargetAutowireCandidate (beanFactory , name );
280+ }
281+
282+ private boolean isScopeTargetAutowireCandidate (ConfigurableListableBeanFactory beanFactory , String name ) {
283+ try {
284+ return ScopedProxyUtils .isScopedTarget (name )
285+ && beanFactory .getBeanDefinition (ScopedProxyUtils .getOriginalBeanName (name )).isAutowireCandidate ();
286+ }
287+ catch (NoSuchBeanDefinitionException ex ) {
288+ return false ;
289+ }
269290 }
270291
271292 private boolean isDefaultCandidate (BeanDefinition definition ) {
0 commit comments