ContextLoaderListener监听器,加载ROOT WebApplicationContext
1.在web.xml配置监听器
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListener监听器继承了ServletContextListener接口,ServletContextListener监听器提供了
web容器启动时,初始化ServletContext后的事件监听,和销毁ServletContext前的事件监听。
ContextLoaderListener在初始化ServletContext后进行了WebApplicationContext的初始化,在销毁ServletContext前进行了WebApplicationContext的销毁。
ContextLoaderListener初始化WebApplicationContext都做了什么工作?
1.WebApplicationContext实现类的查找
spring允许你配置WebApplicationContext实现类.
1)如果在web.xml中配置了ServletContext的初始化参数,其中名字为contextClass,则代表你配置了相应的WebApplicationContext容器实现类,例如:
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.XmlWebApplicationContext</param-value>
</context-param>
2)如果没有在web.xml中配置WebApplicationContext实现类,则查找classPath下存在的默认配置文件ContextLoader.properties属性文件,这个文件在static
静态域中初始化,具体配置如下:
org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
得到实现类的类名,并使用反射得到该类型,并实例化该对象,这样就得到WebApplicationContext实例。
这里默认使用XmlWebApplicationContext作为WebApplicationContext实例。
2.配置XmlWebApplicationContext
1.设置ServletContext
2.设置spring配置文件资源路径,在web.xml配置了,容器在刷新时会使用这些配置来加载bean及设置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/springmvc.xml;/WEB-INF/spring/*</param-value>
</context-param>
如果没有配置则使用/WEB-INF/applicationContext.xml
3.配置环境变量,除了标准的运行环境参数System.getEnv()和System.getProperties(),spring还设置了一个环境变量,
servletContextInitParams 值为当前的ServletContext
4.支持配置XmlWebApplicationContext在刷新前,实现接口ApplicationContextInitializer
实现initialize(XmlWebApplicationContext applicationContext)方法
需要在web.xml中配置
<context-param>
<param-name>globalInitializerClasses</param-name>
<param-value>com.zghw.springmvc.demo.MyApplicationContextInitializer</param-value>
</context-param>
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>com.zghw.springmvc.demo.MyApplicationContextInitializer1</param-value>
</context-param>
globalInitializerClasses代表所有的web applicat