首页 技术 正文
技术 2022年11月21日
0 收藏 381 点赞 2,640 浏览 2427 个字

      8.7.1 启动Spring容器

        对于使用Spring的Web应用,无须手动创建Spring容器,而是通过配置文件声明式地创建Spring容器。因此,在Web应用中创建Spring容器有如下两种方式:

          ⊙ 直接在web.xml文件中配置创建Spring容器

          ⊙ 利用第三方MVC框架的扩展点,创建Spring容器。

        其实第一种创建Spring容器的方式更加常见。为了让Spring容器随Web应用的启动而自动启动,借助于ServletContextListener监听器即可完成,该监听器可以在Web应用启动时回调自定义方法——该方法就可以启动Spring容器。

        Spring 提供了一个ContextLoaderListener,该监听器类实现了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…/>时应指定参数名为contextConfigLoaction。

        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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0"> <!-- 指定多个配置文件 -->
<context-param>
<!-- 参数名为contextConfigLoaction -->
<param-name>contextConfigLoaction</param-name>
<!-- 多个配置文件之间以","隔开 -->
<param-value>/WEB-INF/daoContext.xml,/WEB-INF/applicationContext.xml</param-value>
</context-param> <!-- 使用ContextLoaderListener初始化Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener></web-app>

        如果没有使用contextConfigLocation指定配置文件,则Spring自动查找/WEB-INF/路径下的applicationContext.xml配置文件;如果有contextConfigLocation,则使用该参数确定的配置文件。如果无法找到合适的配置文件,Spring无法正常初始化。

        Spring根据指定的配置文件创建WebApplicationContext对象,并将其保存在Web应用的ServletContext中。在大部分情况下,应用中的Bean无须感受到ApplicationContext的存在,只要利用ApplicationContext的IoC即可。

        如果需要在应用中获取ApplicationContext实例,则可以通过如下代码获取。

WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

        当然,也可以通过ServletContext的getAttribute方法获取ApplicationContext。但使用WebApplicationContextUtils类更方便,因为这样无须记住Applicationtext在ServletContext中的属性名(属性名为WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)。使用WebApplicationContextUtils还有一个额外的好处:如果ServletCotext的WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE属性没有相应的对象,WebApplication-ContextUtils的getWebApplicationContext()方法将会返回空,而不会引起异常。

        还有一种情况,即利用第三方MVC框架的扩展点来创建Spring容器,比如Struts 1,但这种情况通常只对特定框架才有效。

啦啦啦

啦啦啦

啦啦啦

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918