Skip lazy init for beans that explicitly set lazy to false
This commit also adds tests to ensure that the child management context works when lazy initialization is enabled. Also, it adds a BeanFactoryPostProcessor to the child context so that the server is created and listening for requests but other beans in the child context are not created until requested. See gh-16184
This commit is contained in:
@@ -16,7 +16,11 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.web.server;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.boot.LazyInitializationBeanFactoryPostProcessor;
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
|
||||
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFactory;
|
||||
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
|
||||
@@ -33,6 +37,7 @@ import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
@@ -135,6 +140,10 @@ public class ManagementContextAutoConfiguration {
|
||||
.createManagementContext(this.applicationContext,
|
||||
EnableChildManagementContextConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
if (isLazyInitialization()) {
|
||||
managementContext.addBeanFactoryPostProcessor(
|
||||
new LazyInitializationBeanFactoryPostProcessor());
|
||||
}
|
||||
managementContext.setServerNamespace("management");
|
||||
managementContext.setId(this.applicationContext.getId() + ":management");
|
||||
setClassLoaderIfPossible(managementContext);
|
||||
@@ -144,6 +153,14 @@ public class ManagementContextAutoConfiguration {
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean isLazyInitialization() {
|
||||
AbstractApplicationContext context = (AbstractApplicationContext) this.applicationContext;
|
||||
List<BeanFactoryPostProcessor> postProcessors = context
|
||||
.getBeanFactoryPostProcessors();
|
||||
return postProcessors.stream().anyMatch((
|
||||
postProcessor) -> postProcessor instanceof LazyInitializationBeanFactoryPostProcessor);
|
||||
}
|
||||
|
||||
private void setClassLoaderIfPossible(ConfigurableApplicationContext child) {
|
||||
if (child instanceof DefaultResourceLoader) {
|
||||
((DefaultResourceLoader) child)
|
||||
|
||||
Reference in New Issue
Block a user