先说:ClassPathXmlApplicationContext 
这个类,默认获取的是WEB-INF/classes/下的路径,也就是在myeclipse的src下的路径,所以用这个是获取不到WEB-INF下的配置文件的...

下面是几个用法,引src下的一个文件,有以下几种方式

BeanFactory ac = new ClassPathXmlApplicationContext("applicationContext.xml");
BeanFactory ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
BeanFactory ac = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
ac.getBean("beanID");

classpath:和classpath*:的区别: 
classpath:   只能加载一个配置文件,如果配置了多个,则只加载第一个 
classpath*:  可以加载多个配置文件,如果有多个配置文件,就用这个

BeanFactory ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","applicationContext-test.xml"});
BeanFactory ac = new ClassPathXmlApplicationContext("classpath*:/applicationContext*.xml");
BeanFactory ac = new ClassPathXmlApplicationContext("classpath*:src/folder/applicationContext*.xml");

再说:FileSystemXmlApplicationContext 
这个类,默认获取的是项目路径,就是项目名的这级.但是我测试,在myeclipse下,执行main方法,获取配置文件,路径用WebRoot/WEB-INF/是没问题的,但是发布到tomcat下,就怎么配都不成功了,无奈,只能把配置文件全部移到src下了,因为担心发布到linux上后,又出现路径问题

下面是几个用法,引WEB-INF下的一个文件,有以下几种方式

BeanFactory factory = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
BeanFactory factory = new FileSystemXmlApplicationContext("file:D:/workspace/testproject/WebRoot/WEB-INF/applicationContext.xml");

其中,FileSystemXmlApplicationContext的起始路径是项目路径,所以可以直接这样写WebRoot/WEB-INF 
如果前边加了file:则说明后边的路径就要写全路径了,就是绝对路径

下面是几个用法,引WEB-INF/classes下的一个文件,有以下几种方式

BeanFactory factory = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");  

这其实跟ClassPathXmlApplicationContext是一样的,多个就加通配符
下面是几个用法,引WEB-INF下的多个文件,有以下几种方式

BeanFactory factory = new FileSystemXmlApplicationContext(new String[]{"WebRoot/WEB-INF/applicationContext.xml","WebRoot/WEB-INF/applicationContext-test.xml"});
BeanFactory factory = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext*.xml");
BeanFactory factory = new FileSystemXmlApplicationContext("file:D:/workspace/testproject/WebRoot/WEB-INF/applicationContext*.xml");

引WEB-INF/classes下的多个文件,也是一样的

BeanFactory factory = new FileSystemXmlApplicationContext(new String[]{"classpath:applicationContext.xml","classpath:applicationContext*.xml"});
BeanFactory factory = new FileSystemXmlApplicationContext("classpath*:applicationContext*.xml");

Spring ClassPathXmlApplicationContext和FileSystemXmlApplicationContext读取配置文件的方法的更多相关文章

  1. Spring Boot配置,读取配置文件

    Spring Boot配置,读取配置文件 一.配置Spring Boot 1.1 服务器配置 1.2 使用其他Web服务器 1.3 配置启动信息 1.4 配置浏览器显示ico 1.5 Yaml语法 1 ...

  2. spring boot使用java读取配置文件,DateSource测试,BomCP测试,AnnotationConfigApplicationContext的DataSource注入

    一.配置注解读取配置文件         (1)@PropertySource可以指定读取的配置文件,通过@Value注解获取值   实例:           @PropertySource(val ...

  3. 面试突击75:SpringBoot 有几种读取配置文件的方法?

    Spring Boot 中读取配置文件有以下 5 种方法: 使用 @Value 读取配置文件. 使用 @ConfigurationProperties 读取配置文件. 使用 Environment 读 ...

  4. Shell读取配置文件的方法

    参考:http://www.cnblogs.com/binbinjx/p/5680214.html 做批量软件安装自动化时,都喜欢用配置文件的方式改变参数,那怎么通过shell读取配置文件的配置呢?参 ...

  5. Log4j 2.0读取配置文件的方法

    log4j中配置日志文件存放的位置不一定在src下面,即根目录下.这个时候我们需要解决如何加载配置文件的问题.在log4j1.x中解决的方法就比较多了.如:PropertyConfigurator.c ...

  6. java中读取配置文件的方法

    转自:http://blog.csdn.net/stypace/article/details/38414871 一.使用org.apache.commons.configuration 需要使用的是 ...

  7. Spring编译后没有xml配置文件解决方法

    问题描述 在使用Maven来构建Spring项目的时候,使用下面代码来读取Spring配置文件. ClassPathXmlApplicationContext context = new ClassP ...

  8. Spring ClassPathXmlApplicationContext和FileSystemXmlApplicationContext

    先说:ClassPathXmlApplicationContext 这个类,默认获取的是WEB-INF/classes/下的路径,也就是在myeclipse的src下的路径,所以用这个是获取不到WEB ...

  9. shell 读取配置文件的方法

    原文地址:http://bbs.chinaunix.net/thread-3628456-1-1.html 总结地址:https://www.cnblogs.com/binbinjx/p/568021 ...

随机推荐

  1. 基于phonegap,html5,ratchet,handlebars等技术的微表情APP

    该app是由很多有意思的微表情构成的,支持40种表情,并且每种表情都有不同的状态,主要有搜索表情,分享表情,摇一摇换表情等功能.目前只支持安卓版.由前期构思,到技术选型,到界面设计,到编码测试,再到发 ...

  2. LTE:上行调度请求(Scheduling Request,SR) LTE:下行资源分配类型

    http://blog.sina.com.cn/s/blog_927cff010101a7yh.html 上行调度请求(Scheduling Request,SR) 如果UE没有上行数据要传输,eNo ...

  3. [Leetcode] subsets 求数组所有的子集

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  4. Maven如何打包本地依赖包

    有的jar包,在maven中心库里面是没有的,那么,如何在项目中使用呢? 假设我们需要使用:apache-ant-zip-2.3.jar 将该jar包,放在项目的lib目录,例如: 在pom.xml里 ...

  5. Working with large data sets in MySQL

    What does working with large data sets in mySQL teach you ? Of course you have to learn a lot about ...

  6. Educational Codeforces Round 11 A

    A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  7. eclipse集成mybatis的generater插件

    mybatis也能方向生成代码,能方向生成实体类(po).mapper接口和Mapper接口映射文件,能减少我们代码的工作量.详细步骤如下 1.下载mybatis生成架包工具MyBatis_Gener ...

  8. php函数-shuffle

    Shuffle()函数说明: -随机乱序现有数组并不保留键值: -shuffle()函数把数组中的元素按随机顺序重新排列,该函数为数组中的元素分配新的键名,已有键名将被删除. 语法说明: shuffl ...

  9. WebView使用--文章集锦

    对于android WebView加载不出Html5网页的解决方法 在android4.4中webview的使用相对于之前版本的一些区别 理解WebKit和Chromium: Android 4.4 ...

  10. python最简单发送邮件

    #!/usr/bin/env python #coding:utf8 #Author:lsp #Date:下午5:51:13 #Version:0.1 #Function: #导入smtplib和MI ...