SpringMVC解析2-ContextLoaderListener
对于SpringMVC功能实现的分析,我们首先从web.xml开始,在web.xml文件中我们首先配置的就是ContextLoaderListener,那么它所提供了功能有哪些又是如何实现的?当使用编程方式的时候我们可以将spring配置传入到Spring容器中,如: ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 但是在web下,我们需要更多的是与Web环境相互结合,通常的办法是将路径以context-param的方式注册并使用ContextLoaderListener进行监听读取。 ContextLoaderListener的作用是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法, 使用ServletContextListener接口,开发者能够在为客户端请求提供服务之前向ServletContext中添加任意的对象 。这个对象在ServletContext启动的时候被初始化,然后在ServletContext整个运行期间都是可见的