26
26
import org .springframework .beans .TypeConverter ;
27
27
import org .springframework .beans .factory .BeanFactory ;
28
28
import org .springframework .beans .factory .BeanFactoryAware ;
29
+ import org .springframework .beans .factory .config .BeanDefinition ;
29
30
import org .springframework .beans .factory .config .BeanDefinitionHolder ;
31
+ import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
30
32
import org .springframework .beans .factory .config .DependencyDescriptor ;
31
33
import org .springframework .beans .factory .support .AutowireCandidateQualifier ;
32
34
import org .springframework .beans .factory .support .AutowireCandidateResolver ;
@@ -227,18 +229,22 @@ protected boolean checkQualifier(
227
229
228
230
Class <? extends Annotation > type = annotation .annotationType ();
229
231
RootBeanDefinition bd = (RootBeanDefinition ) bdHolder .getBeanDefinition ();
232
+
230
233
AutowireCandidateQualifier qualifier = bd .getQualifier (type .getName ());
231
234
if (qualifier == null ) {
232
235
qualifier = bd .getQualifier (ClassUtils .getShortName (type ));
233
236
}
234
237
if (qualifier == null ) {
235
- Annotation targetAnnotation = null ;
236
- Method resolvedFactoryMethod = bd .getResolvedFactoryMethod ();
237
- if (resolvedFactoryMethod != null ) {
238
- targetAnnotation = AnnotationUtils .getAnnotation (resolvedFactoryMethod , type );
238
+ // First, check annotation on factory method, if applicable
239
+ Annotation targetAnnotation = getFactoryMethodAnnotation (bd , type );
240
+ if (targetAnnotation == null ) {
241
+ RootBeanDefinition dbd = getResolvedDecoratedDefinition (bd );
242
+ if (dbd != null ) {
243
+ targetAnnotation = getFactoryMethodAnnotation (dbd , type );
244
+ }
239
245
}
240
246
if (targetAnnotation == null ) {
241
- // look for matching annotation on the target class
247
+ // Look for matching annotation on the target class
242
248
if (this .beanFactory != null ) {
243
249
Class <?> beanType = this .beanFactory .getType (bdHolder .getBeanName ());
244
250
if (beanType != null ) {
@@ -253,30 +259,31 @@ protected boolean checkQualifier(
253
259
return true ;
254
260
}
255
261
}
262
+
256
263
Map <String , Object > attributes = AnnotationUtils .getAnnotationAttributes (annotation );
257
264
if (attributes .isEmpty () && qualifier == null ) {
258
- // if no attributes, the qualifier must be present
265
+ // If no attributes, the qualifier must be present
259
266
return false ;
260
267
}
261
268
for (Map .Entry <String , Object > entry : attributes .entrySet ()) {
262
269
String attributeName = entry .getKey ();
263
270
Object expectedValue = entry .getValue ();
264
271
Object actualValue = null ;
265
- // check qualifier first
272
+ // Check qualifier first
266
273
if (qualifier != null ) {
267
274
actualValue = qualifier .getAttribute (attributeName );
268
275
}
269
276
if (actualValue == null ) {
270
- // fall back on bean definition attribute
277
+ // Fall back on bean definition attribute
271
278
actualValue = bd .getAttribute (attributeName );
272
279
}
273
280
if (actualValue == null && attributeName .equals (AutowireCandidateQualifier .VALUE_KEY ) &&
274
281
expectedValue instanceof String && bdHolder .matchesName ((String ) expectedValue )) {
275
- // fall back on bean name (or alias) match
282
+ // Fall back on bean name (or alias) match
276
283
continue ;
277
284
}
278
285
if (actualValue == null && qualifier != null ) {
279
- // fall back on default, but only if the qualifier is present
286
+ // Fall back on default, but only if the qualifier is present
280
287
actualValue = AnnotationUtils .getDefaultValue (annotation , attributeName );
281
288
}
282
289
if (actualValue != null ) {
@@ -289,6 +296,25 @@ protected boolean checkQualifier(
289
296
return true ;
290
297
}
291
298
299
+ protected RootBeanDefinition getResolvedDecoratedDefinition (RootBeanDefinition rbd ) {
300
+ BeanDefinitionHolder decDef = rbd .getDecoratedDefinition ();
301
+ if (decDef != null && this .beanFactory instanceof ConfigurableListableBeanFactory ) {
302
+ ConfigurableListableBeanFactory clbf = (ConfigurableListableBeanFactory ) this .beanFactory ;
303
+ if (clbf .containsBeanDefinition (decDef .getBeanName ())) {
304
+ BeanDefinition dbd = clbf .getMergedBeanDefinition (decDef .getBeanName ());
305
+ if (dbd instanceof RootBeanDefinition ) {
306
+ return (RootBeanDefinition ) dbd ;
307
+ }
308
+ }
309
+ }
310
+ return null ;
311
+ }
312
+
313
+ protected Annotation getFactoryMethodAnnotation (RootBeanDefinition bd , Class <? extends Annotation > type ) {
314
+ Method resolvedFactoryMethod = bd .getResolvedFactoryMethod ();
315
+ return (resolvedFactoryMethod != null ? AnnotationUtils .getAnnotation (resolvedFactoryMethod , type ) : null );
316
+ }
317
+
292
318
293
319
/**
294
320
* Determine whether the given dependency carries a value annotation.
0 commit comments