Skip to content

Commit ccdd79f

Browse files
committed
Merge pull request #45968 from tanruian
* pr/45968: Polish 'Improve binding performance of 'local.management.port' property source' Improve binding performance of 'local.management.port' property source Closes gh-45968
2 parents 8f14dca + ad0f08f commit ccdd79f

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

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

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,24 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.web.server;
1818

19+
import java.util.Map;
20+
1921
import org.springframework.beans.factory.SmartInitializingSingleton;
2022
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFactory;
2123
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
2224
import org.springframework.boot.autoconfigure.AutoConfiguration;
2325
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
2426
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2527
import org.springframework.boot.context.properties.EnableConfigurationProperties;
28+
import org.springframework.boot.origin.Origin;
29+
import org.springframework.boot.origin.OriginLookup;
2630
import org.springframework.context.annotation.Bean;
2731
import org.springframework.context.annotation.Configuration;
2832
import org.springframework.context.support.AbstractApplicationContext;
2933
import org.springframework.core.Ordered;
3034
import org.springframework.core.env.ConfigurableEnvironment;
35+
import org.springframework.core.env.EnumerablePropertySource;
3136
import org.springframework.core.env.Environment;
32-
import org.springframework.core.env.PropertySource;
3337
import org.springframework.util.Assert;
3438

3539
/**
@@ -84,17 +88,7 @@ private void verifyAddressConfiguration() {
8488
* @param environment the environment
8589
*/
8690
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-
});
91+
environment.getPropertySources().addLast(new LocalManagementPortPropertySource(environment));
9892
}
9993

10094
@Configuration(proxyBeanMethods = false)
@@ -117,4 +111,45 @@ static ChildManagementContextInitializer childManagementContextInitializer(
117111

118112
}
119113

114+
/**
115+
* {@link EnumerablePropertySource} providing {@code local.management.port} support.
116+
*/
117+
static class LocalManagementPortPropertySource extends EnumerablePropertySource<Object>
118+
implements OriginLookup<String> {
119+
120+
private static final Map<String, String> PROPERTY_MAPPINGS = Map.of("local.management.port",
121+
"local.server.port");
122+
123+
private static final String[] PROPERTY_NAMES = PROPERTY_MAPPINGS.keySet().toArray(String[]::new);
124+
125+
private final Environment environment;
126+
127+
LocalManagementPortPropertySource(Environment environment) {
128+
super("Management Server");
129+
this.environment = environment;
130+
}
131+
132+
@Override
133+
public String[] getPropertyNames() {
134+
return PROPERTY_NAMES;
135+
}
136+
137+
@Override
138+
public Object getProperty(String name) {
139+
String mapped = PROPERTY_MAPPINGS.get(name);
140+
return (mapped != null) ? this.environment.getProperty(mapped) : null;
141+
}
142+
143+
@Override
144+
public Origin getOrigin(String key) {
145+
return null;
146+
}
147+
148+
@Override
149+
public boolean isImmutable() {
150+
return true;
151+
}
152+
153+
}
154+
120155
}

0 commit comments

Comments
 (0)