spring项目中,web.xml中的 ContextLoaderListener监听器的原理
2016-10-11 13:58
597 查看
</pre><pre class="java" name="code">创建监听器和ServletContext的code:
</pre><pre class="java" name="code"><context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
ContextLoaderListener的作用(一句话):初始化BeanFactory,并将BeanFactory设置到application中。
说明:
创建了ContextLoaderListener这个监听器,它继承了ContextLoader类、实现了ServletContextListener接口,监听器对Application的创建进行了监听
;
有ServletContext创建了,这个事件就会被监听器的contextInitlized(ServletContext
event)方法监听到;
将application设置到contextLoader属性上;
执行源码的this.contextLoader.initWebApplicationContext(event.getServletContext());
取出web.xml中contextConfigLocation参数的值,也就是spring的配置信息;
根据这些配置信息生成Bean工厂;
最后把这个bean工厂设置到application中去;
可以在后端处理器(Action)中通过application取出beanFactory,进而从beanFactory取出业务对象,进行业务操作。
--------------------------------------------------------------------------------
在spring、springMVC,项目中这种配置,具有通用性。
相关文章推荐
- ContextLoaderListener、RequestContextListener等web项目的监听器,用于实例化spring容器中的组件
- [Spring]web.xml中配置ContextLoaderListener监听器的作用
- [Spring]web.xml中配置ContextLoaderListener监听器的作用
- spring项目中监听器作用-ContextLoaderListener
- maven 项目出现 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- SSH项目问题【六】java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- maven项目报java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- maven 项目出现 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- maven 项目出现 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- jetty启动项目报无法找到org.springframework.web.context.ContextLoaderListener
- maven 项目出现 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- 160329(一)、在web.xml文件里配置org.springframework.web.context.ContextLoaderListener
- maven项目 启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- maven 项目出现 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- maven 项目出现 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- 【转】maven 项目出现 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- maven 项目出现 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- spring项目中监听器作用-ContextLoaderListener
- Spring配置web.xml中<listener>标签(ContextLoaderListener类解析)解析
- Spring源码学习之一 ContextLoaderListener及XmlWebApplicationContext