Spring加载xml配置文件的方式 ApplicationContext
大家都知道Java读普通文件是通过Basic I/O 中的InputStream、OutStream、Reader、Writer 等实现的。在spring 框架中,它是怎样识别xml这个配置文件的呢?
这就要靠IoC容器的两个接口BeanFactory 和ApplicationContext:
BeanFactory (接口)
|--------XmlBeanFactory(实现类)
ApplicationContext (接口)
|-------- ClassPathXmlApplicationContext(实现类)
|---------FileSystemXmlApplicationContext(实现类)
|---------XmlWebApplicationContext(实现类)
BeanFactory是一个接口,Spring使用它来实例化、配置和管理对象,它里面有一个getBean()方法,只提供了spring最基础的特征,一般使用在低内存的情况下,如Applet。一般的spring项目用它的派生类ApplicationContext,这个类会自动解析我们配置的xml文件,然后根据配置的bean来new对象,将new好的对象放进一个Map中,键是bean的id,值就是new的对象。
1. 编译路径ClassPathXmlApplicationContext来加载xml文件(我们入门篇中的例子http://blog.csdn.net/shymi1991/article/details/48085955 )
这种方式xml文件必须在项目的build path里面,一般用classpath:作为前缀,也可以省略
1)/src目录下
ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:appcontext.xml");
ApplicationContext factory=new ClassPathXmlApplicationContext("appcontext.xml");
2)/ src/conf 目录下的
ApplicationContext factory=new ClassPathXmlApplicationContext("conf/appcontext.xml");
3)同时加载多个xml文件
ApplicationContext factory=new ClassPathXmlApplicationContext(new String[] {"bean1.xml","bean2.xml"});
2.文件系统的路径
xml文件的绝对路径,一般用file:作为前缀,也可以省略
ApplicationContext factory=new FileSystemXmlApplicationContext("file:G:/Test/src/appcontext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("G:/Test/src/appcontext.xml");
3. XmlWebApplicationContext是专为Web工程定制的
ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext );
Spring加载xml配置文件的方式 ApplicationContext的更多相关文章
- Spring加载xml配置文件的方式(BeanFactory和ApplicationContext区别)
描述 大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件 ...
- Spring加载xml配置文件的方式
梳理Spring的流程 xml是最常见的spring 应用系统配置源.Spring中的几种容器都支持使用xml装配bean,包括: XmlBeanFactory,ClassPathXmlApplica ...
- spring加载xml的六种方式
因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装入系统,这就需要利用Spring去动态加载某一位置下的配置文件,所以就总结了下Spring中加载xml配置文件的方式,我总结的有6 ...
- Spring加载XML配置文件
原创链接:http://www.cnblogs.com/yanqin/p/5282929.html(允许转载,但请注明原创链接) BeanFactory加载单个文件 当使用beanfactory去获取 ...
- Spring中加载xml配置文件的六种方式
Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog 因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...
- 使用Spring加载properties配置文件.md
背景 类似于datasource.properties之类的配置文件,最初通过Java的Properties类进行处理.这种方式有许多弊端,如每次都需要读取配置文件:若将Properties作为成员变 ...
- spring加载xml
加载文件顺序 情形一:使用classpath加载且不含通配符 这是最简单的情形,Spring默认会使用当前线程的ClassLoader的getResource方法获取资源的URL,如果无法获得当前线程 ...
- Spring加载XML机制
转载自跳刀的兔子 http://www.cnblogs.com/shipengzhi/articles/3029872.html 加载文件顺序 情形一:使用classpath加载且不含通配符 这是 ...
- spring加载属性配置文件内容
在spring中提供了一个专门加载文件的类PropertyPlaceholderConfigurer,通过这个类我们只需要给定需要加载文件的路径就可以 通过该类加载到项目,但是为了后面在程序中需要使用 ...
随机推荐
- Android实训案例(六)——四大组件之一BroadcastReceiver的基本使用,拨号,短信,SD卡,开机,应用安装卸载监听
Android实训案例(六)--四大组件之一BroadcastReceiver的基本使用,拨号,短信,SD卡,开机,应用安装卸载监听 Android中四大组件的使用时重中之重,我这个阶段也不奢望能把他 ...
- iOS和OS X中的bundle
bundle也可以称之为包(package). 它在iOS和OS X中实际为一个文件夹但却当成单独的文件来对待. 每一个app都有一个bundle,并且你可以通过在xxx.app图标上右击鼠标然后选择 ...
- linux下安装ruby版本管理器RVM
这里以ubuntu为例. 直接以如下命令行安装可能会不成功,因为rvm.io站点有时会被墙: curl -sSL https://get.rvm.io | bash -s stable #或者 cur ...
- 二分算法C实现
#include <stdio.h> #include <stdlib.h> #define NR(x) (sizeof(x)/sizeof(x[0])) int Binary ...
- 安卓笔记--Edittext禁止换行
直接在EditText的xml文件中通过配置android:singleLine="true"把虚拟键盘上的enter键禁止掉,不会显示 <edittext android: ...
- Http的定义及其基本概念介绍
HTTP的特性 HTTP构建于TCP/IP协议之上,默认端口号是80 HTTP是无连接无状态的 HTTP报文 请求报文 HTTP定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST ...
- Java反编译工具(Java Decompiler)
Java Decompiler是一种非常实用的JAVA反编译工具,可以对整个jar包进行反编译,也可以将其集成到eclipse上,非常方便的根据class文件的源码.,官网地址http://jd.be ...
- 面向对象(this的问题一)
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...
- miniui几个常用知识点汇总
1.在表格中去除系统自带的序列号,请看代码: function allAndBrief(id) { if(id==1){ grid.set({ columns: [ { type: "ind ...
- ffmpeg 的 tbr tbc 和 tbn的意义
tbn = the time base in AVStream that has come from the container tbc = the time base in AVCodecConte ...