java Spring 什么时候创建注入对象的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2084637/
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
When does Spring create instances of objects that are injected
提问by Eager Learner
Spring does DI and creates objects so that your program need not worry of creating objects. But the question here is when an instance of injected object is created. Is it when the main program makes use of the instance or at the time an instance of main program is created.
Spring 进行 DI 并创建对象,因此您的程序无需担心创建对象。但这里的问题是何时创建注入对象的实例。是在主程序使用实例时还是在创建主程序实例时。
回答by skaffman
All beans in the context are instantiated, injected and initialized when the context starts up. By the time the first bean has been retrieved from the context, all beans are ready for use.
当上下文启动时,上下文中的所有 bean 都会被实例化、注入和初始化。当从上下文中检索到第一个 bean 时,所有 bean 都可以使用了。
There are two things that can prevent a bean being initialized at context start up:
有两件事可以防止在上下文启动时初始化 bean:
- A bean has bean configured with a different scope(such as
prototype,requestorsession), using thescope="xyz"attribute - A bean has been marked with
lazy-init="true", in which case it will only be instantiated when it's explicitly asked for, or if it's required as a dependency of some other bean.
- 一个 bean使用属性为bean 配置了不同的作用域(例如
prototype,request或session)scope="xyz" - 一个 bean 被标记为
lazy-init="true",在这种情况下,它只会在明确要求时才会被实例化,或者如果它需要作为其他 bean 的依赖项。
回答by Stephen C
In a comment, the OP writes:
在评论中,OP 写道:
So it is up to the programmer to decide whether a bean needs to be lazily initialized or initialized upfront. This could be very subjective, but could you let me know about any best practices followed in this kind of situations.
因此,由程序员决定是需要延迟初始化还是预先初始化 bean。这可能非常主观,但是您能否让我知道在这种情况下遵循的任何最佳实践。
Yes, it is up to the programmer (or system integrator) to decide.
是的,由程序员(或系统集成商)决定。
There aren't really any "best practice" rules for deciding. Think of it this way:
没有真正的“最佳实践”规则来决定。可以这样想:
If you declare a bean as lazily initialized when it will always need to be instantiated, you will possibly make the startup process slower.
If you declare a bean as eagerly initialized when it is not always needed, you will make the startup process slower, and possible use more memory. In the worst case, creating the unnecessary bean may even cause startup to fail.
如果在始终需要实例化 bean 时将其声明为延迟初始化,则可能会使启动过程变慢。
如果在并不总是需要时将 bean 声明为急切初始化,则会使启动过程变慢,并可能使用更多内存。在最坏的情况下,创建不必要的 bean 甚至可能导致启动失败。
In short, you need to understand your application.
简而言之,您需要了解您的应用程序。
回答by nanda
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 实例,而不是在启动时。

