Spring學習筆記2——web項目初始化webapplicationcontext

來源:互聯網
上載者:User

標籤:

1、建立web項目來載入spring

webapplicationcontext是專門為web應用準備,它允許從相對於web根目錄的路徑中裝載設定檔,完成初始化操作。從webapplicationcontext中可以獲得servletcontext的引用。

2、webapplicationcontext的初始化通過配置自啟動的servlet或web容器監聽器方式一:配置自啟動的servlet
<!-- 聲明自動啟動的servlet --> <servlet>  <servlet-name>springContextLoaderServlet</servlet-name>  <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>  <!-- 啟動順序 -->  <load-on-startup>1</load-on-startup> </servlet>

方式二:web容器監聽器

<!-- 聲明web容器監聽器 --> <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
說明:spring有三種啟動方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn 
spring3.0的change log 
http://static.springsource.org/spring/docs/3.0.x/changelog.txt
裡面註明: 
removed ContextLoaderServlet and Log4jConfigServlet 
的確是去掉了 
可以採用餘下兩種啟動方式ContextLoaderListener和ContextLoaderPlugIn
建議使用ContextLoaderListener  3、如下是配置的spring設定檔和載入spring設定檔的web.xml
<!-- 配置Spring的用於初始化容器物件的監聽器方式來初始化webapplicationcontext --><context-param><param-name>contextConfigLocation</param-name><!-- <param-value>/WEB-INF/applicationContext.xml</param-value> --><param-value>classpath:applicationContext*.xml</param-value></context-param><!-- 聲明web容器監聽器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>

 

Spring學習筆記2——web項目初始化webapplicationcontext

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.