简体   繁体   English

使用基于注释的配置创建延迟初始化的Spring bean

[英]Creating lazily initialized Spring beans using annotation based configuration

I am using Spring's @Component annotation to configure many of the beans in my Spring 3.0 application. 我使用Spring的@Component注释来配置Spring 3.0应用程序中的许多bean。 I would like to know if it's possible to construct some of these beans lazily - especially the prototype beans? 我想知道是否可以懒惰地构建一些这些豆 - 特别是prototype豆?

To declare lazy-initialized bean you can use @Lazy annotation. 要声明延迟初始化的bean,可以使用@Lazy注释。

Note, however, that it doesn't make sense for prototype beans - they can't be eagerly initialized, so there is no need to mark them lazy. 但请注意,它对prototype bean没有意义 - 它们不能被急切地初始化,因此不需要将它们标记为懒惰。

Lazy initialization isn't an option in the context of prototype-scoped beans. 在原型范围的bean的上下文中,延迟初始化不是一个选项。 Those beans are instantiated and initialized on demand every time something asks for them, so they are, by nature, lazily initialized. 每当有东西要求时,这些bean都会根据需要进行实例化和初始化,因此它们本质上是懒洋洋地初始化的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM