-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Skip lazy initialization of beans that explicitly set the lazy attribute to false #16184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for trying out the milestone. This sounds like a bug to me. We caught a similar one that switched off DevTools restart just before we released but this one slipped through. |
Adding the following bean to your app should work around the problem for now: @Bean
static BeanFactoryPostProcessor ensureEagerManagementServerInitializationPostProcessor() {
return (beanFactory) -> {
String namePrefix = ManagementContextAutoConfiguration.class.getName() + "$";
for (String definitionName : beanFactory.getBeanDefinitionNames()) {
if (definitionName.startsWith(namePrefix)) {
beanFactory.getBeanDefinition(definitionName).setLazyInit(false);
}
}
};
} This will ensure that the logic that creates the child context for the management server is not lazy and that the server is created and started. There's a secondary problem here. The beans in the separate management context that's used when the management port does not match the server port are all initialised eagerly. They should be lazy like the main context so that the server is created and listening for requests but, for example, the endpoints are not created until a request is received. |
We're going to explore the possibility of looking at the bean's class via the bean definition or the metadata available from the definition and not setting the lazy init flag if, for example, the bean implements |
Is it possible to skip any bean definitions that explicitly set their lazy attribute to false? |
@tkvangorder That's a really nice idea, but I think we might need a framework change to be able to do it. Currently |
We could turn this into a |
spring-projects/spring-framework#22694 is now fixed |
I've got a fix in a branch here. I was wondering specifically about @wilkinsona's comment
The branch adds a BFPP to the child context for enabling lazy initialization. However, I don't think the creation of endpoints could be delayed till a request is received because of |
Thanks for looking at this, @mbhave. There's also a change in DevTools that I made that could perhaps be replaced with some use of For the Actuator child context, I wonder if the problem will be solved without The fix that you've made for the laziness in the child context was what I had in mind. I hadn't dug into it in much detail and had only realised that the BFPP was only being applied to the parent context. |
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
Is there a fix for the Spring Integration issue, that would help me resolve issues like the one I am getting here ? |
Hi,
The suspected issue described below is related to the version 2.2.0.M1 of Spring Boot.
It appears that the management endpoints are not reachable when using the new lazy initialization feature with actuator endpoints exposed on another port than the default one.
Steps to reproduce :
management.server.port=8081
andspring.main.lazy-initialization=true
Hit the endpoint
http://localhost:8081/actuator/health
and you'll get a connection refused.Switch off the lazy initialization and the actuator endpoints will be back again.
Note that actuator endpoints are working correctly if I switch on the lazy initialization and do not change the management port.
Is something I didn't understand or is there a little bug in the lazy initialization feature ?
By the way, thank you for this feature that I have been waiting for a long time!
The text was updated successfully, but these errors were encountered: