1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
28
28
import org .springframework .beans .factory .annotation .Value ;
29
29
import org .springframework .beans .factory .config .AutowireCapableBeanFactory ;
30
30
import org .springframework .beans .factory .config .DependencyDescriptor ;
31
- import org .springframework .context .ApplicationContext ;
32
31
import org .springframework .core .MethodParameter ;
33
32
import org .springframework .core .annotation .AnnotatedElementUtils ;
34
33
import org .springframework .core .annotation .SynthesizingMethodParameter ;
@@ -63,20 +62,19 @@ public Annotation[] getDeclaredAnnotations() {
63
62
64
63
65
64
/**
66
- * Determine if the supplied {@link Parameter} can potentially be
67
- * autowired from an {@link ApplicationContext}.
68
- * <p>Returns {@code true} if the supplied parameter is of type
69
- * {@link ApplicationContext} (or a sub-type thereof) or is annotated or
65
+ * Determine if the supplied {@link Parameter} can <em>potentially</em> be
66
+ * autowired from an {@link AutowireCapableBeanFactory}.
67
+ * <p>Returns {@code true} if the supplied parameter is annotated or
70
68
* meta-annotated with {@link Autowired @Autowired},
71
69
* {@link Qualifier @Qualifier}, or {@link Value @Value}.
70
+ * <p>Note that {@link #resolveDependency} may still be able to resolve the
71
+ * dependency for the supplied parameter even if this method returns {@code false}.
72
72
* @param parameter the parameter whose dependency should be autowired
73
- * @param parameterIndex the index of the parameter
73
+ * @param parameterIndex the index of the parameter in the constructor or method
74
+ * that declares the parameter
74
75
* @see #resolveDependency
75
76
*/
76
77
static boolean isAutowirable (Parameter parameter , int parameterIndex ) {
77
- if (ApplicationContext .class .isAssignableFrom (parameter .getType ())) {
78
- return true ;
79
- }
80
78
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter (parameter , parameterIndex );
81
79
return (AnnotatedElementUtils .hasAnnotation (annotatedParameter , Autowired .class ) ||
82
80
AnnotatedElementUtils .hasAnnotation (annotatedParameter , Qualifier .class ) ||
@@ -85,34 +83,35 @@ static boolean isAutowirable(Parameter parameter, int parameterIndex) {
85
83
86
84
/**
87
85
* Resolve the dependency for the supplied {@link Parameter} from the
88
- * supplied {@link ApplicationContext }.
86
+ * supplied {@link AutowireCapableBeanFactory }.
89
87
* <p>Provides comprehensive autowiring support for individual method parameters
90
88
* on par with Spring's dependency injection facilities for autowired fields and
91
89
* methods, including support for {@link Autowired @Autowired},
92
90
* {@link Qualifier @Qualifier}, and {@link Value @Value} with support for property
93
91
* placeholders and SpEL expressions in {@code @Value} declarations.
94
- * <p>The dependency is required unless the parameter is annotated with
95
- * {@link Autowired @Autowired} with the {@link Autowired#required required}
92
+ * <p>The dependency is required unless the parameter is annotated or meta-annotated
93
+ * with {@link Autowired @Autowired} with the {@link Autowired#required required}
96
94
* flag set to {@code false}.
97
95
* <p>If an explicit <em>qualifier</em> is not declared, the name of the parameter
98
96
* will be used as the qualifier for resolving ambiguities.
99
97
* @param parameter the parameter whose dependency should be resolved
100
- * @param parameterIndex the index of the parameter
98
+ * @param parameterIndex the index of the parameter in the constructor or method
99
+ * that declares the parameter
101
100
* @param containingClass the concrete class that contains the parameter; this may
102
101
* differ from the class that declares the parameter in that it may be a subclass
103
102
* thereof, potentially substituting type variables
104
- * @param applicationContext the application context from which to resolve the
105
- * dependency
103
+ * @param beanFactory the {@code AutowireCapableBeanFactory} from which to resolve
104
+ * the dependency
106
105
* @return the resolved object, or {@code null} if none found
107
106
* @throws BeansException if dependency resolution failed
108
107
* @see #isAutowirable
109
108
* @see Autowired#required
110
- * @see SynthesizingMethodParameter#forParameter(Parameter )
109
+ * @see SynthesizingMethodParameter#forExecutable(Executable, int )
111
110
* @see AutowireCapableBeanFactory#resolveDependency(DependencyDescriptor, String)
112
111
*/
113
112
@ Nullable
114
113
static Object resolveDependency (
115
- Parameter parameter , int parameterIndex , Class <?> containingClass , ApplicationContext applicationContext ) {
114
+ Parameter parameter , int parameterIndex , Class <?> containingClass , AutowireCapableBeanFactory beanFactory ) {
116
115
117
116
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter (parameter , parameterIndex );
118
117
Autowired autowired = AnnotatedElementUtils .findMergedAnnotation (annotatedParameter , Autowired .class );
@@ -122,7 +121,7 @@ static Object resolveDependency(
122
121
parameter .getDeclaringExecutable (), parameterIndex );
123
122
DependencyDescriptor descriptor = new DependencyDescriptor (methodParameter , required );
124
123
descriptor .setContainingClass (containingClass );
125
- return applicationContext . getAutowireCapableBeanFactory () .resolveDependency (descriptor , null );
124
+ return beanFactory .resolveDependency (descriptor , null );
126
125
}
127
126
128
127
/**
0 commit comments