Spring web.xml

来源:互联网 发布:神秘的程序员 头像 编辑:程序博客网 时间:2024/06/03 14:32

ContextLoaderListener是Spring项目中的web.xml中配置的监听器,

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

这个监听器的作用是什么呢?

第一是可以让我们在Class文件中直接引用Spring管理的bean,第二在容器启动的时候自动装配ApplicationContext配置信息

还有因为ContextLoaderListener可以看一下源码发现,他实际上是实现了ServletContextListener这个接口,在web容器启动的时候自动运行它所实现的方法,ServletContextListener中有两个方法

1、public void contextInitialized(ServletContextEvent sce);  

2、public void contextDestroyed(ServletContextEvent sce);

ContextLoaderListener还继承了ContextLoader这个类,这个类关联WebApplicationContext,  WebApplicationContext接口继承了ApplicationContext接口,所以整个加载过程由contextLoader完成。