Commit 471947b4 authored by Dave Syer's avatar Dave Syer

Be more defensive when instantiating custom ServerProperties

If the user provides their own ServerProperties bean we want to peek
at it to see if they set the port (and only that) when we are deciding
if the actuator context needs to be created. This happens very early
(in a @Condition) so we need to be very defensive. There are already
quite a few checks in place to prevent a ServerProperties bean from
being instantiated unless we really need it, and yet, when it is
we can do more.

This change creates the bean (and the ManagementProperties) in a
throwaway BeanFactory using the same BeanDefinition as the main
context. This ensures that when the main context bean is created
it will be in the "natural" order and binding to the Environment
can take place as normal.

Fixes gh-4631
parent aae5d11d
...@@ -34,6 +34,7 @@ import org.springframework.beans.factory.SmartInitializingSingleton; ...@@ -34,6 +34,7 @@ import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.ManagementServletContext; import org.springframework.boot.actuate.endpoint.mvc.ManagementServletContext;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
...@@ -314,15 +315,15 @@ public class EndpointWebMvcAutoConfiguration ...@@ -314,15 +315,15 @@ public class EndpointWebMvcAutoConfiguration
Integer serverPort = getPortProperty(environment, "server."); Integer serverPort = getPortProperty(environment, "server.");
if (serverPort == null && hasCustomBeanDefinition(beanFactory, if (serverPort == null && hasCustomBeanDefinition(beanFactory,
ServerProperties.class, ServerPropertiesAutoConfiguration.class)) { ServerProperties.class, ServerPropertiesAutoConfiguration.class)) {
ServerProperties bean = beanFactory.getBean(ServerProperties.class); ServerProperties bean = getBean(beanFactory, ServerProperties.class);
serverPort = bean.getPort(); serverPort = bean.getPort();
} }
Integer managementPort = getPortProperty(environment, "management."); Integer managementPort = getPortProperty(environment, "management.");
if (managementPort == null && hasCustomBeanDefinition(beanFactory, if (managementPort == null && hasCustomBeanDefinition(beanFactory,
ManagementServerProperties.class, ManagementServerProperties.class,
ManagementServerPropertiesAutoConfiguration.class)) { ManagementServerPropertiesAutoConfiguration.class)) {
ManagementServerProperties bean = beanFactory ManagementServerProperties bean = getBean(beanFactory,
.getBean(ManagementServerProperties.class); ManagementServerProperties.class);
managementPort = bean.getPort(); managementPort = bean.getPort();
} }
if (managementPort != null && managementPort < 0) { if (managementPort != null && managementPort < 0) {
...@@ -334,6 +335,23 @@ public class EndpointWebMvcAutoConfiguration ...@@ -334,6 +335,23 @@ public class EndpointWebMvcAutoConfiguration
: DIFFERENT); : DIFFERENT);
} }
private static <T> T getBean(BeanFactory beanFactory, Class<T> type) {
if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
return null;
}
ConfigurableListableBeanFactory listable = (ConfigurableListableBeanFactory) beanFactory;
String[] names = listable.getBeanNamesForType(type, true, false);
if (names == null || names.length != 1) {
return null;
}
// Use a temporary child bean factory to avoid instantiating the bean in the
// parent (it won't be bound to the environment yet)
BeanDefinition definition = listable.getBeanDefinition(names[0]);
DefaultListableBeanFactory temp = new DefaultListableBeanFactory(listable);
temp.registerBeanDefinition(type.getName(), definition);
return temp.getBean(type);
}
private static Integer getPortProperty(Environment environment, String prefix) { private static Integer getPortProperty(Environment environment, String prefix) {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment, RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment,
prefix); prefix);
......
...@@ -320,6 +320,26 @@ public class EndpointWebMvcAutoConfigurationTests { ...@@ -320,6 +320,26 @@ public class EndpointWebMvcAutoConfigurationTests {
assertAllClosed(); assertAllClosed();
} }
@Test
public void overrideServerProperties() throws Exception {
EnvironmentTestUtils.addEnvironment(this.applicationContext,
"server.displayName:foo");
this.applicationContext.register(RootConfig.class, EndpointConfig.class,
ServerPortConfig.class, PropertyPlaceholderAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class,
ServerPropertiesAutoConfiguration.class, JacksonAutoConfiguration.class,
EmbeddedServletContainerAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class,
EndpointWebMvcAutoConfiguration.class);
this.applicationContext.refresh();
assertContent("/controller", ports.get().server, "controlleroutput");
assertEquals("foo",
this.applicationContext.getBean(ServerProperties.class).getDisplayName());
this.applicationContext.close();
assertAllClosed();
}
@Test @Test
public void portPropertiesOnSamePort() throws Exception { public void portPropertiesOnSamePort() throws Exception {
this.applicationContext.register(RootConfig.class, BaseConfiguration.class, this.applicationContext.register(RootConfig.class, BaseConfiguration.class,
......
...@@ -232,6 +232,7 @@ public class ConfigurationPropertiesBindingPostProcessor ...@@ -232,6 +232,7 @@ public class ConfigurationPropertiesBindingPostProcessor
PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer(); PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer();
if (configurer != null) { if (configurer != null) {
// Flatten the sources into a single list so they can be iterated // Flatten the sources into a single list so they can be iterated
// TODO: maybe we don't really need this (and it has lifecycle implications)
return new FlatPropertySources(configurer.getAppliedPropertySources()); return new FlatPropertySources(configurer.getAppliedPropertySources());
} }
if (this.environment instanceof ConfigurableEnvironment) { if (this.environment instanceof ConfigurableEnvironment) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment