From b1fef925afe382d0064cd6742b21425cae40c80d Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 26 Oct 2020 13:58:45 +0100 Subject: [PATCH] Cache DefaultActiveProfilesResolver instance since it is stateless --- .../context/support/ActiveProfilesUtils.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/support/ActiveProfilesUtils.java b/spring-test/src/main/java/org/springframework/test/context/support/ActiveProfilesUtils.java index 2a2842482b..3cb723130e 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/ActiveProfilesUtils.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/ActiveProfilesUtils.java @@ -50,6 +50,8 @@ abstract class ActiveProfilesUtils { private static final Log logger = LogFactory.getLog(ActiveProfilesUtils.class); + private static final DefaultActiveProfilesResolver defaultActiveProfilesResolver = new DefaultActiveProfilesResolver(); + /** * Resolve active bean definition profiles for the supplied {@link Class}. @@ -86,20 +88,21 @@ abstract class ActiveProfilesUtils { annotation, descriptor.getDeclaringClass().getName())); } + ActiveProfilesResolver resolver; Class resolverClass = annotation.resolver(); if (ActiveProfilesResolver.class == resolverClass) { - resolverClass = DefaultActiveProfilesResolver.class; + resolver = defaultActiveProfilesResolver; } - - ActiveProfilesResolver resolver; - try { - resolver = BeanUtils.instantiateClass(resolverClass, ActiveProfilesResolver.class); - } - catch (Exception ex) { - String msg = String.format("Could not instantiate ActiveProfilesResolver of type [%s] " + - "for test class [%s]", resolverClass.getName(), rootDeclaringClass.getName()); - logger.error(msg); - throw new IllegalStateException(msg, ex); + else { + try { + resolver = BeanUtils.instantiateClass(resolverClass, ActiveProfilesResolver.class); + } + catch (Exception ex) { + String msg = String.format("Could not instantiate ActiveProfilesResolver of type [%s] " + + "for test class [%s]", resolverClass.getName(), rootDeclaringClass.getName()); + logger.error(msg); + throw new IllegalStateException(msg, ex); + } } String[] profiles = resolver.resolve(rootDeclaringClass);