spring项目中,web.xml中的 ContextLoaderListener监听器的原理

来源:互联网 发布:网宿科技 阿里云 编辑:程序博客网 时间:2024/05/19 21:00
</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.xmlcontextConfigLocation参数的值,也就是spring的配置信息;

    根据这些配置信息生成Bean工厂;

    最后把这个bean工厂设置到application中去;

   可以在后端处理器(Action)中通过application取出beanFactory,进而从beanFactory取出业务对象,进行业务操作。


 --------------------------------------------------------------------------------


在spring、springMVC,项目中这种配置,具有通用性。



0 0
原创粉丝点击