05、Spring_web.xml_ContextLoaderListener



類名:ContextLoaderListener;

類路徑:org.springframework.web.context.ContextLoaderListener;

作用:ContextLoaderListener監聽器的作用就是啓動Web容器時,自動裝配ApplicationContext的配置信息。因爲它實現了ServletContextListener這個接口,在web.xml配置這個監聽器,啓動容器時,就會默認執行它實現的方法;


1、ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。如果查看ContextLoaderServlet的API,可以看到它也關聯了ContextLoader這個類而且它實現了HttpServlet這個接口;

2、ContextLoader創建的是 XmlWebApplicationContext這樣一個類,它實現的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->BeanFactory這樣一來spring中的所有bean都由這個類來創建;

3、兩種方式部署applicationContext.xml文件:

A、web.xml中不寫任何參數配置信息,默認路徑是/WEB-INF/applicationContext.xml,名字必須是applicationContext.xml;

B、自定義部署:在classpath路徑下

<context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>
         classpath*:/spring-base.xml
     </param-value>
 </context-param>

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章