Merge branch '2.5.x' into 2.6.x

Closes gh-29374
pull/30003/head
Phillip Webb 3 years ago
commit 91d2b1b988

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -39,6 +39,7 @@ import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy; import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
@ -110,7 +111,8 @@ public class SpringBootContextLoader extends AbstractContextLoader {
application.setWebApplicationType(WebApplicationType.NONE); application.setWebApplicationType(WebApplicationType.NONE);
} }
application.setInitializers(initializers); application.setInitializers(initializers);
ConfigurableEnvironment environment = getEnvironment(application, config.getActiveProfiles()); ConfigurableEnvironment environment = getEnvironment();
setActiveProfiles(environment, config.getActiveProfiles());
ResourceLoader resourceLoader = (application.getResourceLoader() != null) ? application.getResourceLoader() ResourceLoader resourceLoader = (application.getResourceLoader() != null) ? application.getResourceLoader()
: new DefaultResourceLoader(null); : new DefaultResourceLoader(null);
TestPropertySourceUtils.addPropertiesFilesToEnvironment(environment, resourceLoader, TestPropertySourceUtils.addPropertiesFilesToEnvironment(environment, resourceLoader,
@ -121,23 +123,11 @@ public class SpringBootContextLoader extends AbstractContextLoader {
return application.run(args); return application.run(args);
} }
private ConfigurableEnvironment getEnvironment(SpringApplication application, String[] activeProfiles) { private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) {
ConfigurableEnvironment environment = getEnvironment();
boolean applicationEnvironment = false;
if (environment.getClass() == StandardEnvironment.class) {
environment = application.convertEnvironment(environment);
applicationEnvironment = true;
}
setActiveProfiles(environment, activeProfiles, applicationEnvironment);
return environment;
}
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles,
boolean applicationEnvironment) {
if (ObjectUtils.isEmpty(profiles)) { if (ObjectUtils.isEmpty(profiles)) {
return; return;
} }
if (!applicationEnvironment) { if (!(environment instanceof TestEnvironment)) {
environment.setActiveProfiles(profiles); environment.setActiveProfiles(profiles);
} }
String[] pairs = new String[profiles.length]; String[] pairs = new String[profiles.length];
@ -162,7 +152,7 @@ public class SpringBootContextLoader extends AbstractContextLoader {
* @return a {@link ConfigurableEnvironment} instance * @return a {@link ConfigurableEnvironment} instance
*/ */
protected ConfigurableEnvironment getEnvironment() { protected ConfigurableEnvironment getEnvironment() {
return new StandardEnvironment(); return new TestEnvironment();
} }
protected String[] getInlinedProperties(MergedContextConfiguration config) { protected String[] getInlinedProperties(MergedContextConfiguration config) {
@ -293,6 +283,9 @@ public class SpringBootContextLoader extends AbstractContextLoader {
} }
/**
* {@link ApplicationContextInitializer} used to set the parent context.
*/
@Order(Ordered.HIGHEST_PRECEDENCE) @Order(Ordered.HIGHEST_PRECEDENCE)
private static class ParentContextApplicationContextInitializer private static class ParentContextApplicationContextInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> { implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@ -310,4 +303,22 @@ public class SpringBootContextLoader extends AbstractContextLoader {
} }
/**
* Side-effect free {@link Environment} that doesn't set profiles directly from
* properties.
*/
static class TestEnvironment extends StandardEnvironment {
@Override
protected String doGetActiveProfilesProperty() {
return null;
}
@Override
protected String doGetDefaultProfilesProperty() {
return null;
}
}
} }

@ -353,7 +353,9 @@ public class SpringApplication {
* @param environment the environment to convert * @param environment the environment to convert
* @return the converted environment * @return the converted environment
* @since 2.5.7 * @since 2.5.7
* @deprecated since 2.5.8 for removal in 2.7.0
*/ */
@Deprecated
public StandardEnvironment convertEnvironment(ConfigurableEnvironment environment) { public StandardEnvironment convertEnvironment(ConfigurableEnvironment environment) {
return new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment, return new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
deduceEnvironmentClass()); deduceEnvironmentClass());

@ -6,7 +6,8 @@ plugins {
description = "Spring Boot profile smoke test" description = "Spring Boot profile smoke test"
dependencies { dependencies {
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter")) implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web"))
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-webflux"))
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test")) testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,7 +21,9 @@ import smoketest.profile.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.ConfigurableEnvironment;
@SpringBootApplication @SpringBootApplication
public class SampleProfileApplication implements CommandLineRunner { public class SampleProfileApplication implements CommandLineRunner {
@ -38,8 +40,16 @@ public class SampleProfileApplication implements CommandLineRunner {
System.out.println(this.helloWorldService.getMessage()); System.out.println(this.helloWorldService.getMessage());
} }
public static void main(String[] args) { public static void main(String... args) {
SpringApplication.run(SampleProfileApplication.class, args); SpringApplication application = new SpringApplication(SampleProfileApplication.class) {
@Override
protected void bindToSpringApplication(ConfigurableEnvironment environment) {
}
};
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);
} }
} }

@ -0,0 +1,42 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package smoketest.profile;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.assertj.core.api.Assertions.assertThat;
// gh-29169
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class AttributeInjectionTests {
@Autowired(required = false)
private org.springframework.boot.web.servlet.error.ErrorAttributes errorAttributesServlet;
@Autowired(required = false)
private org.springframework.boot.web.reactive.error.ErrorAttributes errorAttributesReactive;
@Test
void contextLoads() {
assertThat(this.errorAttributesServlet).isNull();
assertThat(this.errorAttributesReactive).isNotNull();
}
}

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -48,14 +48,14 @@ class SampleProfileApplicationTests {
@Test @Test
void testDefaultProfile(CapturedOutput output) { void testDefaultProfile(CapturedOutput output) {
SampleProfileApplication.main(new String[0]); SampleProfileApplication.main();
assertThat(output).contains("Hello Phil"); assertThat(output).contains("Hello Phil");
} }
@Test @Test
void testGoodbyeProfile(CapturedOutput output) { void testGoodbyeProfile(CapturedOutput output) {
System.setProperty("spring.profiles.active", "goodbye"); System.setProperty("spring.profiles.active", "goodbye");
SampleProfileApplication.main(new String[0]); SampleProfileApplication.main();
assertThat(output).contains("Goodbye Everyone"); assertThat(output).contains("Goodbye Everyone");
} }
@ -68,13 +68,13 @@ class SampleProfileApplicationTests {
* "name" property. * "name" property.
*/ */
System.setProperty("spring.profiles.active", "generic"); System.setProperty("spring.profiles.active", "generic");
SampleProfileApplication.main(new String[0]); SampleProfileApplication.main();
assertThat(output).contains("Bonjour Phil"); assertThat(output).contains("Bonjour Phil");
} }
@Test @Test
void testGoodbyeProfileFromCommandline(CapturedOutput output) { void testGoodbyeProfileFromCommandline(CapturedOutput output) {
SampleProfileApplication.main(new String[] { "--spring.profiles.active=goodbye" }); SampleProfileApplication.main("--spring.profiles.active=goodbye");
assertThat(output).contains("Goodbye Everyone"); assertThat(output).contains("Goodbye Everyone");
} }

Loading…
Cancel
Save