Java 什么时候实例化一个 spring bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4454209/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:45:37  来源:igfitidea点击:

when is a spring bean instantiated

javaspring

提问by java_geek

ApplicationContext ctx = new ClassPathXmlApplicationContext(
    "com/springinaction/springidol/spring-idol.xml");
Performer performer = (Performer) ctx.getBean("duke");
performer.perform();

In the above, when are the beans instantiated, when the ApplicationContext is created or when the getBean() is called?

在上面,什么时候实例化 bean,什么时候创建 ApplicationContext 或者什么时候调用 getBean()?

采纳答案by skaffman

Assuming the bean is a singleton, and isn't configured for lazy initialisation, then it's created when the context is started up. getBean()just fishes it out.

假设 bean 是一个单例,并且没有配置为延迟初始化,那么它会在上下文启动时创建。 getBean()只是把它捞出来。

Lazy-init beans will only be initialised when first referenced, but this is not the default. Scoped beans (e.g. prototype-scoped) will also only be created when first referenced.

Lazy-init beans 只会在第一次被引用时初始化,但这不是默认值。作用域 bean(例如原型作用域)也将仅在第一次引用时创建。

回答by vstoyanov

According to Spring documentation,

根据 Spring文档

The default behavior for ApplicationContext implementations is to eagerly pre-instantiate all singleton beans at startup.

ApplicationContext 实现的默认行为是在启动时急切地预实例化所有单例 bean。

Also, you can set them to load lazily.

此外,您可以将它们设置为延迟加载。

回答by bug

For reference, see

供参考,请参阅

Here's a brief description of when beans are created:

以下是创建 bean 时的简要说明:

  • A singleton bean (which is the default scope) that does not have the lazy-init property set to true (default is false) is constructed when the application context is created
  • A singleton bean that does have the lazy-init property set to true is constructed when it is first requested
  • A bean set in any other scope is created when it is first requested (for that scope).
  • 创建应用程序上下文时会构造一个没有将 lazy-init 属性设置为 true(默认为 false)的单例 bean(这是默认范围)
  • 在第一次请求时构造了一个将 lazy-init 属性设置为 true 的单例 bean
  • 在任何其他范围内设置的 bean 在第一次请求时(针对该范围)被创建。

回答by Yogesh Sanchihar

  1. By default, all beans are singletons, so whenever Application context gets created, they are all pre-loaded. If, specifically, any singleton bean has an attribute lazy-init="true" set, it will be lazy-loaded, i.e. it will be instantiated when the getBean method is called for the first time.

  2. For other scopes, beans will be instantiated whenever they are requested.

  1. 默认情况下,所有 bean 都是单例的,因此无论何时创建应用程序上下文,它们都是预加载的。具体来说,如果任何单例 bean 设置了属性 lazy-init="true" ,它将被延迟加载,即在第一次调用 getBean 方法时将其实例化。

  2. 对于其他范围,bean 将在被请求时实例化。

回答by BenE

By default it's created when the context is started up but the order depends on dependencies. If we have the following classes :

默认情况下,它是在上下文启动时创建的,但顺序取决于依赖项。如果我们有以下类:

@Component
public  class A{

}

@Component
public class B{
    @Autowired
    A a;

}

Class A will be created before class B because class B depends on class A.

A 类将在 B 类之前创建,因为 B 类依赖于 A 类。

回答by BenE

By default, Spring ApplicationContext eagerly creates and initializes all ‘singleton scoped‘ beans during application startup itself. ApplicationContext makes the bean available in BeanFactory. getBean() returns the instance of the bean.

默认情况下,Spring ApplicationContext 在应用程序启动期间急切地创建和初始化所有“单例范围”bean。ApplicationContext 使 bean 在 BeanFactory 中可用。getBean() 返回 bean 的实例。

回答by Shubham Uniyal

It depends what is the scope of the bean you are calling with getBean() method. If it is 'Singleton', it is pre-instantiated by the ApplicationContext.

这取决于您使用 getBean() 方法调用的 bean 的范围。如果它是“Singleton”,则由 ApplicationContext 预先实例化。

If you are using BeanFactory as an IOC Container, then it uses lazy initialization and the beans will be instantiated only when you call the getBean() method.

如果您将 BeanFactory 用作 IOC 容器,则它使用延迟初始化,并且仅在您调用 getBean() 方法时才会实例化 bean。

This is an advantage of ApplicationContext over BeanFactory that it solves Circular Dependency problem.

这是 ApplicationContext 优于 BeanFactory 的一个优势,它解决了循环依赖问题。