Skip to content

Commit 3a9ab15

Browse files
committed
Reintroduce previous EnvironmentPostProcessor in deprecated form
Restore previous `EnvironmentPostProcessor` in deprecated form to help lessen upgrade pain. Closes gh-47272
1 parent 8d87586 commit 3a9ab15

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureRules.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ static ArchRule allBeanMethodsShouldReturnNonPrivateType() {
130130
}
131131

132132
private static ArchRule allPackagesShouldBeFreeOfTangles() {
133-
return SlicesRuleDefinition.slices().matching("(**)").should().beFreeOfCycles();
133+
return SlicesRuleDefinition.slices()
134+
.matching("(**)")
135+
.should()
136+
.beFreeOfCycles()
137+
.ignoreDependency("org.springframework.boot.env.EnvironmentPostProcessor",
138+
"org.springframework.boot.SpringApplication");
134139
}
135140

136141
private static ArchRule allBeanPostProcessorBeanMethodsShouldBeStaticAndNotCausePrematureInitialization() {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2012-present 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.env;
18+
19+
import org.springframework.boot.bootstrap.BootstrapContext;
20+
import org.springframework.boot.bootstrap.BootstrapRegistry;
21+
import org.springframework.boot.bootstrap.ConfigurableBootstrapContext;
22+
import org.springframework.boot.logging.DeferredLogFactory;
23+
import org.springframework.core.env.ConfigurableEnvironment;
24+
import org.springframework.core.env.Environment;
25+
26+
/**
27+
* Allows for customization of the application's {@link Environment} prior to the
28+
* application context being refreshed.
29+
* <p>
30+
* EnvironmentPostProcessor implementations have to be registered in
31+
* {@code META-INF/spring.factories}, using the fully qualified name of this class as the
32+
* key. Implementations may implement the {@link org.springframework.core.Ordered Ordered}
33+
* interface or use an {@link org.springframework.core.annotation.Order @Order} annotation
34+
* if they wish to be invoked in specific order.
35+
* <p>
36+
* Since Spring Boot 2.4, {@code EnvironmentPostProcessor} implementations may optionally
37+
* take the following constructor parameters:
38+
* <ul>
39+
* <li>{@link DeferredLogFactory} - A factory that can be used to create loggers with
40+
* output deferred until the application has been fully prepared (allowing the environment
41+
* itself to configure logging levels).</li>
42+
* <li>{@link ConfigurableBootstrapContext} - A bootstrap context that can be used to
43+
* store objects that may be expensive to create, or need to be shared
44+
* ({@link BootstrapContext} or {@link BootstrapRegistry} may also be used).</li>
45+
* </ul>
46+
*
47+
* @author Andy Wilkinson
48+
* @author Stephane Nicoll
49+
* @since 1.3.0
50+
* @deprecated since 4.0.0 for removal in 4.2.0 in favor of
51+
* {@link org.springframework.boot.EnvironmentPostProcessor}
52+
*/
53+
@FunctionalInterface
54+
@Deprecated(since = "4.0.0", forRemoval = true)
55+
public interface EnvironmentPostProcessor {
56+
57+
/**
58+
* Post-process the given {@code environment}.
59+
* @param environment the environment to post-process
60+
* @param application the application to which the environment belongs
61+
*/
62+
void postProcessEnvironment(ConfigurableEnvironment environment,
63+
org.springframework.boot.SpringApplication application);
64+
65+
}

core/spring-boot/src/main/java/org/springframework/boot/support/SpringFactoriesEnvironmentPostProcessorsFactory.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616

1717
package org.springframework.boot.support;
1818

19+
import java.util.ArrayList;
1920
import java.util.List;
21+
import java.util.stream.Collectors;
2022

2123
import org.springframework.boot.EnvironmentPostProcessor;
24+
import org.springframework.boot.SpringApplication;
2225
import org.springframework.boot.bootstrap.BootstrapContext;
2326
import org.springframework.boot.bootstrap.BootstrapRegistry;
2427
import org.springframework.boot.bootstrap.ConfigurableBootstrapContext;
2528
import org.springframework.boot.logging.DeferredLogFactory;
29+
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
30+
import org.springframework.core.env.ConfigurableEnvironment;
2631
import org.springframework.core.io.support.SpringFactoriesLoader;
2732
import org.springframework.core.io.support.SpringFactoriesLoader.ArgumentResolver;
2833

@@ -46,7 +51,34 @@ public List<EnvironmentPostProcessor> getEnvironmentPostProcessors(DeferredLogFa
4651
argumentResolver = argumentResolver.and(ConfigurableBootstrapContext.class, bootstrapContext);
4752
argumentResolver = argumentResolver.and(BootstrapContext.class, bootstrapContext);
4853
argumentResolver = argumentResolver.and(BootstrapRegistry.class, bootstrapContext);
49-
return this.loader.load(EnvironmentPostProcessor.class, argumentResolver);
54+
List<Object> postProcessors = new ArrayList<>();
55+
postProcessors.addAll(this.loader.load(EnvironmentPostProcessor.class, argumentResolver));
56+
postProcessors.addAll(loadDeprecatedPostProcessors());
57+
AnnotationAwareOrderComparator.sort(postProcessors);
58+
return postProcessors.stream().map(Adapter::apply).collect(Collectors.toCollection(ArrayList::new));
59+
}
60+
61+
@SuppressWarnings("removal")
62+
private List<org.springframework.boot.env.EnvironmentPostProcessor> loadDeprecatedPostProcessors() {
63+
return this.loader.load(org.springframework.boot.env.EnvironmentPostProcessor.class);
64+
}
65+
66+
@SuppressWarnings("removal")
67+
record Adapter(
68+
org.springframework.boot.env.EnvironmentPostProcessor postProcessor) implements EnvironmentPostProcessor {
69+
70+
@Override
71+
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
72+
this.postProcessor.postProcessEnvironment(environment, application);
73+
}
74+
75+
static EnvironmentPostProcessor apply(Object source) {
76+
if (source instanceof EnvironmentPostProcessor environmentPostProcessor) {
77+
return environmentPostProcessor;
78+
}
79+
return new Adapter((org.springframework.boot.env.EnvironmentPostProcessor) source);
80+
}
81+
5082
}
5183

5284
}

0 commit comments

Comments
 (0)