对于使用Spring的web应用,无须手动创建Spring容器,而是通过配置文件,声明式的创建Spring容器。
在Web应用中,创建Spring容器有如下两种方式:
1、直接在web.xml文件中配置;
2、利用第三方MVC框架的扩展点。

ContextLoaderPlugIn(要导入org.springframework.web.struts-3.0.5.RELEASE.jar包,在struts2.0及以后版本不支持此种方式加载spring)

直接在web.xml文件中配置的方式是更常见。
为了让Spring容器随Web应用的启动而启动,有如下两种方式:
1、利用ServletContextListener实现。
2、利用load-on-startup Servlet(ContextLoaderServlet)实现。(spring3.0及以后版本中已经删除ContextLoaderServlet和Log4jConfigServlet)

<servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

Spring提供ServletContextListener的一个实现类ContextLoaderListener,该类可以作为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文件如下,多个配置文件之间用“,”隔开。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/*.xml,classpath:spring/*.xml</param-value>
    <!--<param-value>classpath:spring*.xml</param-value>-->
</context-param>

创建Spring容器的更多相关文章

  1. 创建 Spring容器的三种方式

    一.src路径下打包完在war包的classes层级下 1.Spring容器创建的三种方式 创建Bean容器之后创建对象: 其中第三种使用的是BeanFactory对象 2.spring通过配置文件用 ...

  2. Spring 读书笔记-----使用Spring容器(一)

    pring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口.他们都可代表Spring容器,Spri ...

  3. Spring读书笔记-----使用Spring容器(二)

    一.使用ApplicationContext 前面介绍了,我们一般不会使用BeanFactory实例作为Spring容器,而是使用ApplicationContext实例作为容器,它增强了BeanFa ...

  4. spring源码学习之:spring容器的applicationContext启动过程

    Spring 容器像一台构造精妙的机器,我们通过配置文件向机器传达控制信息,机器就能够按照设定的模式进行工作.如果我们将Spring容器比喻为一辆汽车,可以将 BeanFactory看成汽车的发动机, ...

  5. (转)Spring读书笔记-----使用Spring容器(二)

    一.使用ApplicationContext 前面介绍了,我们一般不会使用BeanFactory实例作为Spring容器,而是使用ApplicationContext实例作为容器,它增强了BeanFa ...

  6. (转)Spring 读书笔记-----使用Spring容器(一)

    Spring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口.他们都可代表Spring容器,Spr ...

  7. 简述Spring容器与SpringMVC的容器的联系与区别

    简述Spring容器与SpringMVC的容器的联系与区别 2017年07月04日 10:55:07 阅读数:6260 摘要: 在Spring整体框架的核心概念中,容器的核心思想是管理Bean的整个生 ...

  8. springMVC容器和Spring容器

    前段时间有人问我,为什么一定要在web.xml中配置spring的listener呢? <listener> <description>spring监听器</descri ...

  9. web spring 容器

    使用spring的web应用时,不用手动创建spring容器,而是通过配置文件声明式地创建spring容器,因此,在web应用中创建spring容器有如下两种方式: 一.直接在web.xml文件中配置 ...

随机推荐

  1. java网络---基本web概念

    首先需要搞清楚web中的3个关键概念. 一.HTTP,HTML,URL. HTML 全称是Hypertext Markup Language,是用来描述一个网页的语言.或者说,该语言是用来描述网页的“ ...

  2. Visual Studio发布Web项目报错:Unable to add 'xxx' to the Web site. Unable to add file 'xxx'. The specified file could not be encrypted.

    背景 Visual Studio下的Web项目 现象 发布时遇到Unable to add 'xxx' to the Web site.  Unable to add file 'xxx'. The ...

  3. C语言指针学习(续)

    五.数组和指针的关系 int array[10] = {0,1,2,3,4,5,6,7,8,9},value; ... ... value = array[0];//也可以写成 value = *ar ...

  4. 日志级别的选择:Debug、Info、Warn、Error还是Fatal

    原文地址:日志级别的选择:Debug.Info.Warn.Error还是Fatal 作者:shanshan2627 软件中总免不了要使用诸如 Log4net, Log4j, Tracer 等东东来写日 ...

  5. Web Service中的几个重要术语

    WSDL:web service definition language 直译:WebService定义语言 1.对应一种该类型的文件.WSDL 2.定义了Web Service的服务器与客户端应用交 ...

  6. 详解xml文件描述,读取方法以及将对象存放到xml文档中,并按照指定的特征寻找的方案

    主要的几个功能: 1.完成多条Emp信息的XML描述2.读取XML文档解析Emp信息3.将Emp(存放在List中)对象转换为XML文档4.在XML文档中查找指定特征的Emp信息 dom4j,jaxe ...

  7. echo

    echo $echo [-e] [内容字符串]显示后面的内容,缺省选项表示将后面的内容原模原样的显示出来,如果后面接的字符串不用"",会默认以空格为分隔符输出多个串 可以配合She ...

  8. android linker (1) —— __linker_init()

    ilocker:关注 Android 安全(新手) QQ: 2597294287 __linker_init() 在 begin.S 中被调用,并传入两个参数:sp(堆栈指针).#0. linker( ...

  9. multiprocessing module in python(转)

    序.multiprocessing python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程.Python提供了非常好用的多进程包mu ...

  10. java coder的水平

    写java写了也12年了,不决的自己是高手,但是也体会了一些变化.总的来说,Java可以分成几个层次: 首先是需求理解层次,这个层次的coder能理解需求,把需求转化成代码: 第二个层次是单测,能够对 ...