8 -- 深入使用Spring -- 7...1 启动Spring 容器
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,但这种情况通常只对特定框架才有效。
啦啦啦
啦啦啦
啦啦啦
8 -- 深入使用Spring -- 7...1 启动Spring 容器的更多相关文章
- Spring 快速开始 启动Spring
[启动Spring必须配置] [web.xml部署描述符方式] 1.配置Servlet级别上下文 <servlet> <servlet-name>springDispatche ...
- spring源码:web容器启动(li)
web项目中可以集成spring的ApplicationContext进行bean的管理,这样使用起来bean更加便捷,能够利用到很多spring的特性.我们比较常用的web容器有jetty,tomc ...
- spring boot无法启动,或者正常启动之后无法访问报404的解决办法
以前用spring boot都是用idea的自动创建,或者是用的Jhipster创建的,就没有深究怎么去搭建.但是今天晚上心血来潮,想自己搭一个demo来整合一些技术,于是就花一点时间来手动搭.因为今 ...
- Spring揭秘 读书笔记 五 容器的启动
Spring的IoC容器所起的作用,就是生产bean,并维持bean间的依赖关系.它会以某种方式加载Configuration Metadata(通常也就是XML格式的配置信息),然后根据这些信息绑定 ...
- 配置spring的监听器 让spring随项目的启动而启动
<!-- 配置spring的监听器 让spring随项目的启动而启动 --> <listener> <listener-class>org.springframew ...
- main方法启动spring
main方式读取spring配置.main方法启动spring/ 有时候只想写一下简单的测试用一下. 新建一个maven项目 依赖pom <?xml version="1.0" ...
- 让Spring Boot项目启动时可以根据自定义配置决定初始化哪些Bean
让Spring Boot项目启动时可以根据自定义配置决定初始化哪些Bean 问题描述 实现思路 思路一 [不符合要求] 思路二[满足要求] 思路三[未试验] 问题描述 目前我工作环境下,后端主要的框架 ...
- Spring boot自定义启动字符画(banner)
spring boot项目启动时会打印spring boot的ANSI字符画,可以进行自定义. 如何自定义 实现方式非常简单,我们只需要在Spring Boot工程的/src/main/resourc ...
- spring boot 项目启动无任何反应
遇到的问题 spring boot项目启动后无任何报错,ps有进程,nohub无日志 定位 更换jar包,问题依然存在,将jar包放到其他服务器,运行正常,排除打包问题 同服务器其他系统运行正常,但停 ...
随机推荐
- android开发(31) 动画演示 - 从页面底部向上弹出dialog,消失时逐渐向下
我想实现一个效果,从底部向上逐渐弹出.如下图所示: 1.点击 显示 按钮时,一个dialog对话框从底部慢慢向上弹出. 2.关闭dialog时, dialog缓慢的移动向底部消失.很平滑的效果. ...
- 【oneday_onepage】—— 美国人的仪容整洁与个人卫生
There used to be an old joke in America that people should take a bath once a week, whether they nee ...
- Entity Framework应用:使用EF的DataBase First模式实现数据库的增删改查
在上一篇文章中讲解了如何生成EF的DBFirst模式,接下来讲解如何使用DBFirst模式实现数据库数据的增删改查 一.新增数据 新增一个Student,代码如下: static void Add() ...
- Centos7下安装运行keepalived
master服务器ip地址:192.168.0.182 slave服务器ip地址:192.168.0.189 虚拟ip(VIP,一个尚未占用的内网ip即可)地址:192.168.0.180 确认使用 ...
- Android studio 3+版本apk安装失败问题
studio2.3升级到3.1之后将apk发给别人下载到手机上安装,华为提示安装包无效或与操作系统不兼容,魅族提示apk仅为测试版,要求下载正式版安装. 在网上找了一下,发现是studio3.0之后的 ...
- idea破解,idea激活,使用破解补丁无需注册码
dea激活,JetBrain旗下软件激活 前言 idea激活有多种方式,网上较多的是使用注册码或者填License server网址,目前(2017年8月19日)使用注册码的方式,亲测可用的只有lan ...
- mxnet与tensorflow的卷积实现细节比较
mxnet的卷积 kernel = 3 pad=1边界补充0后,不管stride是否1还是2,imgw = 奇数或者偶数, 都是从图像位置(0,0)开始卷积 tensorlfow的卷积 kernel ...
- SAP Process Integration - High Level ERP/Integration Process --- Cargill Process Concept Design
Customer Industry: Commercial off-the-shelf (COTS) application ,, Food Ingredients or Agricultural S ...
- java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
hibernate查询结果条数集 原写法: Integer count = (Integer )session.createQuery(hql).uniqueResult(); 报错:java.lan ...
- python subprocess 模块
subprocess 模块中有一个功能Popen , 可以在代码中调用系统的命令 其功能比os.system 更加强大 代码示例: command = 'python -u %s/generalMak ...