延迟初始化的 Spring 默认行为
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15092898/
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
Spring default behavior for lazy-init
提问by srk
I am beginner to spring, ESP Inversion of control. I was puzzled understanding the difference between the following
我是spring初学者,ESP控制反转。我很困惑理解以下内容之间的区别
<bean id="demo" class="Demo" lazy-init="false"/>
<bean id="demo" class="Demo" lazy-init="true"/>
<bean id="demo" class="Demo" lazy-init="default"/>
To my understanding : lazy-init=false creates the bean at the startup and lazy-init=true doesn't create a bean at the startup rather creates the bean upon request for a particular bean. Correct me here, If my interpretation is wrong.
据我了解:lazy-init=false 在启动时创建 bean,而 lazy-init=true 不会在启动时创建 bean,而是根据特定 bean 的请求创建 bean。在这里纠正我,如果我的解释是错误的。
what exactly the default behavior of lazy-init is? How would it instantiate?
lazy-init 的默认行为究竟是什么?它会如何实例化?
回答by Vjeetje
The default behaviour is false:
默认行为为 false:
By default, ApplicationContext implementations eagerly create and configure all singleton beans as part of the initialization process. Generally, this pre-instantiation is desirable, because errors in the configuration or surrounding environment are discovered immediately, as opposed to hours or even days later. When this behavior is not desirable, you can prevent pre-instantiation of a singleton bean by marking the bean definition as lazy-initialized. A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.
默认情况下,ApplicationContext 实现会在初始化过程中急切地创建和配置所有单例 bean。通常,这种预实例化是可取的,因为可以立即发现配置或周围环境中的错误,而不是数小时甚至数天后。当这种行为不可取时,您可以通过将 bean 定义标记为延迟初始化来防止单例 bean 的预实例化。一个延迟初始化的 bean 告诉 IoC 容器在它第一次被请求时创建一个 bean 实例,而不是在启动时。
回答by DavidR
For those coming here and are using Java config you can set the Bean to lazy-init using annotations like this:
对于那些来到这里并使用 Java 配置的人,您可以使用如下注释将 Bean 设置为lazy-init:
In the configuration class:
在配置类中:
@Configuration
// @Lazy - For all Beans to load lazily
public class AppConf {
@Bean
@Lazy
public Demo demo() {
return new Demo();
}
}
For component scanning and auto-wiring:
对于元件扫描和自动布线:
@Component
@Lazy
public class Demo {
....
....
}
@Component
public class B {
@Autowired
@Lazy // If this is not here, Demo will still get eagerly instantiated to satisfy this request.
private Demo demo;
.......
}
回答by zagyi
The lazy-init="default"setting on a beanonly refers to what is set by the default-lazy-initattribute of the enclosing beanselement. The implicit default value of default-lazy-initis false.
beanlazy-init="default"上的设置仅指由封闭bean元素的属性设置的内容。隐含的默认值是。default-lazy-initdefault-lazy-initfalse
If there is no lazy-initattribute specified on a bean, it's always eagerly instantiated.
如果lazy-init在 bean 上没有指定属性,它总是急切地实例化。
回答by Carl Massaad
lazy-init is the attribute of bean. The values of lazy-init can be true and false. If lazy-init is true, then that bean will be initialized when a request is made to bean. This bean will not be initialized when the spring container is initialized and if lazy-init is falsethen the bean will be initialized with the spring container initialization.
lazy-init 是 bean 的属性。lazy-init 的值可以是 true 和 false。如果 lazy-init 为 true,那么当对 bean 发出请求时,该 bean 将被初始化。当 spring 容器初始化时,这个 bean 不会被初始化, 如果 lazy-init 为 false,那么 bean 将使用 spring 容器初始化进行初始化。
回答by Prashant_M
When we use lazy-init="default"as an attribute in element, the container picks up the value specified by default-lazy-init="true|false"attribute of element and uses it as lazy-init="true|false".
当我们在元素中使用lazy-init="default"作为属性时,容器会选取元素的default-lazy-init="true|false"属性指定的值并将其用作 lazy-init="true|false" ”。
If default-lazy-init attribute is not present in element than lazy-init="default" in element will behave as if lazy-init-"false".
如果元素中不存在 default-lazy-init 属性,则元素中的 lazy-init="default" 将表现为lazy-init-"false"。

