16
16
17
17
package org .springframework .boot .actuate .autoconfigure .web .server ;
18
18
19
+ import java .util .Map ;
20
+
19
21
import org .springframework .beans .factory .SmartInitializingSingleton ;
20
22
import org .springframework .boot .actuate .autoconfigure .web .ManagementContextFactory ;
21
23
import org .springframework .boot .actuate .autoconfigure .web .ManagementContextType ;
22
24
import org .springframework .boot .autoconfigure .AutoConfiguration ;
23
25
import org .springframework .boot .autoconfigure .AutoConfigureOrder ;
24
26
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
25
27
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
28
+ import org .springframework .boot .origin .Origin ;
29
+ import org .springframework .boot .origin .OriginLookup ;
26
30
import org .springframework .context .annotation .Bean ;
27
31
import org .springframework .context .annotation .Configuration ;
28
32
import org .springframework .context .support .AbstractApplicationContext ;
29
33
import org .springframework .core .Ordered ;
30
34
import org .springframework .core .env .ConfigurableEnvironment ;
35
+ import org .springframework .core .env .EnumerablePropertySource ;
31
36
import org .springframework .core .env .Environment ;
32
- import org .springframework .core .env .PropertySource ;
33
37
import org .springframework .util .Assert ;
34
38
35
39
/**
@@ -84,17 +88,7 @@ private void verifyAddressConfiguration() {
84
88
* @param environment the environment
85
89
*/
86
90
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 ));
98
92
}
99
93
100
94
@ Configuration (proxyBeanMethods = false )
@@ -117,4 +111,45 @@ static ChildManagementContextInitializer childManagementContextInitializer(
117
111
118
112
}
119
113
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
+
120
155
}
0 commit comments