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()); }

转载于:https://www.cnblogs.com/cristin/p/8722191.html

Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别相关推荐

  1. spring中ClassPathXmlApplication和FileSystemXmlApplicationContext使用区别

    spring中ClassPathXmlApplication和FileSystemXmlApplicationContext使用区别 ApplicationContext ac = new Class ...

  2. spring中@Inject和@Autowired的区别?分别在什么条件下使用呢?

    问题:spring中@Inject和@Autowired的区别?分别在什么条件下使用呢? 我在浏览SpringSource上的一些博客,在其他一个博客中,那个作者用了@Inject,但是我觉得他用@A ...

  3. Spring中 BeanFactory 与 FactoryBean 的区别

    作者:叁滴水 blog.csdn.net/qq_30285985/article/details/115433099 文章目录 前言 为什么会有FactoryBean? 通过源码深入学习Factory ...

  4. Spring中@within与@target的区别

    区别 @within:定义在哪里,就作用在哪里.(有点静态编译的味道) @target:运行在哪里,就作用在哪里.(类比动态执行的感觉)@within,如果子类没有重写,在某个类上加一个注解,就会默认 ...

  5. Spring中BeanFactory和FactoryBean的区别

    先介绍一下Spring的IOC容器到底是个什么东西,都说是一个控制反转的容器,将对象的控制权交给IOC容器,其实在看了源代码之后,就会发现IOC容器只是一个存储单例的一个ConcurrentHashM ...

  6. Spring中@Autowired和@Resource的区别

    一.定义 @Autowired 对类成员变量.方法及构造函数进行标注,完成自动装配的工作. @Resource 在语义上被定义为通过其唯一的名称来标识特定的目标组件,其中声明的类型与匹配过程无关. 如 ...

  7. Spring中拦截/和拦截/*的区别 - 不能访问到返回的JSP - 访问静态资源(jpg,js等)

    最近碰到了一个spring的web.xml无法跳转到index.html页面的小问题,查了之后从下面这篇文章中受益,特此转发,谢谢!!! 一.我们都知道在基于Spring的Application中,需 ...

  8. Spring中拦截/和拦截/*的区别

    一.我们都知道在基于Spring的Application中,需要在web.xml中增加下面类似的配置信息: <listener> <listener-class> org.sp ...

  9. Spring中redirect和forward的区别

    页面转发区别: 在Servlet中两种实现: forward方式:request.getRequestDispatcher("/somePage.jsp").forward(req ...

  10. Spring框架中ApplicationContext与BeanFactory的区别

    前言 Spring框架中ApplicationContext与BeanFactory的区别.其中,BeanFactory为ApplicationContext的父类,是一种继承关系,Applicati ...

最新文章

  1. TensorFlow解析常量、变量和占位符
  2. VB.NET 创建WORD文档
  3. 学长毕业日记 :本科毕业论文写成博士论文的神操作20170412
  4. html入门学习(二)
  5. GDCM:gdcm::ByteValue的测试程序
  6. HttpClient的简单使用
  7. c++switch实现猜拳_C语言实现人机猜拳游戏,非常适合C++小白练习的项目!
  8. 信息学奥赛一本通C++语言——1049:晶晶赴约会
  9. jsp页面跳转(商品管理系统)
  10. 第 5 章 输入输出管理
  11. 自动驾驶(五十八)---------自动驾驶2019年终盘点
  12. 编辑PDF的软件哪个好 如何修改PDF文件
  13. 阿里巴巴集团CTO张建锋:将开放阿里的技术能力
  14. 如何把PDF转成护眼模式/反色/黑底白字
  15. 面对这些可能出现的意外,你的运维团队准备好了吗?
  16. Kali历史版本下载地址
  17. xmu 1303.田教主的卡片
  18. OPC是什么及OPC简介
  19. 会议签到web_基于Web的网络签到系统设计与实现
  20. spring mvc 学习指南一

热门文章

  1. MyBatis中Mapper接口是怎么和XML文件关联起来的
  2. RabbitMQ的工作模式及消息顺序性的保证
  3. Petya到底是个什么鬼
  4. 数据结构1——堆栈和队列
  5. python+selenium环境配置(windows7环境)
  6. 分布式消息系统jafka快速起步(转)
  7. cocos2d-x之物理世界(创建物理世界)
  8. Android Eclipse 导入 AS Gradle AAR 库手册
  9. 改变php默认目录结构,ThinkPHP中自定义目录结构的设置方法_PHP
  10. Java的继承 以学生管理系统为例