Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别
Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别
一、概述
在项目中遇到加载不到Spring配置文件,简单分析后,写此文备忘!
二、测试所需资源
TestBean.java
public class TestBean {
public TestBean(){
System.out.println(this.getClass().getName().concat(" init !"));
}
public String getTestStr() {
return "testStr";
}
}
applicationContext.xml
<bean id="testBean" class="com.bean.TestBean" />
二、区别
2.1 ClassPathXmlApplicationContext使用方法
ClassPathXmlApplicationContext 默认会去 classPath 路径下找。classPath 路径指的就是编译后的 classes 目录。
示例:
@Test
public void testBean(){
//单配置文件方式一
BeanFactory beanFactory=new ClassPathXmlApplicationContext("applicationContext.xml");
//单配置文件方式二
BeanFactory beanFactory=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//多个配置文件
BeanFactory beanFactory=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
//绝对路径需加“file:”前缀
BeanFactory beanFactory = new ClassPathXmlApplicationContext("file:E:\Workspace\idea_workspace\spring\springtest\src\main\resources\applicationContext.xml");
TestBean bean= (TestBean) beanFactory.getBean("testBean");
assertEquals("testStr",bean.getTestStr());
}
运行示例你会发现 “classpath:” 是可以缺省的。
如果是绝对路径,就需要加上 “file:” 前缀,不可缺省。
2.2 FileSystemXmlApplicationContext使用方法
FileSystemXmlApplicationContext 默认是去项目的路径下加载,可以是相对路径,也可以是绝对路径,若是绝对路径,“file:” 前缀可以缺省。
示例:
@Test
public void testBean(){
//classes目录
BeanFactory beanFactory=new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
//项目路径相对路径
BeanFactory beanFactory=new FileSystemXmlApplicationContext("src\\main\\resources\\applicationContext.xml");
//多配置文件
BeanFactory beanFactory=new FileSystemXmlApplicationContext(new String[]{"src\\main\\resources\\applicationContext.xml"});
//绝对目录
BeanFactory beanFactory=new FileSystemXmlApplicationContext(new String[]{"E:\\Workspace\\idea_workspace\\spring\\springtest\\src\\main\\resources\\applicationContext.xml"});
TestBean bean= (TestBean) beanFactory.getBean("testBean");
assertEquals("testStr",bean.getTestStr());
}
Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别的更多相关文章
- Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别以及ClassPathXmlApplicationContext 的具体路径
一.ClassPathXmlApplicationContext 的具体路径 String s[] = System.getProperty("java.class.path"). ...
- Spring中BeanFactory与FactoryBean的区别
在Spring中有BeanFactory和FactoryBean这2个接口,从名字来看很相似,比较容易搞混. 一.BeanFactory BeanFactory是一个接口,它是Spring中工厂的顶层 ...
- 【Java面试】Spring中 BeanFactory和FactoryBean的区别
一个工作了六年多的粉丝,胸有成竹的去京东面试. 然后被Spring里面的一个问题卡住,唉,我和他说,6年啦,Spring都没搞明白? 那怎么去让面试官给你通过呢? 这个问题是: Spring中Bean ...
- spring中BeanFactory和FactoryBean的区别
共同点: 都是接口 区别: BeanFactory 以Factory结尾,表示它是一个工厂类,用于管理Bean的一个工厂 在Spring中,所有的Bean都是由BeanFactory(也就是IOC容器 ...
- Spring 中 ApplicationContext 和 BeanFactory 的区别,以及 Spring bean 作用域
//从ApplicationContext 中取 bean ApplicationContext ac = new ClassPathXmlApplicationContext ( "com ...
- Spring中BeanFactory与ApplicationContext的区别
BeanFactory:Bean工厂接口,是访问Spring Bean容器的根接口,基本Bean视图客户端.从其名称上即可看出其功能,即实现Spring Bean容器的读取. ApplicationC ...
- Spring中FactoryBean与BeanFactory的区别
版本:spring-framework-4.1 一概述 BeanFactory 与 FactoryBean的区别, 两个名字很像,面试中也经常遇到,所以容易搞混,现从源码以及示例两方面来分析. 二.源 ...
- 【转载】Spring中DispatcherServlet与ContextLoaderListener的区别
昨天在写springmvc的时候,在web.xml中配置了DispatcherServlet,如下: <servlet> <servlet-name>DispatcherSer ...
- Spring中@Component与@Bean的区别
@Component和@Bean的目的是一样的,都是注册bean到Spring容器中. @Component VS @Bean @Component 和 它的子类型(@Controller, @S ...
随机推荐
- Linux 下安装mysql 5.7
Linux 下安装mysql 5.7 本人首次安装时按照菜鸟教程的步骤一步一步来的,结果意外的是 装成5.6了,而且各种无厘头的问题,例如无法启动... 本文参照 大佬:‘这个名字想了很久~’ 的&l ...
- python的基础1
1.换行需要多个引号,'''''' """"""Eg:print('''文能提笔安天下,# 武能上马定乾坤.# 心存谋略和人生,# 古今银熊 ...
- ext 的loadmask 与ajax的同步请求水火不容
由于ajax 的同步请求会有一段请求时间.有的短.有的长,对于短的我们还是能接受的,不过长的话就必须处理一下了, 就比如处于ext 4.2.0的框架下,需要一个遮掩的样式,框架是有自带的,loadma ...
- 跑 vue 项目
cd ***** npm install 或 yarn(推荐) npm run dev 若是报错: missing script: dev ERR! A complete log of this r ...
- 初始Redis
一.NoSql数据库(Not Only Sql): 1.又称为非关系型数据库,主流的NoSql数据库有: Redis,Hbase,MongoDB 2.NoSql产生的背景: 1.1.数据库的高并发的读 ...
- FQ原理
笔者在nginx反向代理篇讲了正向代理和反向代理的区别,今天着重讲其中的FQ是实现原理. 一.普遍的两种方式 1.vpn vpn它将客户端的IP数据报经过加密和二次封装后转发出去,客户端通过vpn上网 ...
- DOS的重定向命令及在安全方面的应用
dos的重定向命令 2006-10-15 16:47 新手DOS应用技巧人小鬼大 重定向命令在安全方面的应用来源:ChinaITLab收集整理2005-7-21 11:12:00 大家知道,DOS下有 ...
- python获取一年所有的日期
python获取一年所有的日期 自动识别闰年. import arrow def isLeapYear(years): ''' 通过判断闰年,获取年份years下一年的总天数 :param years ...
- echart 判断数据是否为空
formatter 判断数据是否为空
- 【JVM】-NO.116.JVM.1 -【JDK11 HashMap详解-5-红黑树】
Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...