spring 如何使用注释将 bean 指定为非惰性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1027129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to specify a bean as non lazy with annotations
提问by Michael Wiles
Does anyone know how to specify a bean as nonlazy when using annotations to configure the bean?
有谁知道在使用注释配置 bean 时如何将 bean 指定为非惰性?
采纳答案by Priyank
Beans are not lazy by default. However as far as annotations are concerned it seems like currently annotations do not support it. http://forum.springsource.org/showthread.php?t=62931
默认情况下,Bean 不是惰性的。然而,就注释而言,目前的注释似乎不支持它。 http://forum.springsource.org/showthread.php?t=62931
Spring's next version though seem to have something in store http://jira.springframework.org/browse/SJC-263
Spring 的下一个版本似乎有一些东西在商店 http://jira.springframework.org/browse/SJC-263
回答by Bozho
In spring 3.0 there is an annotation: @Lazy(false). But note that beans are eager by default.
在 spring 3.0 中有一个注释:@Lazy(false). 但请注意,默认情况下 bean 是渴望的。
回答by yair
Just to set things straight, be known that as to Spring 3.0 and later, beans are by default eagerly initialized.
简单说一下,对于 Spring 3.0 及更高版本,默认情况下会急切地初始化 bean。
Excerpt from the @Lazy(false)link in Bozho's answer:
摘自@Lazy(false)Bozho 回答中的链接:
If this annotation is not present on a Component or Bean definition, eager initialization will occur.If present and set to true, the Bean/Component will not be initialized until referenced by another bean or explicitly retrieved from the enclosing BeanFactory. If present and set to false, the bean will be instantiated on startup by bean factories that perform eager initialization of singletons.
如果此注释不存在于组件或 Bean 定义中,则会发生急切初始化。如果存在并设置为 true,则 Bean/Component 将不会被初始化,直到被另一个 bean 引用或从封闭的 BeanFactory 中显式检索。如果存在并设置为 false,则 bean 将在启动时由执行单例急切初始化的 bean 工厂实例化。
回答by dewrich
I tried @EnableScheduling in my Configuration Annotation class and that did the trick.
我在我的 Configuration Annotation 类中尝试了 @EnableScheduling 并且成功了。

