Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load all beans lazily with @ComponentScan in Spring?

I am using Java-based config to set up my Spring application context like this:

@Configuration
@Lazy
@ComponentScan(basePackageClasses = {MyProject.class, OtherProject.class})
public class MyAppConfig {
    ...
}

Beans defined explicitly in the config are loaded lazily, like you would expect. However, scanned classes annotated with @Named are always loaded eagerly. How can I solve this?

Any help is appreciated.


Note that for classes in the MyProject package, I can work around this by annotating them with @Lazy as well. But the other project does not have a dependency to Spring and I want to keep it like that (hence @Named and not @Component).


Note also that this does not seam to be a problem in XML-based config. There, setting default-lazy-init="true" in the <beans> tag seams to do what I want (although I haven't tested that).

like image 279
rolve Avatar asked Aug 30 '12 23:08

rolve


People also ask

How do you lazy load all the bean using at Lazy?

Lazy load all beans globally. To enable lazy loading for all beans, use default-lazy-init=”true” attribute on beans tag in bean configuration xml files.

How do you implement lazy loading in Spring?

Setting the property value to true means that all the beans in the application will use lazy initialization. This configuration affects all the beans in the context. So, if we want to configure lazy initialization for a specific bean, we can do it through the @Lazy approach.

How do you load all the beans in the package during Spring boot startup?

In Spring Boot, you can use appContext. getBeanDefinitionNames() to get all the beans loaded by the Spring container.

How do you make a lazy bean?

If you are using XML configuration for bean definitions, you can apply lazy initialization to the beans either globally to all beans or to a specific bean. 2.1. To mark a specific bean for lazy initialization in XML, you need to add lazy-init=”true” to the bean definition. lazy-beans-context.


2 Answers

As of version 4.1 RC2, this bug is fixed, and you can accomplish lazy loading on component scan with:

@ComponentScan(basePackages = ["..."], lazyInit = true)

https://jira.spring.io/browse/SPR-10459

like image 122
Eric S. Avatar answered Sep 28 '22 02:09

Eric S.


As you said before there is no direct way to handle that (using @Lazy in the configuration class). But you can try with this approach:

I suppose that OtherProject is a project that is not using Spring, and imagine that these classes are not annotated.

Then you should define in Myproject a configuration that looks like that:

@Configuration
// Avoid use this line if classes aren't annotated @ComponentScan("com.otherProject")
public class MyProjectConfig {

    @Bean(name = "lazyBean")
    @Lazy
    public LazyBean lazyBean(){
        System.out.println("Loading LazyBean bean");
        return new LazyBean(); // Or use a static method factory, this is only an example
    }
}

Using this, the bean "lazyBean" will be created when some instance inject it or when you explicity call it, but never at init time.

Please note that you need to define a new bean per class that you want to use, so this is not good if you have tons of classes but good to minimize the accessibility of classes of your other project (perhaps not all your classes are necessary).

I hope this helps.

like image 29
sgroh Avatar answered Sep 28 '22 03:09

sgroh