Commit 5d311d62 authored by Phillip Webb's avatar Phillip Webb

Polish

parent ec2064d9
...@@ -315,16 +315,15 @@ public class EndpointWebMvcAutoConfiguration ...@@ -315,16 +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 = getBean(beanFactory, ServerProperties.class); serverPort = getTemporaryBean(beanFactory, ServerProperties.class)
serverPort = bean.getPort(); .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 = getBean(beanFactory, managementPort = getTemporaryBean(beanFactory,
ManagementServerProperties.class); ManagementServerProperties.class).getPort();
managementPort = bean.getPort();
} }
if (managementPort != null && managementPort < 0) { if (managementPort != null && managementPort < 0) {
return DISABLE; return DISABLE;
...@@ -335,7 +334,7 @@ public class EndpointWebMvcAutoConfiguration ...@@ -335,7 +334,7 @@ public class EndpointWebMvcAutoConfiguration
: DIFFERENT); : DIFFERENT);
} }
private static <T> T getBean(BeanFactory beanFactory, Class<T> type) { private static <T> T getTemporaryBean(BeanFactory beanFactory, Class<T> type) {
if (!(beanFactory instanceof ConfigurableListableBeanFactory)) { if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
return null; return null;
} }
...@@ -346,10 +345,16 @@ public class EndpointWebMvcAutoConfiguration ...@@ -346,10 +345,16 @@ public class EndpointWebMvcAutoConfiguration
} }
// Use a temporary child bean factory to avoid instantiating the bean in the // Use a temporary child bean factory to avoid instantiating the bean in the
// parent (it won't be bound to the environment yet) // parent (it won't be bound to the environment yet)
BeanDefinition definition = listable.getBeanDefinition(names[0]); return createTemporaryBean(type, listable,
DefaultListableBeanFactory temp = new DefaultListableBeanFactory(listable); listable.getBeanDefinition(names[0]));
temp.registerBeanDefinition(type.getName(), definition); }
return temp.getBean(type);
private static <T> T createTemporaryBean(Class<T> type,
ConfigurableListableBeanFactory parent, BeanDefinition definition) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(
parent);
beanFactory.registerBeanDefinition(type.getName(), definition);
return beanFactory.getBean(type);
} }
private static Integer getPortProperty(Environment environment, String prefix) { private static Integer getPortProperty(Environment environment, String prefix) {
......
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