@@ -202,25 +202,19 @@ private ConditionOutcome evaluateConditionalOnMissingBean(Spec<ConditionalOnMiss
202
202
}
203
203
204
204
protected final MatchResult getMatchingBeans (Spec <?> spec ) {
205
+ ConfigurableListableBeanFactory beanFactory = getSearchBeanFactory (spec );
205
206
ClassLoader classLoader = spec .getContext ().getClassLoader ();
206
- ConfigurableListableBeanFactory beanFactory = spec .getContext ().getBeanFactory ();
207
207
boolean considerHierarchy = spec .getStrategy () != SearchStrategy .CURRENT ;
208
208
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
- }
215
209
MatchResult result = new MatchResult ();
216
210
Set <String > beansIgnoredByType = getNamesOfBeansIgnoredByType (classLoader , beanFactory , considerHierarchy ,
217
211
spec .getIgnoredTypes (), parameterizedContainers );
218
212
for (String type : spec .getTypes ()) {
219
213
Map <String , BeanDefinition > typeMatchedDefinitions = getBeanDefinitionsForType (classLoader ,
220
214
considerHierarchy , beanFactory , type , parameterizedContainers );
221
215
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 ));
224
218
if (typeMatchedNames .isEmpty ()) {
225
219
result .recordUnmatchedType (type );
226
220
}
@@ -232,7 +226,7 @@ protected final MatchResult getMatchingBeans(Spec<?> spec) {
232
226
Map <String , BeanDefinition > annotationMatchedDefinitions = getBeanDefinitionsForAnnotation (classLoader ,
233
227
beanFactory , annotation , considerHierarchy );
234
228
Set <String > annotationMatchedNames = matchedNamesFrom (annotationMatchedDefinitions ,
235
- (name , definition ) -> isCandidate (name , definition , beansIgnoredByType ));
229
+ (name , definition ) -> isCandidate (beanFactory , name , definition , beansIgnoredByType ));
236
230
if (annotationMatchedNames .isEmpty ()) {
237
231
result .recordUnmatchedAnnotation (annotation );
238
232
}
@@ -252,6 +246,17 @@ protected final MatchResult getMatchingBeans(Spec<?> spec) {
252
246
return result ;
253
247
}
254
248
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
+
255
260
private Set <String > matchedNamesFrom (Map <String , BeanDefinition > namedDefinitions ,
256
261
BiPredicate <String , BeanDefinition > filter ) {
257
262
Set <String > matchedNames = new LinkedHashSet <>(namedDefinitions .size ());
@@ -263,9 +268,25 @@ private Set<String> matchedNamesFrom(Map<String, BeanDefinition> namedDefinition
263
268
return matchedNames ;
264
269
}
265
270
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
+ }
269
290
}
270
291
271
292
private boolean isDefaultCandidate (BeanDefinition definition ) {
0 commit comments