It is easily to know how many contexts have been created if we create ApplicationContext
instances programmatically. However, how many context are created if we use ContextLoaderListener
? For example Spring's reference as below:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</list开发者_JAVA技巧ener>
It has 2 context xml files. Does it means 2 contexts are created?
Thanks.
ContextLoaderListener
creates only one application context containing all beans from files selected in contextConfigLocation
. Bean definitions are merged together and form a single context.
However if you use Spring MVC, the framework will create one extra child context per each DispatcherServlet
.
Only one context is created - only one root application context exists.
Bootstrap listener to start up and shut down Spring's root WebApplicationContext.
If you look at the code of ContextLoader
- it creates a WebApplicationContext
using the contextConfigLocation
param (which is later parsed by the context)
精彩评论