java Spring 在WEB应用中的实例化
.前面讲解的都是通过直接读取配置文件,进行的实例化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应用中的实例化的更多相关文章
- 重新学习Spring一--Spring在web项目中的启动过程
1 Spring 在web项目中的启动过程 Spring简介 Spring 最简单的功能就是创建对象和管理这些对象间的依赖关系,实现高内聚.低耦合.(高内聚:相关性很强的代码组成,既单一责任原则:低耦 ...
- J2EE进阶(五)Spring在web.xml中的配置
J2EE进阶(五)Spring在web.xml中的配置 前言 在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制自动加载到容器中.在web ...
- 使用Spring时web.xml中的配置
使用Spring时web.xml中的配置: <?xml version="1.0" encoding="UTF-8"?> <web-app x ...
- Spring在Web应用中使用的原理
那Spring如何在web应用中使用 ①加入Spring web应用的特定jar包spring-web-4.0.0.RELEASE.jar.spring-webmvc-4.0.0.RELEASE.ja ...
- 第三章 Maven构建 Java Spring Boot Web项目
3.1 认识Srping Boot Spring Boot是一个框架,是一种全新的编程规范,它的产生简化了对框架的使用,简化了Spring众多的框架中大量的繁琐的配置文件,所以说Spring Bo ...
- Spring 在web 容器中的启动过程
1.对于一个web 应用,其部署在web 容器中,web 容器提供其一个全局的上下文环境,这个上下文就是 ServletContext ,其后面的spring IoC 容器提供宿主环境 2.在web. ...
- Spring在Web项目中的三种启动加载的配置
在最近的项目中,使用到了spring相关的很多东西,有点把spring的配置给搞混了,从网上查到的资料以及整理了一下. 在Web项目中,启动spring容器的方式有三种,ContextLoaderLi ...
- Spring在web开发中的应用
(1)在 web 项目中要使用 spring 需要导入一个 jar 包: spring-web-4.2.4.jar包 (2)在 web.xml 文件中配置 Listener <listener& ...
- spring在web.xml中的配置
在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制,自动加载的容器中去,在web项目中,配置文件加载到web容器中进行解析,目前,sprin ...
随机推荐
- UIScrollView,UIView转换UIImage代码(整个view截图, 不只是可视区域)
-(UIImage*)captureView:(UIView *)theView{ CGRect rect = theView.frame; if ([theView isKindOf ...
- ASP.NET MVC 開發心得分享 (21):Routing 觀念與技巧
ASP.NET MVC 預設在 Global.asax 所定義的 RegisterRoutes 方法中可以輕易的定義你希望擁有的網址格式,嚴格上來講這並非 ASP.NET MVC 的專利,而是從 AS ...
- EF RepositoryBase 参考示例【转】
1.定义泛型类 namespace Crm.Data.Logic.Repository{ public abstract class AbstractRepository<TC, T> ...
- base64dll
继上次的dll学习后,想开发个软件,连接到百度的云存储服务器,上传文件.发现要算秘钥,在网上找了到了hmac-sha1,base64的源码,发现有些是c++写的,有些是c写的一起写到一个文件里有些麻烦 ...
- HNU 13375 Flowery Trails (spfa最短路)
求最短路径覆盖的全部边权值和. 思路:分别从起点和终点两次求最短路,再比较两个点到起点的距离和他们之间的权值相加和是否等于最短路径. 这题很好 #include <cstring> #in ...
- POJ3750: 小孩报数问题+一道经典约瑟夫问题(猴子选大王)
又一次因为一个小错误,POJ上Wrong Answer了无数次..... 在差不多要放弃的时候,发现了这个猥琐的不能再猥琐的bug,改完了提交就AC了,简直无语.... 本题wo采用模拟方法: 1 # ...
- java 关于多态的一点总结
http://www.cnblogs.com/wenruo/p/5352683.html 一直不是很理解多态,今天看了两遍<think in java>第八章,试着总结一下. 多态的本质就 ...
- hdoj 1241 Oil Deposits
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- (二)如何在.net中使用Redis
Step1:使用NuGet工具安装Redis C# API,这里有多个API我们可以使用其中一个:
- Python 3. 里filter与generator expression的区别
# -*- coding: utf-8 -*- """ A test to show the difference between filter and genrator ...