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. AJAX----注:借鉴

    Ajax的工作原理. Ajax的核心是JavaScript对象XmlHttpRequest.该对象在Internet Explorer5首次引入,它是一种支持异步请求的技术.简而言之,XmlHttpR ...

  2. CentOS最小化系统,怎么安装图形界面

    CentOS最小化系统做服务器,都是没有图形界面的.很多初学者不习惯命令行操作,那么应该怎么安装图形界面?本经验咗嚛以centos6.5系统为例   方法步骤:   首先进入centos系统界面,先测 ...

  3. POJ 1426 - Find The Multiple - [DP][BFS]

    题目链接:http://poj.org/problem?id=1426 Given a positive integer n, write a program to find out a nonzer ...

  4. 基于 redis 的分布式锁实现 Distributed locks with Redis debug 排查错误

    小结: 1. 锁的实现方式,按照应用的实现架构,可能会有以下几种类型: 如果处理程序是单进程多线程的,在 python下,就可以使用 threading 模块的 Lock 对象来限制对共享变量的同步访 ...

  5. vim-cscope插件

    在工程根目录生成cscope.out 索引文件的脚本 rm -f cscope* currentPath=$(pwd ) echo $currentPath find $currentPath -na ...

  6. MAVEN_day01 下载与安装及环境变量的配置

    一.MAVEN简介 MAVEN是apache组织下的一个开源项目,是使用纯java编写的,之用于管理java工程. 二.MAVEN下载与安装 下载地址:http://maven.apache.org/ ...

  7. input type = file 上传图片转为base64

    项目背景是做图片识别,接口需要上传图片格式为base64格式的,react项目的相关代码: let reader = new FileReader();reader.readAsDataURL(e.t ...

  8. python基础之 编码进阶,文件操作和深浅copy

    1.编码的进阶 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码.即先将其他编码的字符串解码(decode)成unicode,再从unic ...

  9. node os模块

    const os = require('os'); console.log(os.homedir()); console.log(os.hostname()); console.log(os.plat ...

  10. 要学的javaee技术

    mybatis.hibernate.spirng MVC.freemarker.zookeeper.dubbo.quartz的技术框架:NoSQL技术ehcache.memcached.redis等: ...