Vaishu13 Vaishu13 - 4 months ago 11x
Java Question

Spring contextloaderlistener role in bean creation

I looked at some of docs for

and application context and based on that what I understand is Application Context is the container initialized by a
and load all the spring config files. What I am not clear about is which one is responsible for the bean creation based on the meta data that is provided. Is it the
or the Application Context.


The ApplicationContext creates the beans utilising a BeanFactory, which is the actual component that turns xml/annotations into classes and manages the lifecycle of each.

A ContextLoaderListener is used when spring is running inside another container (eg a servlet engine like tomcat) to detect the startup of the application and initialise the ApplicationContext. When the spring is used in another context -eg a java program run through a main method - the application can create the AppContext directly without needing the ContextLoaderListener like this:

public class Main {
    public static void main(String[] args) throws Exception {
       ApplicationContext ctx = new ClassPathXmlApplicationContext(
    Foo foo = (Foo) ctx.getBean("fooBean");

} }