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,如果无法获得当前线程 ...
随机推荐
- 读书笔记 - js高级程序设计 - 第十章 DOM
文档元素 是文档的最外层元素,在Html页面中,文档元素始终都是<html>元素 在xml中,任何元素都可以是文档元素 Node类型 Node.ELEMENT_NODE 元素 Node ...
- 关于低成本Sub-1Ghz无线射频方案首选:CMT2110A
对于现在的无线遥控市场竞争力极大,不过是在成本方面和性能方面大家都希望能做到精打细算,目前超低功耗Sub-1Ghz 首选方案是CMT2110/17A 芯片具有超低成本,高性能灵活的单芯片OOK发射器, ...
- Pickle incompatibility of numpy arrays between Python 2 and 3
Traceback (most recent call last): File "mnist.py", line 7, in <module> train_set, v ...
- 【Java】Java计时器(秒表)
https://blog.csdn.net/c_jian/article/details/50506759 应用名称:Java计时器 用到的知识:Java GUI编程 开发环境:win8+eclips ...
- 动手动脑 4 String 类
动手动脑1: 请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? 在Java中,内容相同的字串常量(“Hello”)只保存一份以 ...
- Gym - 101142C CodeCoder vs TopForces(搜索)
题意:给定n个人在两个网站上的得分,一个人若能在任意一个网站里战胜另一个人,则认为这个人能战胜那个人.问每个人都能战胜多少人. 分析: 1.战胜具有传递性. 例如: 4 5 2 7 3 3 因为第三个 ...
- c 循环左移
char b[11] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; const int iShift = 4; for (int j = ...
- 五年Java经验,面试还是说不出日志该怎么写更好?——日志规范与最佳实践篇
本文是一个系列,欢迎关注 查看上一篇文章可以扫描文章下方的二维码,点击往期回顾-日志系列即可查看所有相关文章 概览 上一篇我们讨论了为什么要使用日志框架,这次我们深入问题的根源,为什么我们需要日志? ...
- 修改maven默认仓库(即repository)的路径
原文链接:https://blog.csdn.net/ideality_hunter/article/details/53006188 简要说明:主要操作为新建仓库路径,在maven的conf目录下修 ...
- Windows下C extension not loaded for Word2Vec, training will be slow.解决方法
在网上看了好多个博客,都没有很好解决,最后google.. 大概问题就是gensim库在安装时没有和其他一些包关联起来(可能是由于用pip安装的gensim导致这个问题),所以在用Word2Vec时没 ...