您的位置:首页 > 编程语言 > Java开发

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,项目中这种配置,具有通用性。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐