I get confused between the two ApplicationContext and ServletContext when it comes to Spring MVC Application. I know that There is just only One ApplicationContext per Sprin
It is initialized when an Servlet application is deployed. Servlet Context holds all the configurations (init-param, context-params etc) of the whole servlet application.
It is a Spring specific thing. It is initialized by Spring. It holds all the bean definitions and life-cycle of the of the beans that is defined inside the spring configuration files. Servlet-Context has no idea about this things.
There are two types of contexts in Spring parent and child.
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
/WEB-INF/service-context.xml,
/WEB-INF/dao-context.xml,
/WEB-INF/was-context.xml,
/WEB-INF/jndi-context.xml,
/WEB-INF/json-context.xml
role-purpose-of-contextloaderlistener-in-spring
Spring-ContextLoaderListener-And-DispatcherServlet-Concepts
When spring container start ups, it reads all the bean definitions from the configuration files and creates beans objects and manages life cycle of the beans objects.
This configuration is totally optional.
DispatcherServlet vs ContextLoaderListener
/declaring-spring-bean-in-parent-context-vs-child-context
myWebApplication
org.springframework.web.servlet.DispatcherServlet
1
myWebApplication
/app/*
When spring web application starts it will look for spring bean configuration file myWebApplication-servlet.xml. It will read all the bean definitions and create and manages the bean objects life cycle. If parent spring context is available it will merge the child spring context with the parent spring context. If there is no Spring parent context available the application will only have the child spring context.