From 62f40f2c38a77ec1087a2724728a2aa6493b3be5 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 8 Jun 2022 18:43:32 +0100 Subject: [PATCH] Apply initializers and bean registrations before registering classes Previously, classes were registered first which meant that their conditions were evaluated before any initializers and bean registrations were applied. This prevented the bean registrations and initializers from affecting the outcome of the condition evaluation. This commit inverts the ordering so that classes are not registerd, and therefore their conditions are not evaluated, until after the bean registrations and initializers have been applied. Closes gh-31280 --- .../runner/AbstractApplicationContextRunner.java | 4 ++-- .../AbstractApplicationContextRunnerTests.java | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java index d7603b8e57..f611737258 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java @@ -402,12 +402,12 @@ public abstract class AbstractApplicationContextRunner registration.apply(context)); + this.runnerConfiguration.initializers.forEach((initializer) -> initializer.initialize(context)); Class[] classes = Configurations.getClasses(this.runnerConfiguration.configurations); if (classes.length > 0) { ((AnnotationConfigRegistry) context).register(classes); } - this.runnerConfiguration.beanRegistrations.forEach((registration) -> registration.apply(context)); - this.runnerConfiguration.initializers.forEach((initializer) -> initializer.initialize(context)); context.refresh(); } diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java index 2fcf93ff6c..6b694a74b4 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 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. @@ -39,6 +39,7 @@ import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Lazy; +import org.springframework.context.annotation.Profile; import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.util.ClassUtils; @@ -230,6 +231,13 @@ abstract class AbstractApplicationContextRunnerTests context.getEnvironment().setActiveProfiles("test")) + .withUserConfiguration(ProfileConfig.class) + .run((context) -> assertThat(context).hasSingleBean(ProfileConfig.class)); + } + protected abstract T get(); private static void throwCheckedException(String message) throws IOException { @@ -342,4 +350,10 @@ abstract class AbstractApplicationContextRunnerTests