您的位置:首页 > 移动开发 > WebAPP

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

2015-02-27 21:00 369 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: