Skip to content

Commit 8d6b442

Browse files
committed
Remove support for spring.profiles
Closes gh-22523
1 parent 573fc34 commit 8d6b442

File tree

8 files changed

+22
-124
lines changed

8 files changed

+22
-124
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ private void applyContributor(ConfigDataEnvironmentContributors contributors,
357357

358358
private void checkForInvalidProperties(ConfigDataEnvironmentContributors contributors) {
359359
for (ConfigDataEnvironmentContributor contributor : contributors) {
360-
InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor);
360+
InvalidConfigDataPropertyException.throwIfPropertyFound(contributor);
361361
}
362362
}
363363

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

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@
2121
import java.util.function.Predicate;
2222

2323
import org.springframework.boot.cloud.CloudPlatform;
24-
import org.springframework.boot.context.properties.bind.BindContext;
25-
import org.springframework.boot.context.properties.bind.BindHandler;
2624
import org.springframework.boot.context.properties.bind.Bindable;
2725
import org.springframework.boot.context.properties.bind.Binder;
2826
import org.springframework.boot.context.properties.bind.Name;
29-
import org.springframework.boot.context.properties.source.ConfigurationProperty;
3027
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
3128
import org.springframework.util.ObjectUtils;
3229

@@ -40,13 +37,8 @@ class ConfigDataProperties {
4037

4138
private static final ConfigurationPropertyName NAME = ConfigurationPropertyName.of("spring.config");
4239

43-
private static final ConfigurationPropertyName LEGACY_PROFILES_NAME = ConfigurationPropertyName
44-
.of("spring.profiles");
45-
4640
private static final Bindable<ConfigDataProperties> BINDABLE_PROPERTIES = Bindable.of(ConfigDataProperties.class);
4741

48-
private static final Bindable<String[]> BINDABLE_STRING_ARRAY = Bindable.of(String[].class);
49-
5042
private final List<ConfigDataLocation> imports;
5143

5244
private final Activate activate;
@@ -87,51 +79,14 @@ ConfigDataProperties withoutImports() {
8779
return new ConfigDataProperties(null, this.activate);
8880
}
8981

90-
ConfigDataProperties withLegacyProfiles(String[] legacyProfiles, ConfigurationProperty property) {
91-
if (this.activate != null && !ObjectUtils.isEmpty(this.activate.onProfile)) {
92-
throw new InvalidConfigDataPropertyException(property, false, NAME.append("activate.on-profile"), null);
93-
}
94-
return new ConfigDataProperties(this.imports, new Activate(this.activate.onCloudPlatform, legacyProfiles));
95-
}
96-
9782
/**
9883
* Factory method used to create {@link ConfigDataProperties} from the given
9984
* {@link Binder}.
10085
* @param binder the binder used to bind the properties
10186
* @return a {@link ConfigDataProperties} instance or {@code null}
10287
*/
10388
static ConfigDataProperties get(Binder binder) {
104-
LegacyProfilesBindHandler legacyProfilesBindHandler = new LegacyProfilesBindHandler();
105-
String[] legacyProfiles = binder.bind(LEGACY_PROFILES_NAME, BINDABLE_STRING_ARRAY, legacyProfilesBindHandler)
106-
.orElse(null);
107-
ConfigDataProperties properties = binder.bind(NAME, BINDABLE_PROPERTIES, new ConfigDataLocationBindHandler())
108-
.orElse(null);
109-
if (!ObjectUtils.isEmpty(legacyProfiles)) {
110-
properties = (properties != null)
111-
? properties.withLegacyProfiles(legacyProfiles, legacyProfilesBindHandler.getProperty())
112-
: new ConfigDataProperties(null, new Activate(null, legacyProfiles));
113-
}
114-
return properties;
115-
}
116-
117-
/**
118-
* {@link BindHandler} used to check for legacy processing properties.
119-
*/
120-
private static class LegacyProfilesBindHandler implements BindHandler {
121-
122-
private ConfigurationProperty property;
123-
124-
@Override
125-
public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, BindContext context,
126-
Object result) {
127-
this.property = context.getConfigurationProperty();
128-
return result;
129-
}
130-
131-
ConfigurationProperty getProperty() {
132-
return this.property;
133-
}
134-
89+
return binder.bind(NAME, BINDABLE_PROPERTIES, new ConfigDataLocationBindHandler()).orElse(null);
13590
}
13691

13792
/**

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import java.util.Map;
2323
import java.util.Set;
2424

25-
import org.apache.commons.logging.Log;
26-
2725
import org.springframework.boot.context.properties.source.ConfigurationProperty;
2826
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
2927
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
@@ -38,14 +36,14 @@
3836
*/
3937
public class InvalidConfigDataPropertyException extends ConfigDataException {
4038

41-
private static final Map<ConfigurationPropertyName, ConfigurationPropertyName> WARNINGS;
39+
private static final Map<ConfigurationPropertyName, ConfigurationPropertyName> ERRORS;
4240
static {
43-
Map<ConfigurationPropertyName, ConfigurationPropertyName> warnings = new LinkedHashMap<>();
44-
warnings.put(ConfigurationPropertyName.of("spring.profiles"),
41+
Map<ConfigurationPropertyName, ConfigurationPropertyName> errors = new LinkedHashMap<>();
42+
errors.put(ConfigurationPropertyName.of("spring.profiles"),
4543
ConfigurationPropertyName.of("spring.config.activate.on-profile"));
46-
warnings.put(ConfigurationPropertyName.of("spring.profiles[0]"),
44+
errors.put(ConfigurationPropertyName.of("spring.profiles[0]"),
4745
ConfigurationPropertyName.of("spring.config.activate.on-profile"));
48-
WARNINGS = Collections.unmodifiableMap(warnings);
46+
ERRORS = Collections.unmodifiableMap(errors);
4947
}
5048

5149
private static final Set<ConfigurationPropertyName> PROFILE_SPECIFIC_ERRORS;
@@ -101,20 +99,18 @@ public ConfigurationPropertyName getReplacement() {
10199
}
102100

103101
/**
104-
* Throw an {@link InvalidConfigDataPropertyException} or log a warning if the given
105-
* {@link ConfigDataEnvironmentContributor} contains any invalid property. A warning
106-
* is logged if the property is still supported, but not recommended. An error is
107-
* thrown if the property is completely unsupported.
108-
* @param logger the logger to use for warnings
102+
* Throw an {@link InvalidConfigDataPropertyException} if the given
103+
* {@link ConfigDataEnvironmentContributor} contains any invalid property.
109104
* @param contributor the contributor to check
110105
*/
111-
static void throwOrWarn(Log logger, ConfigDataEnvironmentContributor contributor) {
106+
static void throwIfPropertyFound(ConfigDataEnvironmentContributor contributor) {
112107
ConfigurationPropertySource propertySource = contributor.getConfigurationPropertySource();
113108
if (propertySource != null) {
114-
WARNINGS.forEach((name, replacement) -> {
109+
ERRORS.forEach((name, replacement) -> {
115110
ConfigurationProperty property = propertySource.getConfigurationProperty(name);
116111
if (property != null) {
117-
logger.warn(getMessage(property, false, replacement, contributor.getResource()));
112+
throw new InvalidConfigDataPropertyException(property, false, replacement,
113+
contributor.getResource());
118114
}
119115
});
120116
if (contributor.isFromProfileSpecificImport()

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.assertj.core.api.Condition;
3434
import org.junit.jupiter.api.AfterEach;
3535
import org.junit.jupiter.api.BeforeEach;
36-
import org.junit.jupiter.api.Disabled;
3736
import org.junit.jupiter.api.Test;
3837
import org.junit.jupiter.api.io.TempDir;
3938

@@ -161,7 +160,8 @@ void runWhenNoActiveProfilesLoadsDefaultProfileFile() {
161160
@Test
162161
void runWhenActiveProfilesDoesNotLoadDefault() {
163162
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testprofilesdocument",
164-
"--spring.profiles.default=thedefault", "--spring.profiles.active=other");
163+
"--spring.config.location=classpath:configdata/profiles/", "--spring.profiles.default=thedefault",
164+
"--spring.profiles.active=other");
165165
String property = context.getEnvironment().getProperty("my.property");
166166
assertThat(property).isEqualTo("fromotherprofile");
167167
}
@@ -591,7 +591,6 @@ void runWhenResolvedIsOptionalDoesNotThrowException() {
591591
}
592592

593593
@Test
594-
@Disabled("Disabled until spring.profiles suppport is dropped")
595594
void runWhenUsingInvalidPropertyThrowsException() {
596595
assertThatExceptionOfType(InvalidConfigDataPropertyException.class).isThrownBy(
597596
() -> this.application.run("--spring.config.location=classpath:invalidproperty.properties"));

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataPropertiesTests.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.springframework.mock.env.MockEnvironment;
3030

3131
import static org.assertj.core.api.Assertions.assertThat;
32-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3332

3433
/**
3534
* Tests for {@link ConfigDataProperties}.
@@ -193,27 +192,6 @@ void isActiveAgainstBoundDataWhenCloudPlatformDoesntMatch() {
193192
assertThat(properties.isActive(context)).isFalse();
194193
}
195194

196-
@Test
197-
void isActiveWhenBindingToLegacyProperty() {
198-
MapConfigurationPropertySource source = new MapConfigurationPropertySource();
199-
source.put("spring.profiles", "a,b");
200-
Binder binder = new Binder(source);
201-
ConfigDataProperties properties = ConfigDataProperties.get(binder);
202-
ConfigDataActivationContext context = new ConfigDataActivationContext(CloudPlatform.KUBERNETES,
203-
createTestProfiles());
204-
assertThat(properties.isActive(context)).isTrue();
205-
}
206-
207-
@Test
208-
void getWhenHasLegacyAndNewPropertyThrowsException() {
209-
MapConfigurationPropertySource source = new MapConfigurationPropertySource();
210-
source.put("spring.profiles", "a,b");
211-
source.put("spring.config.activate.on-profile", "a | b");
212-
Binder binder = new Binder(source);
213-
assertThatExceptionOfType(InvalidConfigDataPropertyException.class)
214-
.isThrownBy(() -> ConfigDataProperties.get(binder));
215-
}
216-
217195
@Test
218196
void getImportOriginWhenCommaListReturnsOrigin() {
219197
MapConfigurationPropertySource source = new MapConfigurationPropertySource();

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/InvalidConfigDataPropertyExceptionTests.java

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.context.config;
1818

19-
import org.apache.commons.logging.Log;
20-
import org.junit.jupiter.api.Disabled;
2119
import org.junit.jupiter.api.Test;
2220

2321
import org.springframework.boot.context.config.ConfigDataEnvironmentContributor.Kind;
@@ -30,8 +28,6 @@
3028
import static org.assertj.core.api.Assertions.assertThat;
3129
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3230
import static org.assertj.core.api.Assertions.assertThatNoException;
33-
import static org.mockito.BDDMockito.then;
34-
import static org.mockito.Mockito.mock;
3531

3632
/**
3733
* Tests for {@link InvalidConfigDataPropertyException}.
@@ -49,8 +45,6 @@ class InvalidConfigDataPropertyExceptionTests {
4945

5046
private ConfigurationProperty property = new ConfigurationProperty(this.invalid, "bad", MockOrigin.of("origin"));
5147

52-
private Log logger = mock(Log.class);
53-
5448
@Test
5549
void createHasCorrectMessage() {
5650
assertThat(new InvalidConfigDataPropertyException(this.property, false, this.replacement, this.resource))
@@ -106,13 +100,12 @@ void getReplacementReturnsReplacement() {
106100
}
107101

108102
@Test
109-
@Disabled("Disabled until spring.profiles support is dropped")
110103
void throwOrWarnWhenHasInvalidPropertyThrowsException() {
111104
MockPropertySource propertySource = new MockPropertySource();
112105
propertySource.setProperty("spring.profiles", "a");
113106
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofExisting(propertySource);
114107
assertThatExceptionOfType(InvalidConfigDataPropertyException.class)
115-
.isThrownBy(() -> InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor))
108+
.isThrownBy(() -> InvalidConfigDataPropertyException.throwIfPropertyFound(contributor))
116109
.withMessageStartingWith("Property 'spring.profiles' is invalid and should be replaced with "
117110
+ "'spring.config.activate.on-profile'");
118111
}
@@ -128,14 +121,13 @@ void throwOrWarnWhenWhenHasInvalidProfileSpecificPropertyThrowsException() {
128121
void throwOrWarnWhenWhenHasInvalidProfileSpecificPropertyOnIgnoringProfilesContributorDoesNotThrowException() {
129122
ConfigDataEnvironmentContributor contributor = createInvalidProfileSpecificPropertyContributor(
130123
"spring.profiles.active", ConfigData.Option.IGNORE_PROFILES);
131-
assertThatNoException()
132-
.isThrownBy(() -> InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor));
124+
assertThatNoException().isThrownBy(() -> InvalidConfigDataPropertyException.throwIfPropertyFound(contributor));
133125
}
134126

135127
private void throwOrWarnWhenWhenHasInvalidProfileSpecificPropertyThrowsException(String name) {
136128
ConfigDataEnvironmentContributor contributor = createInvalidProfileSpecificPropertyContributor(name);
137129
assertThatExceptionOfType(InvalidConfigDataPropertyException.class)
138-
.isThrownBy(() -> InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor))
130+
.isThrownBy(() -> InvalidConfigDataPropertyException.throwIfPropertyFound(contributor))
139131
.withMessageStartingWith("Property '" + name + "' is invalid in a profile specific resource");
140132
}
141133

@@ -153,27 +145,7 @@ private ConfigDataEnvironmentContributor createInvalidProfileSpecificPropertyCon
153145
void throwOrWarnWhenHasNoInvalidPropertyDoesNothing() {
154146
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor
155147
.ofExisting(new MockPropertySource());
156-
InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor);
157-
}
158-
159-
@Test
160-
void throwOrWarnWhenHasWarningPropertyLogsWarning() {
161-
MockPropertySource propertySource = new MockPropertySource();
162-
propertySource.setProperty("spring.profiles", "a");
163-
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofExisting(propertySource);
164-
InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor);
165-
then(this.logger).should().warn("Property 'spring.profiles' is invalid and should be replaced with "
166-
+ "'spring.config.activate.on-profile' [origin: \"spring.profiles\" from property source \"mockProperties\"]");
167-
}
168-
169-
@Test
170-
void throwOrWarnWhenHasWarningPropertyWithListSyntaxLogsWarning() {
171-
MockPropertySource propertySource = new MockPropertySource();
172-
propertySource.setProperty("spring.profiles[0]", "a");
173-
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofExisting(propertySource);
174-
InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor);
175-
then(this.logger).should().warn("Property 'spring.profiles[0]' is invalid and should be replaced with "
176-
+ "'spring.config.activate.on-profile' [origin: \"spring.profiles[0]\" from property source \"mockProperties\"]");
148+
InvalidConfigDataPropertyException.throwIfPropertyFound(contributor);
177149
}
178150

179151
private static class TestConfigDataResource extends ConfigDataResource {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
spring.profile=a
1+
spring.profiles=a

spring-boot-project/spring-boot/src/test/resources/testprofiles.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ my:
33
property: fromyamlfile
44
other: notempty
55
---
6-
spring:
7-
profiles: dev
6+
spring.config.activate.on-profile: dev
87
my:
98
property: fromdevprofile
109
---
11-
spring:
12-
profiles: other
10+
spring.config.activate.on-profile: other
1311
my:
1412
property: fromotherprofile

0 commit comments

Comments
 (0)