-
Notifications
You must be signed in to change notification settings - Fork 40.5k
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
Provide a configuration option to enable lazy initialisation #15870
Comments
hi @wilkinsona , @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import(ProcessorConfiguration.class)
public @interface EnableLazyInitByDefault { } public class ProcessorConfiguration {
@Bean
public FactoryProcessor factoryProcessor() {
return new FactoryProcessor();
}
} public class FactoryProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
for (String s : beanFactory.getBeanDefinitionNames()) {
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(s);
if (beanDefinition instanceof AnnotatedBeanDefinition) {
AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) beanDefinition;
MethodMetadata methodMetadata = annotatedBeanDefinition.getFactoryMethodMetadata();
if (methodMetadata != null)
beanDefinition.setLazyInit(!methodMetadata.getDeclaringClassName().startsWith("org.springframework"));
}
}
}
} i know it's already assigned, i'm just validating my understanding! |
Yeah, that's a large part of it. We're currently planning on enabling lazy init of all beans, not just those in a particular package space. There's also a bit of work to do to make things lazy in a WebFlux app. If you'd like to see what the code currently looks like, it is available in this branch. |
This needs some reference documentation then we're happy for it to be merged. |
Reopening as DevTools restart does not work with lazy initialization enabled. |
Hi Andy, I am glad to see this in the official codebase. Back when I had originally purposed the idea in the context of integration tests, Once we migrate to Boot 2.2, will we still have to exclude the flows or was there some other workaround? Thanks! |
I'd forgotten about the problems that you encountered with the Spring Integration DSL. Sorry. I'm not aware of any changes in Spring Integration so the problem may still exist. Please give 2.2 M1 a try and let us know how you get on. There may be something that we can do to improve the situation, or we may be able to do something in conjunction with the Spring Integration team. If the problem you encountered still occurs, please open a new issue and we'll take a look. |
It might be a bit before we move to 2.2, we JUST finished moving to 2.1. If I get some time I will try to come up with a sample to test things out. |
The bean factory that is used by default in a Spring Boot application performs eager initialisation of all singleton beans. There has been some interest in performing initialisation lazily so we want to provide a configuration option that will allow users to opt in to this behaviour. When enabled, beans will be initialised on first use. In a web application, this will mean that a large number of beans are initialised on first request rather than during the application's startup.
The main benefit of lazy initialisation is that initial startup time is reduced. However, this comes at a cost:
The text was updated successfully, but these errors were encountered: