因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装入系统,这就需要利用Spring去动态加载某一位置下的配置文件,所以就总结了下Spring中加载xml配置文件的方式,我总结的有6种, 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>元素来指定多个配置文件位置,其配置如下:

  1. <context-param>
  2. <!-- Context Configuration locations for Spring XML files -->
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>
  5. ./WEB-INF/**/Appserver-resources.xml,
  6. classpath:config/aer/aerContext.xml,
  7. classpath:org/codehaus/xfire/spring/xfire.xml,
  8. ./WEB-INF/**/*.spring.xml
  9. </param-value>
  10. </context-param>

这个方法加载配置文件的前提是已经知道配置文件在哪里,虽然可以利用“*”通配符,但灵活度有限。 

Spring中加载xml配置文件的六种方式的更多相关文章

  1. Spring中加载xml配置文件的常用的几种方式

    https://blog.csdn.net/qq877507054/article/details/62432062

  2. spring BeanFactory加载xml配置文件示例

    项目目录结构如下: HelloWorld.java package com.thief.demo; public class HelloWorld { public void sayHello() { ...

  3. Spring中加载配置文件的方式

    原文:http://blog.csdn.net/snowjlz/article/details/8158560 Spring 中加载XML配置文件的方式,好像有3种, XML是最常见的Spring 应 ...

  4. Spring中加载ApplicationContext.xml文件的方式

    Spring中加载ApplicationContext.xml文件的方式 原文:http://blog.csdn.net/snowjlz/article/details/8158560 1.利用Cla ...

  5. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)

    接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...

  6. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(一)

    Android ViewPager 中加载 Fragmenet的两种方式 一.当fragment里面的内容较少时,直接 使用fragment xml布局文件填充 文件总数 布局文件:view_one. ...

  7. Spring加载xml配置文件的方式 ApplicationContext

    大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件的呢? ...

  8. Spring加载XML配置文件

    原创链接:http://www.cnblogs.com/yanqin/p/5282929.html(允许转载,但请注明原创链接) BeanFactory加载单个文件 当使用beanfactory去获取 ...

  9. Spring加载xml配置文件的方式(BeanFactory和ApplicationContext区别)

    描述 大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件 ...

随机推荐

  1. 回到顶端js实现

    function goTop(){ var _btn = document.getElementById("goTop"); if (document.documentElemen ...

  2. Bean 的作用域

    •在 Spring 中, 可以在 <bean> 元素的 scope 属性里设置 Bean 的作用域. 默认情况下, Spring 只为每个在 IOC 容器里声明的 Bean 创建唯一一个实 ...

  3. python 学习笔记12(序列常用方法总结)

    http://www.cnblogs.com/vamei/archive/2012/07/19/2599940.html 多回想!!! 1. 序列(list,tuple,string) len(s) ...

  4. BZOJ 4245: [ONTAK2015]OR-XOR

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 492  Solved: 269[Submit][Sta ...

  5. Leetcode 160. Intersection of two linked lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  6. macOS 安装 wget

    适用于macOS Sierra Apple Store下载安装Xcode 安装Homebrew包管理,类似于Ubuntu下的apt-get: 终端下输入 ruby -e "$(curl -f ...

  7. Bzoj2007 [Noi2010]海拔

    Time Limit: 20 Sec  Memory Limit: 552 MB Submit: 2380  Solved: 1130 Description YT市是一个规划良好的城市,城市被东西向 ...

  8. SPOJ GSS1 Can you answer these queries I

    Time Limit: 115MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description You are g ...

  9. centos虚拟机克隆

    vmware vsphere平台上克隆centos6 一.删掉/etc/udev/rules.d/70-persistent-net.rules文件 reboot 二.vim ifcfg-eth0 删 ...

  10. 安卓开发30:AsyncTask的用法

    http://blog.csdn.net/initphp/article/details/10392093 安卓开发笔记系列(43)  在开发Android应用时必须遵守单线程模型的原则: Andro ...