Skip to content

Commit eb3d641

Browse files
committed
Merge branch 'spring-projectsgh-4320'
2 parents b02cdc7 + f5b973a commit eb3d641

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

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

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,6 +42,7 @@
4242
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
4343
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
4444
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
45+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4546
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
4647
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
4748
import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration;
@@ -87,6 +88,7 @@
8788
* @author Christian Dupuis
8889
* @author Andy Wilkinson
8990
* @author Johannes Edmeier
91+
* @author Eddú Meléndez
9092
*/
9193
@Configuration
9294
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class })
@@ -198,6 +200,7 @@ public Object getProperty(String name) {
198200
// Put Servlets and Filters in their own nested class so they don't force early
199201
// instantiation of ManagementServerProperties.
200202
@Configuration
203+
@ConditionalOnProperty(prefix = "management", name = "add-application-context-header", matchIfMissing = true, havingValue = "true")
201204
protected static class ApplicationContextFilterConfiguration {
202205

203206
@Bean
@@ -222,8 +225,6 @@ private static class ApplicationContextHeaderFilter extends OncePerRequestFilter
222225

223226
private final ApplicationContext applicationContext;
224227

225-
private ManagementServerProperties properties;
226-
227228
ApplicationContextHeaderFilter(ApplicationContext applicationContext) {
228229
this.applicationContext = applicationContext;
229230
}
@@ -232,14 +233,7 @@ private static class ApplicationContextHeaderFilter extends OncePerRequestFilter
232233
protected void doFilterInternal(HttpServletRequest request,
233234
HttpServletResponse response, FilterChain filterChain)
234235
throws ServletException, IOException {
235-
if (this.properties == null) {
236-
this.properties = this.applicationContext
237-
.getBean(ManagementServerProperties.class);
238-
}
239-
if (this.properties.getAddApplicationContextHeader()) {
240-
response.addHeader("X-Application-Context",
241-
this.applicationContext.getId());
242-
}
236+
response.addHeader("X-Application-Context", this.applicationContext.getId());
243237
filterChain.doFilter(request, response);
244238
}
245239

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -98,6 +98,7 @@
9898
* @author Phillip Webb
9999
* @author Greg Turnquist
100100
* @author Andy Wilkinson
101+
* @author Eddú Meléndez
101102
*/
102103
public class EndpointWebMvcAutoConfigurationTests {
103104

@@ -136,6 +137,7 @@ public void onSamePort() throws Exception {
136137
assertContent("/controller", ports.get().management, null);
137138
assertContent("/endpoint", ports.get().management, null);
138139
assertTrue(hasHeader("/endpoint", ports.get().server, "X-Application-Context"));
140+
assertTrue(this.applicationContext.containsBean("applicationContextIdFilter"));
139141
this.applicationContext.close();
140142
assertAllClosed();
141143
}
@@ -149,6 +151,7 @@ public void onSamePortWithoutHeader() throws Exception {
149151
EndpointWebMvcAutoConfiguration.class);
150152
this.applicationContext.refresh();
151153
assertFalse(hasHeader("/endpoint", ports.get().server, "X-Application-Context"));
154+
assertFalse(this.applicationContext.containsBean("applicationContextIdFilter"));
152155
this.applicationContext.close();
153156
assertAllClosed();
154157
}

0 commit comments

Comments
 (0)