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. nginx获取uri里面的参数

    add_header Content-Disposition "attachment;fileName=$arg_filename"; 请求连接为:10.26.1.165/abc? ...

  2. ASP.NET页面使用JQuery EasyUI生成Dialog后台取值为空

    原因: JQuery EasyUI生成Dialog后原来的文档结构发生了变化,原本在form里的内容被移动form外面,提交到后台后就没有办法取值了. 解决办法: 在生成Dialog后将它append ...

  3. 如何将PDF文件转Word,有什么方法

    PDF文件怎样转换成Word呢?在现在的日常办公中PDF文件和Word文件都是办公必不可少的两种文件格式了.那么当我们在工作中需要对这两种文件进行转换时,我们应该怎样实现呢?下面我们就一起来看一下吧. ...

  4. P1_jemeter安装--jdk安装

    学习的python,需要下载jemter做接口测试. 一..jMeter介绍 Apache组织开发的基于JAVA压力测试工具 100%纯JAVA开发,完全可移植性 可用于测试静态和动态资源 多协议-- ...

  5. 记一次windows服务开发中遇到的问题

    最近在研究windows service和quartz.net,所以迅速在园子大神那里扒了一个demo,运行,安装一切顺利. 但在在App.config配置中增加了数据库连接字符串配置后,服务安装后无 ...

  6. Luogu 1023 - 税收与补贴问题 - [数学题]

    题目链接:https://www.luogu.org/problemnew/show/P1023 题目背景每样商品的价格越低,其销量就会相应增大.现已知某种商品的成本及其在若干价位上的销量(产品不会低 ...

  7. [No000017F]如何监控注册表的修改

    今天我们将向您展示如何使用我们最喜欢的工具之一Proc Mon,在您更改PC上的组策略设置时查看编辑的注册表项. 使用Proc Mon查看组策略对象修改的注册表设置 您要做的第一件事就是从Sys In ...

  8. 设备树..ing

    .dts==>.dtb ==>device_node ==>  platform_device ==> led_dev.c  ==>匹配 led_drv.c    (设备 ...

  9. pandas的Panel类型dtype

    panel = pd.Panel(dataframe_dict) 把一个多列类型不相同(里面有int,float)的dataframe字典直接赋值给Panel,从Panel中解析出来的datafram ...

  10. Remove menucool tooltip trial version

    You use this crack on your own risk , i dont reserve any right on this script or what is going to af ...