Skip to content
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

Closed
wilkinsona opened this issue Feb 7, 2019 · 7 comments
Closed

Provide a configuration option to enable lazy initialisation #15870

wilkinsona opened this issue Feb 7, 2019 · 7 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@wilkinsona
Copy link
Member

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:

  1. Handling of HTTP requests may take longer while any deferred initialisation occurs
  2. Failures that would normally occur at startup will now not occur until later
@wilkinsona wilkinsona added the type: enhancement A general enhancement label Feb 7, 2019
@wilkinsona wilkinsona added this to the 2.2.x milestone Feb 7, 2019
@wilkinsona wilkinsona self-assigned this Feb 7, 2019
@bishoybasily
Copy link

bishoybasily commented Feb 10, 2019

hi @wilkinsona ,
i'm just asking if something like this can do the job ?

@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!
thank you.

@wilkinsona
Copy link
Member Author

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.

@wilkinsona wilkinsona added the for: team-attention An issue we'd like other members of the team to review label Feb 13, 2019
@wilkinsona
Copy link
Member Author

This needs some reference documentation then we're happy for it to be merged.

@wilkinsona wilkinsona removed the for: team-attention An issue we'd like other members of the team to review label Feb 13, 2019
@wilkinsona wilkinsona modified the milestones: 2.2.x, 2.2.0.M1 Feb 14, 2019
@wilkinsona
Copy link
Member Author

Reopening as DevTools restart does not work with lazy initialization enabled.

@wilkinsona wilkinsona reopened this Mar 8, 2019
@tkvangorder
Copy link

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,
we had run into some issues with the Spring Integration DSL:

#9685

Once we migrate to Boot 2.2, will we still have to exclude the flows or was there some other workaround?

Thanks!

@wilkinsona
Copy link
Member Author

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.

@tkvangorder
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants