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