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
This commit is contained in:
Phillip Webb
2025-06-17 12:32:13 -07:00
@@ -16,6 +16,8 @@
package org.springframework.boot.actuate.autoconfigure.web.server;
import java.util.Map;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFactory;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
@@ -23,13 +25,15 @@ import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.OriginLookup;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.util.Assert;
/**
@@ -84,17 +88,7 @@ public class ManagementContextAutoConfiguration {
* @param environment the environment
*/
private void addLocalManagementPortPropertyAlias(ConfigurableEnvironment environment) {
environment.getPropertySources().addLast(new PropertySource<>("Management Server") {
@Override
public Object getProperty(String name) {
if ("local.management.port".equals(name)) {
return environment.getProperty("local.server.port");
}
return null;
}
});
environment.getPropertySources().addLast(new LocalManagementPortPropertySource(environment));
}
@Configuration(proxyBeanMethods = false)
@@ -117,4 +111,45 @@ public class ManagementContextAutoConfiguration {
}
/**
* {@link EnumerablePropertySource} providing {@code local.management.port} support.
*/
static class LocalManagementPortPropertySource extends EnumerablePropertySource<Object>
implements OriginLookup<String> {
private static final Map<String, String> PROPERTY_MAPPINGS = Map.of("local.management.port",
"local.server.port");
private static final String[] PROPERTY_NAMES = PROPERTY_MAPPINGS.keySet().toArray(String[]::new);
private final Environment environment;
LocalManagementPortPropertySource(Environment environment) {
super("Management Server");
this.environment = environment;
}
@Override
public String[] getPropertyNames() {
return PROPERTY_NAMES;
}
@Override
public Object getProperty(String name) {
String mapped = PROPERTY_MAPPINGS.get(name);
return (mapped != null) ? this.environment.getProperty(mapped) : null;
}
@Override
public Origin getOrigin(String key) {
return null;
}
@Override
public boolean isImmutable() {
return true;
}
}
}