Skip to content

Commit 5dd40e6

Browse files
committed
Polish
1 parent 4c39073 commit 5dd40e6

File tree

4 files changed

+67
-65
lines changed

4 files changed

+67
-65
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcChildContextConfiguration.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* {@link EmbeddedServletContainer} running on a different port is required.
6363
*
6464
* @author Dave Syer
65+
* @author Stephane Nicoll
6566
* @see EndpointWebMvcAutoConfiguration
6667
*/
6768
@Configuration
@@ -145,18 +146,27 @@ public ManagementErrorEndpoint errorEndpoint(final ErrorAttributes errorAttribut
145146
return new ManagementErrorEndpoint(this.errorPath, errorAttributes);
146147
}
147148

149+
/**
150+
* Configuration to add {@link HandlerMapping} for {@link MvcEndpoint}s. See
151+
* {@link SecureEndpointHandlerMappingConfiguration} for an extended version that also
152+
* configures the security filter.
153+
*/
148154
@Configuration
149155
@ConditionalOnMissingClass(WebSecurityConfigurerAdapter.class)
150-
static class EndpointHandlerMappingSimpleConfiguration {
156+
protected static class EndpointHandlerMappingConfiguration {
151157

152158
@Autowired(required = false)
153159
private List<EndpointHandlerMappingCustomizer> mappingCustomizers;
154160

155161
@Bean
156162
public HandlerMapping handlerMapping(MvcEndpoints endpoints,
157163
ListableBeanFactory beanFactory) {
158-
159-
EndpointHandlerMapping mapping = doCreateEndpointHandlerMapping(endpoints, beanFactory);
164+
Set<MvcEndpoint> set = new HashSet<MvcEndpoint>(endpoints.getEndpoints());
165+
set.addAll(beanFactory.getBeansOfType(MvcEndpoint.class).values());
166+
EndpointHandlerMapping mapping = new EndpointHandlerMapping(set);
167+
// In a child context we definitely want to see the parent endpoints
168+
mapping.setDetectHandlerMethodsInAncestorContexts(true);
169+
postProcessMapping(beanFactory, mapping);
160170
if (this.mappingCustomizers != null) {
161171
for (EndpointHandlerMappingCustomizer customizer : this.mappingCustomizers) {
162172
customizer.customize(mapping);
@@ -165,36 +175,31 @@ public HandlerMapping handlerMapping(MvcEndpoints endpoints,
165175
return mapping;
166176
}
167177

168-
protected EndpointHandlerMapping doCreateEndpointHandlerMapping(MvcEndpoints endpoints,
169-
ListableBeanFactory beanFactory) {
170-
Set<MvcEndpoint> set = new HashSet<MvcEndpoint>(endpoints.getEndpoints());
171-
set.addAll(beanFactory.getBeansOfType(MvcEndpoint.class).values());
172-
EndpointHandlerMapping mapping = new EndpointHandlerMapping(set);
173-
// In a child context we definitely want to see the parent endpoints
174-
mapping.setDetectHandlerMethodsInAncestorContexts(true);
175-
return mapping;
178+
/**
179+
* Hook to allow additional post processing of {@link EndpointHandlerMapping}.
180+
* @param beanFactory the source bean factory
181+
* @param mapping the mapping to customize
182+
*/
183+
protected void postProcessMapping(ListableBeanFactory beanFactory,
184+
EndpointHandlerMapping mapping) {
176185
}
177186

178187
}
179188

189+
/**
190+
* Extension of {@link EndpointHandlerMappingConfiguration} that also configures the
191+
* security filter.
192+
*/
180193
@Configuration
181194
@ConditionalOnClass(WebSecurityConfigurerAdapter.class)
182-
static class EndpointHandlerMappingSecurityConfiguration
183-
extends EndpointHandlerMappingSimpleConfiguration {
195+
protected static class SecureEndpointHandlerMappingConfiguration extends
196+
EndpointHandlerMappingConfiguration {
184197

185198
@Override
186-
protected EndpointHandlerMapping doCreateEndpointHandlerMapping(MvcEndpoints endpoints,
187-
ListableBeanFactory beanFactory) {
188-
189-
EndpointHandlerMapping mapping = super.doCreateEndpointHandlerMapping(endpoints, beanFactory);
190-
injectIntoSecurityFilter(beanFactory, mapping);
191-
return mapping;
192-
}
193-
194-
private void injectIntoSecurityFilter(ListableBeanFactory beanFactory,
199+
protected void postProcessMapping(ListableBeanFactory beanFactory,
195200
EndpointHandlerMapping mapping) {
196-
// The parent context has the security filter, so we need to get it injected with
197-
// our EndpointHandlerMapping if we can.
201+
// The parent context has the security filter, so we need to get it injected
202+
// with our EndpointHandlerMapping if we can.
198203
if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory,
199204
ManagementWebSecurityConfigurerAdapter.class).length == 1) {
200205
ManagementWebSecurityConfigurerAdapter bean = beanFactory

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ public void defaultHealthIndicator() {
9292
public void redisHealthIndicator() {
9393
this.context = new AnnotationConfigApplicationContext();
9494
this.context.register(RedisAutoConfiguration.class,
95-
ManagementServerProperties.class,
96-
HealthIndicatorAutoConfiguration.class);
95+
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
9796
EnvironmentTestUtils.addEnvironment(this.context,
9897
"management.health.diskspace.enabled:false");
9998
this.context.refresh();
@@ -108,8 +107,7 @@ public void redisHealthIndicator() {
108107
public void notRedisHealthIndicator() {
109108
this.context = new AnnotationConfigApplicationContext();
110109
this.context.register(RedisAutoConfiguration.class,
111-
ManagementServerProperties.class,
112-
HealthIndicatorAutoConfiguration.class);
110+
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
113111
EnvironmentTestUtils.addEnvironment(this.context,
114112
"management.health.redis.enabled:false",
115113
"management.health.diskspace.enabled:false");
@@ -125,8 +123,8 @@ public void notRedisHealthIndicator() {
125123
public void mongoHealthIndicator() {
126124
this.context = new AnnotationConfigApplicationContext();
127125
this.context.register(MongoAutoConfiguration.class,
128-
ManagementServerProperties.class,
129-
MongoDataAutoConfiguration.class, HealthIndicatorAutoConfiguration.class);
126+
ManagementServerProperties.class, MongoDataAutoConfiguration.class,
127+
HealthIndicatorAutoConfiguration.class);
130128
EnvironmentTestUtils.addEnvironment(this.context,
131129
"management.health.diskspace.enabled:false");
132130
this.context.refresh();
@@ -141,8 +139,8 @@ public void mongoHealthIndicator() {
141139
public void notMongoHealthIndicator() {
142140
this.context = new AnnotationConfigApplicationContext();
143141
this.context.register(MongoAutoConfiguration.class,
144-
ManagementServerProperties.class,
145-
MongoDataAutoConfiguration.class, HealthIndicatorAutoConfiguration.class);
142+
ManagementServerProperties.class, MongoDataAutoConfiguration.class,
143+
HealthIndicatorAutoConfiguration.class);
146144
EnvironmentTestUtils.addEnvironment(this.context,
147145
"management.health.mongo.enabled:false",
148146
"management.health.diskspace.enabled:false");
@@ -170,8 +168,7 @@ public void combinedHealthIndicator() {
170168
public void dataSourceHealthIndicator() {
171169
this.context = new AnnotationConfigApplicationContext();
172170
this.context.register(EmbeddedDataSourceConfiguration.class,
173-
ManagementServerProperties.class,
174-
HealthIndicatorAutoConfiguration.class);
171+
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
175172
EnvironmentTestUtils.addEnvironment(this.context,
176173
"management.health.diskspace.enabled:false");
177174
this.context.refresh();
@@ -186,8 +183,8 @@ public void dataSourceHealthIndicator() {
186183
public void dataSourceHealthIndicatorWithCustomValidationQuery() {
187184
this.context = new AnnotationConfigApplicationContext();
188185
this.context.register(PropertyPlaceholderAutoConfiguration.class,
189-
ManagementServerProperties.class,
190-
DataSourceProperties.class, DataSourceConfig.class,
186+
ManagementServerProperties.class, DataSourceProperties.class,
187+
DataSourceConfig.class,
191188
DataSourcePoolMetadataProvidersConfiguration.class,
192189
HealthIndicatorAutoConfiguration.class);
193190
EnvironmentTestUtils.addEnvironment(this.context,
@@ -207,8 +204,7 @@ public void dataSourceHealthIndicatorWithCustomValidationQuery() {
207204
public void notDataSourceHealthIndicator() {
208205
this.context = new AnnotationConfigApplicationContext();
209206
this.context.register(EmbeddedDataSourceConfiguration.class,
210-
ManagementServerProperties.class,
211-
HealthIndicatorAutoConfiguration.class);
207+
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
212208
EnvironmentTestUtils.addEnvironment(this.context,
213209
"management.health.db.enabled:false",
214210
"management.health.diskspace.enabled:false");
@@ -224,8 +220,7 @@ public void notDataSourceHealthIndicator() {
224220
public void rabbitHealthIndicator() {
225221
this.context = new AnnotationConfigApplicationContext();
226222
this.context.register(RabbitAutoConfiguration.class,
227-
ManagementServerProperties.class,
228-
HealthIndicatorAutoConfiguration.class);
223+
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
229224
EnvironmentTestUtils.addEnvironment(this.context,
230225
"management.health.diskspace.enabled:false");
231226
this.context.refresh();
@@ -240,8 +235,7 @@ public void rabbitHealthIndicator() {
240235
public void notRabbitHealthIndicator() {
241236
this.context = new AnnotationConfigApplicationContext();
242237
this.context.register(RabbitAutoConfiguration.class,
243-
ManagementServerProperties.class,
244-
HealthIndicatorAutoConfiguration.class);
238+
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
245239
EnvironmentTestUtils.addEnvironment(this.context,
246240
"management.health.rabbit.enabled:false",
247241
"management.health.diskspace.enabled:false");
@@ -257,8 +251,7 @@ public void notRabbitHealthIndicator() {
257251
public void solrHeathIndicator() {
258252
this.context = new AnnotationConfigApplicationContext();
259253
this.context.register(SolrAutoConfiguration.class,
260-
ManagementServerProperties.class,
261-
HealthIndicatorAutoConfiguration.class);
254+
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
262255
EnvironmentTestUtils.addEnvironment(this.context,
263256
"management.health.diskspace.enabled:false");
264257
this.context.refresh();
@@ -273,8 +266,7 @@ public void solrHeathIndicator() {
273266
public void notSolrHeathIndicator() {
274267
this.context = new AnnotationConfigApplicationContext();
275268
this.context.register(SolrAutoConfiguration.class,
276-
ManagementServerProperties.class,
277-
HealthIndicatorAutoConfiguration.class);
269+
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
278270
EnvironmentTestUtils.addEnvironment(this.context,
279271
"management.health.solr.enabled:false",
280272
"management.health.diskspace.enabled:false");

spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadata.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,20 @@ static String toDashedCase(String name) {
8080
Matcher matcher = CAMEL_CASE_PATTERN.matcher(name);
8181
StringBuffer result = new StringBuffer();
8282
while (matcher.find()) {
83-
String first = matcher.group(1);
84-
String second = matcher.group(2);
85-
String target;
86-
if (first.equals("_")) { // not a word for the binder
87-
target = first + second;
88-
} else {
89-
target = first + "-" + second;
90-
}
91-
matcher.appendReplacement(result,target);
83+
matcher.appendReplacement(result, getDashed(matcher));
9284
}
9385
matcher.appendTail(result);
94-
String value = result.toString();
95-
return value.toLowerCase();
86+
return result.toString().toLowerCase();
87+
}
88+
89+
private static String getDashed(Matcher matcher) {
90+
String first = matcher.group(1);
91+
String second = matcher.group(2);
92+
if (first.equals("_")) {
93+
// not a word for the binder
94+
return first + second;
95+
}
96+
return first + "-" + second;
9697
}
9798

9899
}

spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadataTests.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,42 +30,46 @@ public class ConfigurationMetadataTests {
3030

3131
@Test
3232
public void toDashedCaseCamelCase() {
33-
assertThat(ConfigurationMetadata.toDashedCase("simpleCamelCase"), is("simple-camel-case"));
33+
assertThat(toDashedCase("simpleCamelCase"), is("simple-camel-case"));
3434
}
3535

3636
@Test
3737
public void toDashedCaseWordsUnderScore() {
38-
assertThat(ConfigurationMetadata.toDashedCase("Word_With_underscore"), is("word_with_underscore"));
38+
assertThat(toDashedCase("Word_With_underscore"), is("word_with_underscore"));
3939
}
4040

4141
@Test
4242
public void toDashedCaseWordsSeveralUnderScores() {
43-
assertThat(ConfigurationMetadata.toDashedCase("Word___With__underscore"), is("word___with__underscore"));
43+
assertThat(toDashedCase("Word___With__underscore"), is("word___with__underscore"));
4444
}
4545

4646
@Test
4747
public void toDashedCaseLowerCaseUnderscore() {
48-
assertThat(ConfigurationMetadata.toDashedCase("lower_underscore"), is("lower_underscore"));
48+
assertThat(toDashedCase("lower_underscore"), is("lower_underscore"));
4949
}
5050

5151
@Test
5252
public void toDashedCaseUpperUnderscore() {
53-
assertThat(ConfigurationMetadata.toDashedCase("UPPER_UNDERSCORE"), is("upper_underscore"));
53+
assertThat(toDashedCase("UPPER_UNDERSCORE"), is("upper_underscore"));
5454
}
5555

5656
@Test
5757
public void toDashedCaseMultipleUnderscores() {
58-
assertThat(ConfigurationMetadata.toDashedCase("super___crazy"), is("super___crazy"));
58+
assertThat(toDashedCase("super___crazy"), is("super___crazy"));
5959
}
6060

6161
@Test
6262
public void toDashedCaseUppercase() {
63-
assertThat(ConfigurationMetadata.toDashedCase("UPPERCASE"), is("uppercase"));
63+
assertThat(toDashedCase("UPPERCASE"), is("uppercase"));
6464
}
6565

6666
@Test
6767
public void toDashedCaseLowercase() {
68-
assertThat(ConfigurationMetadata.toDashedCase("lowercase"), is("lowercase"));
68+
assertThat(toDashedCase("lowercase"), is("lowercase"));
69+
}
70+
71+
private String toDashedCase(String name) {
72+
return ConfigurationMetadata.toDashedCase(name);
6973
}
7074

7175
}

0 commit comments

Comments
 (0)