Skip to content

Commit 1d302f4

Browse files
committed
Add customer Application Environment subclasses
Add custom `ApplicationEnvironment`, `ApplicationServletEnvironment` and `ApplicationReactiveWebEnvironment` subclasses for use with `SpringApplication`. The subclasses all disable the resolution of active and default profiles using properties since this is handled directly by the `ConfigDataEnvironmentPostProcessor`. Closes gh-24892 See gh-24890
1 parent d938dd9 commit 1d302f4

15 files changed

+372
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot;
18+
19+
import org.springframework.core.env.StandardEnvironment;
20+
21+
/**
22+
* {@link StandardEnvironment} for typical use in a typical {@link SpringApplication}.
23+
*
24+
* @author Phillip Webb
25+
*/
26+
class ApplicationEnvironment extends StandardEnvironment {
27+
28+
@Override
29+
protected String doGetActiveProfilesProperty() {
30+
return null;
31+
}
32+
33+
@Override
34+
protected String doGetDefaultProfilesProperty() {
35+
return null;
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot;
18+
19+
import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment;
20+
21+
/**
22+
* {@link StandardReactiveWebEnvironment} for typical use in a typical
23+
* {@link SpringApplication}.
24+
*
25+
* @author Phillip Webb
26+
*/
27+
class ApplicationReactiveWebEnvironment extends StandardReactiveWebEnvironment {
28+
29+
@Override
30+
protected String doGetActiveProfilesProperty() {
31+
return null;
32+
}
33+
34+
@Override
35+
protected String doGetDefaultProfilesProperty() {
36+
return null;
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot;
18+
19+
import org.springframework.web.context.support.StandardServletEnvironment;
20+
21+
/**
22+
* {@link StandardServletEnvironment} for typical use in a typical
23+
* {@link SpringApplication}.
24+
*
25+
* @author Phillip Webb
26+
*/
27+
class ApplicationServletEnvironment extends StandardServletEnvironment {
28+
29+
@Override
30+
protected String doGetActiveProfilesProperty() {
31+
return null;
32+
}
33+
34+
@Override
35+
protected String doGetDefaultProfilesProperty() {
36+
return null;
37+
}
38+
39+
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
4848
import org.springframework.boot.convert.ApplicationConversionService;
4949
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
50-
import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment;
5150
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
5251
import org.springframework.context.ApplicationContext;
5352
import org.springframework.context.ApplicationContextInitializer;
@@ -82,7 +81,6 @@
8281
import org.springframework.util.ReflectionUtils;
8382
import org.springframework.util.StopWatch;
8483
import org.springframework.util.StringUtils;
85-
import org.springframework.web.context.support.StandardServletEnvironment;
8684

8785
/**
8886
* Class that can be used to bootstrap and launch a Spring application from a Java main
@@ -391,11 +389,11 @@ private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners
391389
private Class<? extends StandardEnvironment> deduceEnvironmentClass() {
392390
switch (this.webApplicationType) {
393391
case SERVLET:
394-
return StandardServletEnvironment.class;
392+
return ApplicationServletEnvironment.class;
395393
case REACTIVE:
396-
return StandardReactiveWebEnvironment.class;
394+
return ApplicationReactiveWebEnvironment.class;
397395
default:
398-
return StandardEnvironment.class;
396+
return ApplicationEnvironment.class;
399397
}
400398
}
401399

@@ -493,11 +491,11 @@ private ConfigurableEnvironment getOrCreateEnvironment() {
493491
}
494492
switch (this.webApplicationType) {
495493
case SERVLET:
496-
return new StandardServletEnvironment();
494+
return new ApplicationServletEnvironment();
497495
case REACTIVE:
498-
return new StandardReactiveWebEnvironment();
496+
return new ApplicationReactiveWebEnvironment();
499497
default:
500-
return new StandardEnvironment();
498+
return new ApplicationEnvironment();
501499
}
502500
}
503501

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
6262
import org.springframework.context.event.SmartApplicationListener;
6363
import org.springframework.core.Ordered;
64+
import org.springframework.core.env.AbstractEnvironment;
6465
import org.springframework.core.env.ConfigurableEnvironment;
6566
import org.springframework.core.env.Environment;
6667
import org.springframework.core.env.MutablePropertySources;
@@ -361,13 +362,18 @@ private void initializeProfiles() {
361362
this.profiles.addAll(includedViaProperty);
362363
addActiveProfiles(activatedViaProperty);
363364
if (this.profiles.size() == 1) { // only has null profile
364-
for (String defaultProfileName : this.environment.getDefaultProfiles()) {
365+
for (String defaultProfileName : getDefaultProfiles(binder)) {
365366
Profile defaultProfile = new Profile(defaultProfileName, true);
366367
this.profiles.add(defaultProfile);
367368
}
368369
}
369370
}
370371

372+
private String[] getDefaultProfiles(Binder binder) {
373+
return binder.bind(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME, STRING_ARRAY)
374+
.orElseGet(this.environment::getDefaultProfiles);
375+
}
376+
371377
private List<Profile> getOtherActiveProfiles(Set<Profile> activatedViaProperty,
372378
Set<Profile> includedViaProperty) {
373379
return Arrays.stream(this.environment.getActiveProfiles()).map(Profile::new).filter(
@@ -777,9 +783,9 @@ private void applyActiveProfiles(PropertySource<?> defaultProperties) {
777783
if (defaultProperties != null) {
778784
Binder binder = new Binder(ConfigurationPropertySources.from(defaultProperties),
779785
new PropertySourcesPlaceholdersResolver(this.environment));
780-
activeProfiles.addAll(getDefaultProfiles(binder, "spring.profiles.include"));
786+
activeProfiles.addAll(bindStringList(binder, "spring.profiles.include"));
781787
if (!this.activatedProfiles) {
782-
activeProfiles.addAll(getDefaultProfiles(binder, "spring.profiles.active"));
788+
activeProfiles.addAll(bindStringList(binder, "spring.profiles.active"));
783789
}
784790
}
785791
this.processedProfiles.stream().filter(this::isDefaultProfile).map(Profile::getName)
@@ -791,7 +797,7 @@ private boolean isDefaultProfile(Profile profile) {
791797
return profile != null && !profile.isDefaultProfile();
792798
}
793799

794-
private List<String> getDefaultProfiles(Binder binder, String property) {
800+
private List<String> bindStringList(Binder binder, String property) {
795801
return binder.bind(property, STRING_LIST).orElse(Collections.emptyList());
796802
}
797803

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/Profiles.java

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ private boolean hasExplicit(Supplier<String[]> supplier, String propertyValue, S
109109
if (!StringUtils.hasLength(propertyValue)) {
110110
return !unset.equals(profiles);
111111
}
112+
if (unset.equals(profiles)) {
113+
return false;
114+
}
112115
Set<String> propertyProfiles = StringUtils
113116
.commaDelimitedListToSet(StringUtils.trimAllWhitespace(propertyValue));
114117
return !propertyProfiles.equals(profiles);

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/StandardReactiveWebEnvironment.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.web.reactive.context;
1818

1919
import org.springframework.core.env.Environment;
20+
import org.springframework.core.env.MutablePropertySources;
2021
import org.springframework.core.env.StandardEnvironment;
2122

2223
/**
@@ -29,4 +30,12 @@
2930
*/
3031
public class StandardReactiveWebEnvironment extends StandardEnvironment implements ConfigurableReactiveWebEnvironment {
3132

33+
public StandardReactiveWebEnvironment() {
34+
super();
35+
}
36+
37+
protected StandardReactiveWebEnvironment(MutablePropertySources propertySources) {
38+
super(propertySources);
39+
}
40+
3241
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.core.env.AbstractEnvironment;
22+
import org.springframework.core.env.Environment;
23+
import org.springframework.core.env.StandardEnvironment;
24+
import org.springframework.mock.env.MockPropertySource;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Base class for {@link SpringApplication} {@link Environment} tests.
30+
*
31+
* @author Phillip Webb
32+
*/
33+
public abstract class AbstractApplicationEnvironmentTests {
34+
35+
@Test
36+
void getActiveProfilesDoesNotResolveProperty() {
37+
StandardEnvironment environment = createEnvironment();
38+
new MockPropertySource().withProperty("", "");
39+
environment.getPropertySources().addFirst(
40+
new MockPropertySource().withProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "test"));
41+
assertThat(environment.getActiveProfiles()).isEmpty();
42+
}
43+
44+
@Test
45+
void getDefaultProfilesDoesNotResolveProperty() {
46+
StandardEnvironment environment = createEnvironment();
47+
new MockPropertySource().withProperty("", "");
48+
environment.getPropertySources().addFirst(
49+
new MockPropertySource().withProperty(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME, "test"));
50+
assertThat(environment.getDefaultProfiles()).containsExactly("default");
51+
}
52+
53+
protected abstract StandardEnvironment createEnvironment();
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot;
18+
19+
import org.springframework.core.env.StandardEnvironment;
20+
21+
/**
22+
* Tests for {@link ApplicationEnvironment}.
23+
*
24+
* @author Phillip Webb
25+
*/
26+
class ApplicationEnvironmentTests extends AbstractApplicationEnvironmentTests {
27+
28+
@Override
29+
protected StandardEnvironment createEnvironment() {
30+
return new ApplicationEnvironment();
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot;
18+
19+
import org.springframework.core.env.StandardEnvironment;
20+
21+
/**
22+
* Tests for {@link ApplicationReactiveWebEnvironment}.
23+
*
24+
* @author Phillip Webb
25+
*/
26+
class ApplicationReactiveWebEnvironmentTests extends AbstractApplicationEnvironmentTests {
27+
28+
@Override
29+
protected StandardEnvironment createEnvironment() {
30+
return new ApplicationReactiveWebEnvironment();
31+
}
32+
33+
}

0 commit comments

Comments
 (0)