介绍Spring容器时,已经清楚地说过,对于使用Spring的Web应用,无须手动创建Spring容器,而是通过配置文件,声明式地创建Spring容器。因此在Web应用中创建Spring容器有如下两种方式:
- 直接在web.xml文件中配置创建Spring容器。
- 利用第三方MVC框架的扩展点,创建Spring容器。
其实第一种创建Spring窗口的方式更加常见。为了让Spring容器随Web应用的启动而自动启动,借助于ServletContextListener监听器即可完成,该监听器可以在Web应用启动时回调自定义方法——该方法就可以启动Spring容器。
Spring 提供了一个ContextLoadListener,该监听器类实现了ServletContextListener接口。该类可以作为Listener使用,它会在创建时自动查找 WEB-INF/ 下的 applicationContext.xml 文件,因此,如果只有一个配置文件,并且文件名为 applicationContext.xml,则只需在 web.xml 文件中增加如下配置片段即可。
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
如果有多个配置文件需要载入,则考虑使用 <context-param.../> 元素来确定配置文件的文件名。ContextLoaderListener 加载时,会查找名为 contextConfigLocation 的初始化参数。因此,配置 <context-param.../> 时应指定参数名为 contextConfigLocation。
带多个配置文件的 web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!--指定多个配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/daoContext.xml,/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
如果没有使用 contextConfigLocation 指定配置文件,则 Spring 自动查找 applicationContext.xml 配置文件:如果有 contextConfigLocation,则利用该参数确定的配置文件。如果无法找到合适的配置文件,Spring 将无法正常初始化。
Spring 根据指定配置文件创建 WebApplicationContext 对象,并将其保存在 Web 应用的 ServletContext 中。大部分情况下,应用中的 Bean 无须感受到 ApplicationContext的存在,只要利用ApplicationContext 的 IoC 即可.
如果需要在应用中获取 ApplicationContext 实例,则可以通过如下代码获取:
//获取当前Web应用的Spring容器 WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
当然,也可以通过 ServletContext 的 getAttribute 方法获取 ApplicationContext。但使用 WebApplicationContextUtils 类更方便,因为这样无须记住 ApplicationContext 在 ServletContext 中的属性名(属性名为WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)。使用 WebApplicationContextUtils 还有一个额外的好处:即使 ServletContext 的 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 属性没有相应的对象,WebApplicationContextUtils 的 getWebApplicationContext() 方法将会返回空,而不会引起异常。
还有一种情况,即利用第三方MVC框架的扩展点来创建 Spring 容器,比如 Struts1,但这种情况通常只对特定框架才有效,故此处不再赘述。
相关推荐
精通Eclipse整合Web开发——Struts,Spring,Hibernate,JBPM 图书馆下载的,不是很清晰,如果介意的,请不要下载,不要拍砖。 总共有三部分。
spring整合hibernate实现事务处理 1.spring整合hibernate实现事务处理-注解方式 2.spring整合hibernate实现事务处理-XML方式
ssh整合,针对spring整合hibernate代码,示例代码,教程。
Spring整合hibernate(2)之基于HibernateTemplate的整合,jar需要自己添加!
MyClipse6.0\Struts2,Spring与Hibernate整合应用,学生成绩管理系统
Spring整合Hibernate的所有jar包,辛苦整理两个小时,给大家分享出来。 一共27个包
Spring+hibernate整合源代码 结束Sping+hibernate 的使用方法 包括演示示例
spring-hibernate.jar
spring mvc + spring + hibernate 全注解整合开发视频教程 11
精通Eclipse整合Web开发——Struts,Spring,Hibernate,JBPM 图书馆下载的,很不容易。 不是很清晰,如果介意的,请不要下载,不要拍砖。 总共有三部分。
spring整合hibernate所用相关jar包,适合初学者
Spring框架整合Hibernate框架,为大家提供一个学习案例.
spring整合hibernate的所需jar包,很全的!
spring与hibernate的整合 spring事务的配置
这是使用spring整合hibernate、struts时所要用到的核心jar包,洗完对大家有帮助
Spring4整合Hibernate4实现用户购买图书和结账等操作,整合主要实现用IoC容器来管理Hibernate的SessionFactory实例,并使Hibernate使用Spring所提供的声明式事务……
spring boot整合JPA——demo
整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6。 此外,还有:log4j、slf4j、junit4、ehcache等知识点。 项目...
Spring3.3与Hibernate2.1整合
Spring整合Hibernate配置测试示例