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的区别的更多相关文章

  1. Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别以及ClassPathXmlApplicationContext 的具体路径

    一.ClassPathXmlApplicationContext 的具体路径 String s[] = System.getProperty("java.class.path"). ...

  2. Spring中BeanFactory与FactoryBean的区别

    在Spring中有BeanFactory和FactoryBean这2个接口,从名字来看很相似,比较容易搞混. 一.BeanFactory BeanFactory是一个接口,它是Spring中工厂的顶层 ...

  3. 【Java面试】Spring中 BeanFactory和FactoryBean的区别

    一个工作了六年多的粉丝,胸有成竹的去京东面试. 然后被Spring里面的一个问题卡住,唉,我和他说,6年啦,Spring都没搞明白? 那怎么去让面试官给你通过呢? 这个问题是: Spring中Bean ...

  4. spring中BeanFactory和FactoryBean的区别

    共同点: 都是接口 区别: BeanFactory 以Factory结尾,表示它是一个工厂类,用于管理Bean的一个工厂 在Spring中,所有的Bean都是由BeanFactory(也就是IOC容器 ...

  5. Spring 中 ApplicationContext 和 BeanFactory 的区别,以及 Spring bean 作用域

    //从ApplicationContext 中取 bean ApplicationContext ac = new ClassPathXmlApplicationContext ( "com ...

  6. Spring中BeanFactory与ApplicationContext的区别

    BeanFactory:Bean工厂接口,是访问Spring Bean容器的根接口,基本Bean视图客户端.从其名称上即可看出其功能,即实现Spring Bean容器的读取. ApplicationC ...

  7. Spring中FactoryBean与BeanFactory的区别

    版本:spring-framework-4.1 一概述 BeanFactory 与 FactoryBean的区别, 两个名字很像,面试中也经常遇到,所以容易搞混,现从源码以及示例两方面来分析. 二.源 ...

  8. 【转载】Spring中DispatcherServlet与ContextLoaderListener的区别

    昨天在写springmvc的时候,在web.xml中配置了DispatcherServlet,如下: <servlet> <servlet-name>DispatcherSer ...

  9. Spring中@Component与@Bean的区别

    @Component和@Bean的目的是一样的,都是注册bean到Spring容器中. @Component  VS  @Bean @Component 和 它的子类型(@Controller, @S ...

随机推荐

  1. return & finally 执行顺序 这是我读到的最合理的解释

    新词:return [expression]  栈顶元素 局部变量的快照 java方法是在栈幀中执行,栈幀是线程私有栈的单位,执行方法的线程会为每一个方法分配一小块栈空间来作为该方法执行时的内存空间, ...

  2. win 10安装Linux虚拟机教程

    1.首先下载虚拟机 用的是VMware 官方下载地址:https://my.vmware.com/en/web/vmware/free#desktop_end_user_computing/vmwar ...

  3. php数组合并方法array_merge + 排序array_multisort方法 array_unique数组去重 array_values数组索引值重新从0开始递增

    $dingdan = array_merge($jie_dingdan,$user_dingdan);//数组合并方法 $orderFile = array(); foreach($dingdan a ...

  4. python全栈开发 * 进程理论 进程创建 * 180724

    一.进程理论 1.进程是资源分配的最小单位. 2.进程调度就是多个进程在操作系统的控制下被CPU执行,去享用计算机的资源. 先来先服务 短作业优先 时间片轮转 多级反馈队列 3.进程调度的过程是不能够 ...

  5. 目标检测(四)Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks

    作者:Shaoqing Ren, Kaiming He, Ross Girshick, and Jian Sun SPPnet.Fast R-CNN等目标检测算法已经大幅降低了目标检测网络的运行时间. ...

  6. PTA 复数四则运算

    本题要求编写程序,计算2个复数的和.差.积.商. 输入格式: 输入在一行中按照a1 b1 a2 b2的格式给出2个复数C1=a1+b1i和C2=a2+b2i的实部和虚部.题目保证C2不为0. 输出格式 ...

  7. mysql报错Ignoring the redo log due to missing MLOG_CHECKPOINT between

    mysql报错Ignoring the redo log due to missing MLOG_CHECKPOINT between mysql版本:5.7.19 系统版本:centos7.3 由于 ...

  8. Python时间、日期、时间戳之间的转换

    一.字符串与为时间字符串之间的互相转换 方法:time模块下的strptime方法 a = "2012-11-11 23:40:00" # 字符串转换为时间字符串 import t ...

  9. 关于 Shell 的相关概念和配置方法,全在这儿了!

    使用Linux的过程中少不了使用各种各样的Shell, 而根据启动环境的不同,Shell会读取不同的配置文件.本文便来详细介绍这些不同名字的配置文件在何时会被Shell读取. 什么是 Shell Sh ...

  10. 【Common】NO.81.Note.1.Common.1.001-【各种英文符号的表示及念法】

    1.0.0 Summary Tittle:[Common]NO.81.Note.1.Common.1.001-[各种英文符号的表示及念法] Style:Common Series:Common Sin ...