From d5464a44f0feaaf626182348dce296a4b2ddab84 Mon Sep 17 00:00:00 2001 From: tanruian Date: Tue, 17 Jun 2025 14:14:23 +0800 Subject: [PATCH 1/2] 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 --- .../ManagementContextAutoConfiguration.java | 52 ++++++++++++++----- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java index 760df4cc8f5..ff6b5d766db 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java @@ -23,13 +23,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 +86,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 +109,40 @@ public class ManagementContextAutoConfiguration { } + static class LocalManagementPortPropertySource extends EnumerablePropertySource + implements OriginLookup { + + private static final String[] PROPERTIES = { "local.management.port" }; + + private final ConfigurableEnvironment environment; + + LocalManagementPortPropertySource(ConfigurableEnvironment environment) { + super("Management Server"); + this.environment = environment; + } + + @Override + public String[] getPropertyNames() { + return PROPERTIES; + } + + @Override + public Object getProperty(String name) { + if ("local.management.port".equals(name)) { + return this.environment.getProperty("local.server.port"); + } + return null; + } + + @Override + public Origin getOrigin(String key) { + return null; + } + + @Override + public boolean isImmutable() { + return true; + } + } + } From ad0f08f8a2b66bd4ade7aeab2b88d4af3f917aa0 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 17 Jun 2025 12:18:35 -0700 Subject: [PATCH 2/2] Polish 'Improve binding performance of 'local.management.port' property source' See gh-45968 --- .../ManagementContextAutoConfiguration.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java index ff6b5d766db..ea5f524fb44 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java @@ -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; @@ -109,29 +111,33 @@ public class ManagementContextAutoConfiguration { } + /** + * {@link EnumerablePropertySource} providing {@code local.management.port} support. + */ static class LocalManagementPortPropertySource extends EnumerablePropertySource implements OriginLookup { - private static final String[] PROPERTIES = { "local.management.port" }; + private static final Map PROPERTY_MAPPINGS = Map.of("local.management.port", + "local.server.port"); - private final ConfigurableEnvironment environment; + private static final String[] PROPERTY_NAMES = PROPERTY_MAPPINGS.keySet().toArray(String[]::new); - LocalManagementPortPropertySource(ConfigurableEnvironment environment) { + private final Environment environment; + + LocalManagementPortPropertySource(Environment environment) { super("Management Server"); this.environment = environment; } @Override public String[] getPropertyNames() { - return PROPERTIES; + return PROPERTY_NAMES; } @Override public Object getProperty(String name) { - if ("local.management.port".equals(name)) { - return this.environment.getProperty("local.server.port"); - } - return null; + String mapped = PROPERTY_MAPPINGS.get(name); + return (mapped != null) ? this.environment.getProperty(mapped) : null; } @Override @@ -143,6 +149,7 @@ public class ManagementContextAutoConfiguration { public boolean isImmutable() { return true; } + } }