Spring学习笔记2——web项目初始化webapplicationcontext

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>

 

posted on 2015-02-27 21:00  FeiMin  阅读(1177)  评论(0编辑  收藏  举报

导航