Skip to content

Commit a7770e7

Browse files
committed
Remove ApplicationContext dependency from ParameterAutowireUtils
This commit is a prerequisite for spring-projectsgh-2060.
1 parent 49bbbb0 commit a7770e7

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

spring-test/src/main/java/org/springframework/test/context/junit/jupiter/ParameterAutowireUtils.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,6 @@
2828
import org.springframework.beans.factory.annotation.Value;
2929
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
3030
import org.springframework.beans.factory.config.DependencyDescriptor;
31-
import org.springframework.context.ApplicationContext;
3231
import org.springframework.core.MethodParameter;
3332
import org.springframework.core.annotation.AnnotatedElementUtils;
3433
import org.springframework.core.annotation.SynthesizingMethodParameter;
@@ -63,20 +62,19 @@ public Annotation[] getDeclaredAnnotations() {
6362

6463

6564
/**
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
7068
* meta-annotated with {@link Autowired @Autowired},
7169
* {@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}.
7272
* @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
7475
* @see #resolveDependency
7576
*/
7677
static boolean isAutowirable(Parameter parameter, int parameterIndex) {
77-
if (ApplicationContext.class.isAssignableFrom(parameter.getType())) {
78-
return true;
79-
}
8078
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
8179
return (AnnotatedElementUtils.hasAnnotation(annotatedParameter, Autowired.class) ||
8280
AnnotatedElementUtils.hasAnnotation(annotatedParameter, Qualifier.class) ||
@@ -85,34 +83,35 @@ static boolean isAutowirable(Parameter parameter, int parameterIndex) {
8583

8684
/**
8785
* Resolve the dependency for the supplied {@link Parameter} from the
88-
* supplied {@link ApplicationContext}.
86+
* supplied {@link AutowireCapableBeanFactory}.
8987
* <p>Provides comprehensive autowiring support for individual method parameters
9088
* on par with Spring's dependency injection facilities for autowired fields and
9189
* methods, including support for {@link Autowired @Autowired},
9290
* {@link Qualifier @Qualifier}, and {@link Value @Value} with support for property
9391
* 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}
9694
* flag set to {@code false}.
9795
* <p>If an explicit <em>qualifier</em> is not declared, the name of the parameter
9896
* will be used as the qualifier for resolving ambiguities.
9997
* @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
101100
* @param containingClass the concrete class that contains the parameter; this may
102101
* differ from the class that declares the parameter in that it may be a subclass
103102
* 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
106105
* @return the resolved object, or {@code null} if none found
107106
* @throws BeansException if dependency resolution failed
108107
* @see #isAutowirable
109108
* @see Autowired#required
110-
* @see SynthesizingMethodParameter#forParameter(Parameter)
109+
* @see SynthesizingMethodParameter#forExecutable(Executable, int)
111110
* @see AutowireCapableBeanFactory#resolveDependency(DependencyDescriptor, String)
112111
*/
113112
@Nullable
114113
static Object resolveDependency(
115-
Parameter parameter, int parameterIndex, Class<?> containingClass, ApplicationContext applicationContext) {
114+
Parameter parameter, int parameterIndex, Class<?> containingClass, AutowireCapableBeanFactory beanFactory) {
116115

117116
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
118117
Autowired autowired = AnnotatedElementUtils.findMergedAnnotation(annotatedParameter, Autowired.class);
@@ -122,7 +121,7 @@ static Object resolveDependency(
122121
parameter.getDeclaringExecutable(), parameterIndex);
123122
DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
124123
descriptor.setContainingClass(containingClass);
125-
return applicationContext.getAutowireCapableBeanFactory().resolveDependency(descriptor, null);
124+
return beanFactory.resolveDependency(descriptor, null);
126125
}
127126

128127
/**

spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -143,8 +143,9 @@ public void afterEach(ExtensionContext context) throws Exception {
143143
* Determine if the value for the {@link Parameter} in the supplied {@link ParameterContext}
144144
* should be autowired from the test's {@link ApplicationContext}.
145145
* <p>Returns {@code true} if the parameter is declared in a {@link Constructor}
146-
* that is annotated with {@link Autowired @Autowired} and otherwise delegates to
147-
* {@link ParameterAutowireUtils#isAutowirable}.
146+
* that is annotated with {@link Autowired @Autowired} or if the parameter is
147+
* of type {@link ApplicationContext} (or a sub-type thereof) and otherwise delegates
148+
* to {@link ParameterAutowireUtils#isAutowirable}.
148149
* <p><strong>WARNING</strong>: If the parameter is declared in a {@code Constructor}
149150
* that is annotated with {@code @Autowired}, Spring will assume the responsibility
150151
* for resolving all parameters in the constructor. Consequently, no other registered
@@ -159,6 +160,7 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon
159160
Executable executable = parameter.getDeclaringExecutable();
160161
return (executable instanceof Constructor &&
161162
AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) ||
163+
ApplicationContext.class.isAssignableFrom(parameter.getType()) ||
162164
ParameterAutowireUtils.isAutowirable(parameter, index);
163165
}
164166

@@ -176,7 +178,8 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
176178
int index = parameterContext.getIndex();
177179
Class<?> testClass = extensionContext.getRequiredTestClass();
178180
ApplicationContext applicationContext = getApplicationContext(extensionContext);
179-
return ParameterAutowireUtils.resolveDependency(parameter, index, testClass, applicationContext);
181+
return ParameterAutowireUtils.resolveDependency(parameter, index, testClass,
182+
applicationContext.getAutowireCapableBeanFactory());
180183
}
181184

182185

0 commit comments

Comments
 (0)