Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
306 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...