Browse Source

Ensure that parameter resolution in SpringExtension is thread-safe

Prior to this commit, parallel execution of @BeforeEach and @AfterEach
methods that accepted @Autowired arguments would fail intermittently
due to a race condition in the internal implementation of the JDK's
java.lang.reflect.Executable.getParameters() method.

This commit addresses this issue by creating instances of
SynthesizingMethodParameter via
SynthesizingMethodParameter.forExecutable(Executable, int) instead of
SynthesizingMethodParameter.forParameter(Parameter), since the latter
looks up the parameter index by iterating over the array returned by
Executable.getParameters() (which is not thread-safe).

Issue: SPR-17533
pull/23430/head
Sam Brannen 6 years ago
parent
commit
cd67b285e1
  1. 3
      spring-test/src/main/java/org/springframework/test/context/junit/jupiter/ParameterAutowireUtils.java

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

@ -118,7 +118,8 @@ abstract class ParameterAutowireUtils { @@ -118,7 +118,8 @@ abstract class ParameterAutowireUtils {
Autowired autowired = AnnotatedElementUtils.findMergedAnnotation(annotatedParameter, Autowired.class);
boolean required = (autowired == null || autowired.required());
MethodParameter methodParameter = SynthesizingMethodParameter.forParameter(parameter);
MethodParameter methodParameter = SynthesizingMethodParameter.forExecutable(
parameter.getDeclaringExecutable(), parameterIndex);
DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
descriptor.setContainingClass(containingClass);
return applicationContext.getAutowireCapableBeanFactory().resolveDependency(descriptor, null);

Loading…
Cancel
Save