先说: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. oracle带条件的Insert语句

    背景 在一条记录完结时,自动向表中加入一条新的记录,采用的是事务处理,修改现有记录,并新增一条记录,直接采用的insert语句会报错 //主键冲突 unique constraint (XXXXXX) ...

  2. mysql原理以及相关优化

    说起MySQL的查询优化,相信大家积累一堆技巧:不能使用SELECT *.不使用NULL字段.合理创建索引.为字段选择合适的数据类型..... 你是否真的理解这些优化技巧?是否理解其背后的工作原理?在 ...

  3. MvcMusicStore学习中常出现的一个BUG

    BUG描述:var genreModel = storeDB.Genres.Include("Albums").Single(g => g.Name == genre); 前 ...

  4. windows下自己常用的几个bat

    1.samba映射盘符和解除 net use Z: "\\sambaserver ip\dir" "password" /user:"username ...

  5. PAT 1087 有多少不同的值

    https://pintia.cn/problem-sets/994805260223102976/problems/1038429191091781632 当自然数 n 依次取 1.2.3.…….N ...

  6. android中常见的命名及其特点详解

    Paseal命名法 Paseal命名法特点:String MyName-DelphiInt MyAge每个单词首字母大写 Camel命名法 Camel(驼峰的意思)命名法特点:String myNam ...

  7. 【BZOJ 4103】 [Thu Summer Camp 2015]异或运算 可持久化01Trie

    我们观察数据:树套树 PASS    主席树 PASS  一层一个Trie PASS 再看,异或!我们就把目光暂时定在01Tire然后我们发现,我们可以带着一堆点在01Trie上行走,因为O(n*q* ...

  8. Linux上 Can't connect to X11 window server using XX as the value of the DISPLAY 错误解决方法

    在Linux上运行需要图形界面的程序时出现如下错误提示: No protocol specified Exception in thread "main" java.awt.AWT ...

  9. 安卓sdk安装教程

    http://blog.csdn.net/love4399/article/details/77164500

  10. Nginx support TCP Load balance

    1. Install nginx package 2. edit nginx configuration file [root@ip- nginx]# more nginx.conf user ngi ...