Spring加载xml配置文件的方式
梳理Spring的流程
xml是最常见的spring 应用系统配置源。Spring中的几种容器都支持使用xml装配bean,包括:
XmlBeanFactory,ClassPathXmlApplicationContext,FileSystemXmlApplicationContext,XmlWebApplicationContext
一: XmlBeanFactory 引用资源
Resource resource = new ClassPathResource("appcontext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
二: ClassPathXmlApplicationContext 编译路径
ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:appcontext.xml");
// src目录下的
ApplicationContext factory=new ClassPathXmlApplicationContext("appcontext.xml");
ApplicationContext factory=new ClassPathXmlApplicationContext(new String[] {"bean1.xml","bean2.xml"});
// src/conf 目录下的
ApplicationContext factory=new ClassPathXmlApplicationContext("conf/appcontext.xml");
ApplicationContext factory=new ClassPathXmlApplicationContext("file:G:/Test/src/appcontext.xml");
三: 用文件系统的路径
ApplicationContext factory=new FileSystemXmlApplicationContext("src/appcontext.xml");
//使用了 classpath: 前缀,作为标志, 这样,FileSystemXmlApplicationContext 也能够读入classpath下的相对路径
ApplicationContext factory=new FileSystemXmlApplicationContext("classpath:appcontext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("file:G:/Test/src/appcontext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("G:/Test/src/appcontext.xml");
四: XmlWebApplicationContext是专为Web工程定制的。
ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext );
五: 使用BeanFactory
BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource("bean1.xml"));
reader.loadBeanDefinitions(new ClassPathResource("bean2.xml"));
BeanFactory bf=(BeanFactory)reg;
六:Web 应用启动时加载多个配置文件
通过ContextLoaderListener 也可加载多个配置文件,在web.xml文件中利用
<context-pararn>元素来指定多个配置文件位置,其配置如下:
- <context-param>
- <!-- Context Configuration locations for Spring XML files -->
- <param-name>contextConfigLocation</param-name>
- <param-value>
- ./WEB-INF/**/Appserver-resources.xml,
- classpath:config/aer/aerContext.xml,
- classpath:org/codehaus/xfire/spring/xfire.xml,
- ./WEB-INF/**/*.spring.xml
- </param-value>
- </context-param>
这个方法加载配置文件的前提是已经知道配置文件在哪里,虽然可以利用“*”通配符,但灵活度有限。
http://javapub.iteye.com/blog/751772
Spring加载xml配置文件的方式的更多相关文章
- Spring加载xml配置文件的方式 ApplicationContext
大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件的呢? ...
- Spring加载xml配置文件的方式(BeanFactory和ApplicationContext区别)
描述 大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件 ...
- 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加载属性配置文件内容
在spring中提供了一个专门加载文件的类PropertyPlaceholderConfigurer,通过这个类我们只需要给定需要加载文件的路径就可以 通过该类加载到项目,但是为了后面在程序中需要使用 ...
- Spring加载Properties配置文件的三种方式
一.通过 context:property-placeholder 标签实现配置文件加载 1) 用法: 1.在spring.xml配置文件中添加标签 <context:property-plac ...
- spring加载xml
加载文件顺序 情形一:使用classpath加载且不含通配符 这是最简单的情形,Spring默认会使用当前线程的ClassLoader的getResource方法获取资源的URL,如果无法获得当前线程 ...
随机推荐
- Linux系统学习笔记
6.启动python cd /root/yq/v4_1_6309a_btc_nw_cq nohup python3 -u v4_1_6309a_btc_nw_cq.py >> 6309a_ ...
- java初学小项目-酒店客房管理系统
最近初次接触JAVA,感觉之前学的C语言很有用,跟着视频做了一个小项目-酒店客房管理系统 /* 酒店客房管理系统 */ import java.util.Scanner;//通过键盘来输入命令需要的引 ...
- JS - 解决引入 js 文件无效的问题
增加 type 即可 <script type="text/javascript" src="....js"></script>
- 剑指offer_2.1_Day_5
输入一个链表,按链表从尾到头的顺序返回一个ArrayList. import java.util.ArrayList; public class Solution { public ArrayList ...
- torch.Tensor文档学习笔记
A torch.Tensor is a multi-dimensional matrix containing elements of a single data type. 张量(torch.Ten ...
- Json字符串转Java对象和List集合
对象POJO和JSON互转 public class JsonUtil { /** * JSON 转 POJO */ public static <T> T getObject(Strin ...
- VBE2019的下载、安装和使用(最新版2020.2.22)
VBE2019可用于XP系统.Windows 7和Windows 10的32位.64位Office对应的VBA环境 安装包下载地址:VBE2019-Setup.zip 下载后解压缩,直接双击安装(请勿 ...
- mysql 去除重复 Select中DISTINCT关键字的用法(查询两列,只去掉重复的一列)
在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的 ...
- 流程引擎表单引擎的常见问题技术交流-关于广州xx公司对驰骋BPM提出
第1章: 先使用.net 再使用java,数据迁移问题?会存在哪些问题. RE: .net 版本的ccflow与java版本的jflow系列版本都是一个数据库结构,一个操作手册,流程模版,表单模版通用 ...
- iOS部分页面横屏显示
在iOS系统支持横屏顺序默认读取plist里面设置的方向(优先级最高)等同于Xcode Geneal设置里面勾选application window设置的级别次之 然后是UINavigationcon ...