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

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

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

2.web.xml

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>classpath:beans.xml</param-value>
  4. </context-param>
  5.  
  6. <listener>
  7. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  8. </listener>

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

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

  1. public void contextInitialized(ServletContextEvent event) {
  2. this.contextLoader = createContextLoader();
  3. if (this.contextLoader == null) {
  4. this.contextLoader = this;
  5. }
  6. this.contextLoader.initWebApplicationContext(event.getServletContext());
  7. }

进入initWebApplicationContext方法 :

  1. ApplicationContext parent = loadParentContext(servletContext);
  2.  
  3. // Store context in local instance variable, to guarantee that
  4. // it is available on ServletContext shutdown.
  5. 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. UIScrollView,UIView转换UIImage代码(整个view截图, 不只是可视区域)

    -(UIImage*)captureView:(UIView *)theView{     CGRect rect = theView.frame;     if ([theView isKindOf ...

  2. ASP.NET MVC 開發心得分享 (21):Routing 觀念與技巧

    ASP.NET MVC 預設在 Global.asax 所定義的 RegisterRoutes 方法中可以輕易的定義你希望擁有的網址格式,嚴格上來講這並非 ASP.NET MVC 的專利,而是從 AS ...

  3. EF RepositoryBase 参考示例【转】

    1.定义泛型类 namespace Crm.Data.Logic.Repository{    public abstract class AbstractRepository<TC, T> ...

  4. base64dll

    继上次的dll学习后,想开发个软件,连接到百度的云存储服务器,上传文件.发现要算秘钥,在网上找了到了hmac-sha1,base64的源码,发现有些是c++写的,有些是c写的一起写到一个文件里有些麻烦 ...

  5. HNU 13375 Flowery Trails (spfa最短路)

    求最短路径覆盖的全部边权值和. 思路:分别从起点和终点两次求最短路,再比较两个点到起点的距离和他们之间的权值相加和是否等于最短路径. 这题很好 #include <cstring> #in ...

  6. POJ3750: 小孩报数问题+一道经典约瑟夫问题(猴子选大王)

    又一次因为一个小错误,POJ上Wrong Answer了无数次..... 在差不多要放弃的时候,发现了这个猥琐的不能再猥琐的bug,改完了提交就AC了,简直无语.... 本题wo采用模拟方法: 1 # ...

  7. java 关于多态的一点总结

    http://www.cnblogs.com/wenruo/p/5352683.html 一直不是很理解多态,今天看了两遍<think in java>第八章,试着总结一下. 多态的本质就 ...

  8. hdoj 1241 Oil Deposits

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  9. (二)如何在.net中使用Redis

    Step1:使用NuGet工具安装Redis C# API,这里有多个API我们可以使用其中一个:

  10. Python 3. 里filter与generator expression的区别

    # -*- coding: utf-8 -*- """ A test to show the difference between filter and genrator ...