ApplicationContext ctx = new ClassPathXmlApplicationContext(
\"com/springinaction/springidol/spring-idol.xml\");
Performer performer = (Performer) ctx.getBean(\"
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 (e.g. prototype-scoped) will also only be created when first referenced.
For reference, see
Here's a brief description of when beans are created:
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.
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.
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.
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.