Skip to content

Commit d5464a4

Browse files
tanruianphilwebb
authored andcommitted
Improve binding performance of 'local.management.port' property source
Update `ManagementContextAutoConfiguration` to use an immutable `EnumerablePropertySource` to provide better property binding performance. See gh-45968 Signed-off-by: tanruian <tanruiantra@qq.com>
1 parent 8f14dca commit d5464a4

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
2424
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2525
import org.springframework.boot.context.properties.EnableConfigurationProperties;
26+
import org.springframework.boot.origin.Origin;
27+
import org.springframework.boot.origin.OriginLookup;
2628
import org.springframework.context.annotation.Bean;
2729
import org.springframework.context.annotation.Configuration;
2830
import org.springframework.context.support.AbstractApplicationContext;
2931
import org.springframework.core.Ordered;
3032
import org.springframework.core.env.ConfigurableEnvironment;
33+
import org.springframework.core.env.EnumerablePropertySource;
3134
import org.springframework.core.env.Environment;
32-
import org.springframework.core.env.PropertySource;
3335
import org.springframework.util.Assert;
3436

3537
/**
@@ -84,17 +86,7 @@ private void verifyAddressConfiguration() {
8486
* @param environment the environment
8587
*/
8688
private void addLocalManagementPortPropertyAlias(ConfigurableEnvironment environment) {
87-
environment.getPropertySources().addLast(new PropertySource<>("Management Server") {
88-
89-
@Override
90-
public Object getProperty(String name) {
91-
if ("local.management.port".equals(name)) {
92-
return environment.getProperty("local.server.port");
93-
}
94-
return null;
95-
}
96-
97-
});
89+
environment.getPropertySources().addLast(new LocalManagementPortPropertySource(environment));
9890
}
9991

10092
@Configuration(proxyBeanMethods = false)
@@ -117,4 +109,40 @@ static ChildManagementContextInitializer childManagementContextInitializer(
117109

118110
}
119111

112+
static class LocalManagementPortPropertySource extends EnumerablePropertySource<Object>
113+
implements OriginLookup<String> {
114+
115+
private static final String[] PROPERTIES = { "local.management.port" };
116+
117+
private final ConfigurableEnvironment environment;
118+
119+
LocalManagementPortPropertySource(ConfigurableEnvironment environment) {
120+
super("Management Server");
121+
this.environment = environment;
122+
}
123+
124+
@Override
125+
public String[] getPropertyNames() {
126+
return PROPERTIES;
127+
}
128+
129+
@Override
130+
public Object getProperty(String name) {
131+
if ("local.management.port".equals(name)) {
132+
return this.environment.getProperty("local.server.port");
133+
}
134+
return null;
135+
}
136+
137+
@Override
138+
public Origin getOrigin(String key) {
139+
return null;
140+
}
141+
142+
@Override
143+
public boolean isImmutable() {
144+
return true;
145+
}
146+
}
147+
120148
}

0 commit comments

Comments
 (0)