简体   繁体   中英

when is a spring bean instantiated

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?

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.

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

According to Spring documentation ,

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

Also, you can set them to load lazily.

For reference, see

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

  • 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).
  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, ie 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.

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.

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.

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

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.

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.

Basically, when you run ApplicationContext, it automatically creates all the Annotated beans and makes ready them to use for you.

If you don't need some of them, you can annotate them with @Lazy and they will not instantiated when you run the application

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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