.前面讲解的都是通过直接读取配置文件,进行的实例化ApplicationContext

AbstractApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");

下面讲解直接通过配置文件进行初始化。

2.web.xml

  <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

这样,ApplicationContext便已经实例化了,默认就直接加载了beans.xml里面的内容。

来看看底层的代码,类ContextLoaderListener中有个初始化方法

    public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
if (this.contextLoader == null) {
this.contextLoader = this;
}
this.contextLoader.initWebApplicationContext(event.getServletContext());
}

进入initWebApplicationContext方法 :

            ApplicationContext parent = loadParentContext(servletContext);

            // Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
this.context = createWebApplicationContext(servletContext, parent);

这句也就是容器加载的结果。

1和2一个是java代码一个是xml代码,不过实现的效果都是一样的。

java Spring 在WEB应用中的实例化的更多相关文章

  1. 重新学习Spring一--Spring在web项目中的启动过程

    1 Spring 在web项目中的启动过程 Spring简介 Spring 最简单的功能就是创建对象和管理这些对象间的依赖关系,实现高内聚.低耦合.(高内聚:相关性很强的代码组成,既单一责任原则:低耦 ...

  2. J2EE进阶(五)Spring在web.xml中的配置

     J2EE进阶(五)Spring在web.xml中的配置 前言 在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制自动加载到容器中.在web ...

  3. 使用Spring时web.xml中的配置

    使用Spring时web.xml中的配置: <?xml version="1.0" encoding="UTF-8"?> <web-app x ...

  4. Spring在Web应用中使用的原理

    那Spring如何在web应用中使用 ①加入Spring web应用的特定jar包spring-web-4.0.0.RELEASE.jar.spring-webmvc-4.0.0.RELEASE.ja ...

  5. 第三章 Maven构建 Java Spring Boot Web项目

    3.1   认识Srping Boot Spring Boot是一个框架,是一种全新的编程规范,它的产生简化了对框架的使用,简化了Spring众多的框架中大量的繁琐的配置文件,所以说Spring Bo ...

  6. Spring 在web 容器中的启动过程

    1.对于一个web 应用,其部署在web 容器中,web 容器提供其一个全局的上下文环境,这个上下文就是 ServletContext ,其后面的spring IoC 容器提供宿主环境 2.在web. ...

  7. Spring在Web项目中的三种启动加载的配置

    在最近的项目中,使用到了spring相关的很多东西,有点把spring的配置给搞混了,从网上查到的资料以及整理了一下. 在Web项目中,启动spring容器的方式有三种,ContextLoaderLi ...

  8. Spring在web开发中的应用

    (1)在 web 项目中要使用 spring 需要导入一个 jar 包: spring-web-4.2.4.jar包 (2)在 web.xml 文件中配置 Listener <listener& ...

  9. spring在web.xml中的配置

    在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制,自动加载的容器中去,在web项目中,配置文件加载到web容器中进行解析,目前,sprin ...

随机推荐

  1. scp linux远程拷贝和本地拷贝命令

    linux远程拷贝和本地拷贝命令   一.linux对linux 远程拷贝   scp命令    scp 文件名  root@远程ip:/路径/    将本地home目录下的test.tar的文件拷贝 ...

  2. C# using Sendkey function to send a key to another application

    If notepad is already started, you should write: // import the function in your class [DllImport (&q ...

  3. Prism - WPF MVVM(Model-View-ViewModel)设计模式【学习】

    开发工具: VS2010 Blend Prism框架 基本概念: 数据绑定,依赖属性,依赖对象 WPF 委托式命令 Icommand接口 Lambda表达式 MVVM(Model-View-ViewM ...

  4. PC-删除共享[绝对够狠的方法]

    1.批处理清除法-------------------------------------------------------------------------------------------- ...

  5. Oracle数据库字符串连接方法

    转至:http://database.51cto.com/art/201011/232267.htm 和其他数据库系统类似,Oracle字符串连接使用“||”进行字符串拼接,其使用方式和MSSQLSe ...

  6. Zabbix lld发现磁盘监控

    一.软件版本 操作系统:CentOS-6.5-x86_64 zabbix版本:3.0.3 二.脚本编写: 1.python版本: #!/usr/bin/env python import json i ...

  7. css Spirtes 错位问题解决

    本文为转载内容,原文链接: https://github.com/banricho/webLog/issues/1

  8. Jquery常用方法篇(一)

    1.add方法:将匹配的元素添加到当前的jquery对象集合中. 语法 add(expr,[context]) 参数1表示 用于匹配元素选择器(待添加的对象),或者用于动态生成的HTML代码(字符串) ...

  9. NOIopenjudge 407:Heritage

    总时间限制:  5000ms 内存限制:  65536kB 描述 Your rich uncle died recently, and the heritage needs to be divided ...

  10. 【面试虐菜】—— Oracle中CHAR、VARCHAR的区别

    1.CHAR. CHAR存储定长数据很方便,CHAR字段上的索引效率级高,比如定义char(10),那么不论你存储的数据是否达到了10个字节,都要占去10个字节的空间,不足的自动用空格填充,所以在读取 ...