From a7770e7448af16cb2028b4c4bab30be676cd53ec Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 26 Feb 2019 15:22:25 +0100 Subject: [PATCH] Remove ApplicationContext dependency from ParameterAutowireUtils This commit is a prerequisite for gh-2060. --- .../junit/jupiter/ParameterAutowireUtils.java | 37 +++++++++---------- .../junit/jupiter/SpringExtension.java | 11 ++++-- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/ParameterAutowireUtils.java b/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/ParameterAutowireUtils.java index b9351b948666..b49d801a63c5 100644 --- a/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/ParameterAutowireUtils.java +++ b/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/ParameterAutowireUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,6 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.beans.factory.config.DependencyDescriptor; -import org.springframework.context.ApplicationContext; import org.springframework.core.MethodParameter; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.SynthesizingMethodParameter; @@ -63,20 +62,19 @@ public Annotation[] getDeclaredAnnotations() { /** - * Determine if the supplied {@link Parameter} can potentially be - * autowired from an {@link ApplicationContext}. - *

Returns {@code true} if the supplied parameter is of type - * {@link ApplicationContext} (or a sub-type thereof) or is annotated or + * Determine if the supplied {@link Parameter} can potentially be + * autowired from an {@link AutowireCapableBeanFactory}. + *

Returns {@code true} if the supplied parameter is annotated or * meta-annotated with {@link Autowired @Autowired}, * {@link Qualifier @Qualifier}, or {@link Value @Value}. + *

Note that {@link #resolveDependency} may still be able to resolve the + * dependency for the supplied parameter even if this method returns {@code false}. * @param parameter the parameter whose dependency should be autowired - * @param parameterIndex the index of the parameter + * @param parameterIndex the index of the parameter in the constructor or method + * that declares the parameter * @see #resolveDependency */ static boolean isAutowirable(Parameter parameter, int parameterIndex) { - if (ApplicationContext.class.isAssignableFrom(parameter.getType())) { - return true; - } AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex); return (AnnotatedElementUtils.hasAnnotation(annotatedParameter, Autowired.class) || AnnotatedElementUtils.hasAnnotation(annotatedParameter, Qualifier.class) || @@ -85,34 +83,35 @@ static boolean isAutowirable(Parameter parameter, int parameterIndex) { /** * Resolve the dependency for the supplied {@link Parameter} from the - * supplied {@link ApplicationContext}. + * supplied {@link AutowireCapableBeanFactory}. *

Provides comprehensive autowiring support for individual method parameters * on par with Spring's dependency injection facilities for autowired fields and * methods, including support for {@link Autowired @Autowired}, * {@link Qualifier @Qualifier}, and {@link Value @Value} with support for property * placeholders and SpEL expressions in {@code @Value} declarations. - *

The dependency is required unless the parameter is annotated with - * {@link Autowired @Autowired} with the {@link Autowired#required required} + *

The dependency is required unless the parameter is annotated or meta-annotated + * with {@link Autowired @Autowired} with the {@link Autowired#required required} * flag set to {@code false}. *

If an explicit qualifier is not declared, the name of the parameter * will be used as the qualifier for resolving ambiguities. * @param parameter the parameter whose dependency should be resolved - * @param parameterIndex the index of the parameter + * @param parameterIndex the index of the parameter in the constructor or method + * that declares the parameter * @param containingClass the concrete class that contains the parameter; this may * differ from the class that declares the parameter in that it may be a subclass * thereof, potentially substituting type variables - * @param applicationContext the application context from which to resolve the - * dependency + * @param beanFactory the {@code AutowireCapableBeanFactory} from which to resolve + * the dependency * @return the resolved object, or {@code null} if none found * @throws BeansException if dependency resolution failed * @see #isAutowirable * @see Autowired#required - * @see SynthesizingMethodParameter#forParameter(Parameter) + * @see SynthesizingMethodParameter#forExecutable(Executable, int) * @see AutowireCapableBeanFactory#resolveDependency(DependencyDescriptor, String) */ @Nullable static Object resolveDependency( - Parameter parameter, int parameterIndex, Class containingClass, ApplicationContext applicationContext) { + Parameter parameter, int parameterIndex, Class containingClass, AutowireCapableBeanFactory beanFactory) { AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex); Autowired autowired = AnnotatedElementUtils.findMergedAnnotation(annotatedParameter, Autowired.class); @@ -122,7 +121,7 @@ static Object resolveDependency( parameter.getDeclaringExecutable(), parameterIndex); DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required); descriptor.setContainingClass(containingClass); - return applicationContext.getAutowireCapableBeanFactory().resolveDependency(descriptor, null); + return beanFactory.resolveDependency(descriptor, null); } /** diff --git a/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java b/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java index f8b62497865a..43b7c98bad41 100644 --- a/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java +++ b/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -143,8 +143,9 @@ public void afterEach(ExtensionContext context) throws Exception { * Determine if the value for the {@link Parameter} in the supplied {@link ParameterContext} * should be autowired from the test's {@link ApplicationContext}. *

Returns {@code true} if the parameter is declared in a {@link Constructor} - * that is annotated with {@link Autowired @Autowired} and otherwise delegates to - * {@link ParameterAutowireUtils#isAutowirable}. + * that is annotated with {@link Autowired @Autowired} or if the parameter is + * of type {@link ApplicationContext} (or a sub-type thereof) and otherwise delegates + * to {@link ParameterAutowireUtils#isAutowirable}. *

WARNING: If the parameter is declared in a {@code Constructor} * that is annotated with {@code @Autowired}, Spring will assume the responsibility * for resolving all parameters in the constructor. Consequently, no other registered @@ -159,6 +160,7 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon Executable executable = parameter.getDeclaringExecutable(); return (executable instanceof Constructor && AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) || + ApplicationContext.class.isAssignableFrom(parameter.getType()) || ParameterAutowireUtils.isAutowirable(parameter, index); } @@ -176,7 +178,8 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte int index = parameterContext.getIndex(); Class testClass = extensionContext.getRequiredTestClass(); ApplicationContext applicationContext = getApplicationContext(extensionContext); - return ParameterAutowireUtils.resolveDependency(parameter, index, testClass, applicationContext); + return ParameterAutowireUtils.resolveDependency(parameter, index, testClass, + applicationContext.getAutowireCapableBeanFactory()); }