本人在测试一个方法时需要加载XML配置文件,spring提供了相应的方法,就小小研究了下,在此记录下具体的过程,方便初学者和自己日后回顾。

Spring容器最基本的接口就是BeanFactory. BeanFactory负责配置、创建、管理Bean,它有一个子接口ApplicationContext,也称为Spring上下文。Spring容器负责管理Bean与Bean之间的信赖关系。

BeanFactory有很多实现类,通常使用 org.springframework.beans.factory.xml.XmlBeanFactory类。但对于大部分J2EE应用而言,推荐使 用ApplicationContext. ApplicationContext是BeanFactory的子接口,其常用实现类是org.springframework.context.support.FileSystemXmlApplicationContext 和org.springframework.context.support.ClassXmlAplicationContext 。 
Springr的配置信息通常采用XML配置文件来设置,因此,创建BeanFactory实例时,应该提供XML配置文件作为参数。、

下面详细介绍ApplicationContext的实际运用:

一:ClassPathXmlApplicationContext 
1.没有前缀:默认为项目的classpath下相对路径 
   ApplicationContext appCt = new ClassPathXmlApplicationContext("app.spring.xml");

2.前缀classpath:表示的是项目的classpath下相对路径 
   ApplicationContext appCt = new ClassPathXmlApplicationContext("classpath:app.spring.xml");

3.使用前缀file 表示的是文件的绝对路径 
   ApplicationContext appCt = new ClassPathXmlApplicationContext("file:D:/app.spring.xml");

4.可以同时加载多个文件 
  String[] xmlCfg = new String[] { "classpath:base.spring.xml","app.spring.xml"}; 
  ApplicationContext appCt = new ClassPathXmlApplicationContext(xmlCfg);

5.使用通配符加载所有符合要求的文件 
  ApplicationContext appCt = new ClassPathXmlApplicationContext("*.spring.xml");

二:FileSystemXmlApplicationContext 
1.默认为项目工作路径 即项目的根目录 
ApplicationContext appCt2 = new FileSystemXmlApplicationContext("src/main/resources/app.spring.xml");

2.前缀classpath:表示的是项目的classpath下相对路径 
   ApplicationContext appCt2 = new FileSystemXmlApplicationContext("classpath:app.spring.xml");

3.使用前缀file 表示的是文件的绝对路径 
   ApplicationContext appCt2 = new FileSystemXmlApplicationContext("file:D:/app.spring.xml "); 
   ApplicationContext appCt2 = new FileSystemXmlApplicationContext("D:/app.spring.xml ");

4.可以同时加载多个文件 
  String[] xmlCfg = new String[] { "src/main/resources/base.spring.xml","classpath:app.spring.xml"}; 
  ApplicationContext appCt2 = new FileSystemXmlApplicationContext(xmlCfg);

5.使用通配符加载所有符合要求的文件 
  ApplicationContext appCt2 = new FileSystemXmlApplicationContext("classpath:*.spring.xml");

详细代码如下:

    1. import  org.springframework.context.ApplicationContext;
    2. import  org.springframework.context.support.ClassPathXmlApplicationContext;
    3. import  org.springframework.context.support.FileSystemXmlApplicationContext;
    4. import  aoplog.LogAfterAdvice;
    5. import  aoplog.LogBeforeAdvice;
    6. /**
    7. * @author Michael
    8. *
    9. */
    10. public   class  TestApplicationContext {
    11. /**
    12. * @param args
    13. */
    14. public   static   void  main(String[] args) {
    15. /**
    16. * ClassPathXmlApplicationContext
    17. */
    18. // 没有前缀:默认为项目的classpath下相对路径
    19. ApplicationContext appCt = new  ClassPathXmlApplicationContext(
    20. "app.spring.xml" );
    21. // 前缀classpath:表示的是项目的classpath下相对路径
    22. // ApplicationContext appCt = new ClassPathXmlApplicationContext(
    23. // "classpath:app.spring.xml");
    24. // 使用前缀file 表示的是文件的绝对路径
    25. // ApplicationContext appCt = new ClassPathXmlApplicationContext(
    26. // "file:D:/app.spring.xml");
    27. LogBeforeAdvice logBefore = (LogBeforeAdvice) appCt
    28. .getBean("logBefore" );
    29. System.out.println("ClassPathXmlApplicationContext test:"
    30. + logBefore.getClass());
    31. // 利用通配符文件加载
    32. ApplicationContext appCtXx = new  ClassPathXmlApplicationContext(
    33. "*.spring.xml" );
    34. // 多文件加载
    35. String[] xmlCfg = new  String[] {  "classpath:base.spring.xml" ,
    36. "myapp.spring.xml"  };
    37. ApplicationContext appCtMore = new  ClassPathXmlApplicationContext(
    38. xmlCfg);
    39. /*
    40. * FileSystemXmlApplicationContext
    41. */
    42. // 默认为项目工作路径 即项目的根目录
    43. ApplicationContext appCt2 = new  FileSystemXmlApplicationContext(
    44. "src/main/resources/app.spring.xml" );
    45. // 前缀classpath:表示的是项目的classpath下相对路径
    46. // ApplicationContext appCt2 = new FileSystemXmlApplicationContext(
    47. // "classpath:app.spring.xml");
    48. // 使用前缀file 表示的是文件的绝对路径
    49. // ApplicationContext appCt2 = new FileSystemXmlApplicationContext(
    50. // "file:D:/app.spring.xml");
    51. LogAfterAdvice logAfter = (LogAfterAdvice) appCt2.getBean("logAfter" );
    52. System.out.println("FileSystemXmlApplicationContext test:"
    53. + logAfter.getClass());
    54. }

Spring和junit测试之配置文件路径的更多相关文章

  1. spring 通过启动命令配置文件路径

    公司使用dubbo开发,提供了很多的服务,每个服务中一些配置都是一样的,比如注册中心地址,公共码表库等一下配置,这样在部署每一个程序的时候,修改每一个服务的配置增加很多的工作量.且领导不想对程序有大的 ...

  2. Spring集成JUnit单元测试框架

    一.JUnit介绍 JUnit是Java中最有名的单元测试框架,用于编写和运行可重复的测试,多数Java的开发环境都已经集成了JUnit作为单元测试的工具.好的单元测试能极大的提高开发效率和代码质量. ...

  3. Spring——ClassPathXmlApplicationContext(配置文件路径解析 1)

    ClassPathXmlApplicationContext     在我的 BeanFactory 容器文章中主要提及了 BeanFactory 容器初始化(Spring 配置文件加载(还没解析)) ...

  4. spring注解注入properties配置文件

    早期,如果需要通过spring读取properties文件中的配置信息,都需要在XML文件中配置文件读取方式. 基于XML的读取方式: <bean class="org.springf ...

  5. Spring整合junit测试

    本节内容: Spring整合junit测试的意义 Spring整合junit测试 一.Spring与整合junit测试的意义 在没整合junit之前,我们在写测试方法时,需要在每个方法中手动创建容器, ...

  6. Spring系列之新注解配置+Spring集成junit+注解注入

    Spring系列之注解配置 Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率 你本来要写一段很长的代码来构造一个 ...

  7. 使用Spring配合Junit进行单元测试的总结

    最近公司的项目和自己的项目中都用到了spring集成junit进行单元测试,总结一下几种基本的用法: 1.直接对spring中注入的bean进行测试(以DAO为例): 在测试类上添加@RunWith注 ...

  8. Struts2+Spring+Mybatis+Junit 测试

    Struts2+Spring+Mybatis+Junit 测试 博客分类: HtmlUnit Junit Spring 测试 Mybatis  package com.action.kioskmoni ...

  9. 3.SpringMVC修改配置文件路径和给界面传递数据

    1.修改配置文件路径  达到  配置多文件的目的 web.xml文件中基础配置有springMVC配置的servlet路径 <servlet-name>SpringMVC</serv ...

随机推荐

  1. GCC编译错误小结

    gcc编译时对’xxxx’未定义的引用问题可能错误 错误一: 没有实现xxxx 错误二: c++引用c语言so库,但是so库头文件没有extern "C" 错误三: 检查各个共享库 ...

  2. animation过渡效果

    References: http://developer.android.com/training/animation/index.html http://developer.android.com/ ...

  3. Linux 下如何处理包含空格和特殊字符的文件名

    Linux 下如何处理包含空格和特殊字符的文件名 作者: Avishek Kumar 译者: LCTT zpl1025 | 2015-07-08 07:47   评论: 12 收藏: 9 分享: 1 ...

  4. 【转】【VS Code】配置文件Launch及快捷键

     Ctrl+shift+p,然后输入launch,点击第一个选项即可配置. 之后选择More即可 具体配置可修改为: { "version": "0.2.0", ...

  5. (笔记)Linux下C语言实现静态IP地址,掩码,网关的设置

    #include <sys/ioctl.h> #include <sys/types.h> #include <sys/socket.h> #include < ...

  6. django学习1

    参考资料:http://www.cnblogs.com/feixuelove1009/p/5910384.html#top 本学习只在记录过程,需要更全面的资料,可直接上参考资料细看. django版 ...

  7. Linux可视化服务器管理工具webmin

    webmin是一个可视化的linux服务器管理工具,可以帮助我们实现很多功能. Webmin官网: http://www.webmin.com/ 下载地址:http://prdownloads.sou ...

  8. Saltstack配置管理(2)

    1.SaltStack批量安装zabbix_agent端. vim /etc/salt/states/init/zabbix_agnet.sls zabbix_install.conf: pkg.in ...

  9. discuz密码找回:[1]忘记UCENTER创始人密码

    人们都是健忘的,何况每天的事情很多,有些站长更是兼职做,赚点外快而已,而ucenter更是不常用,所以忘记密码是在正常不过的事情,如果密码忘记怎么找回呢?方法有很多种,例如用comsenz tools ...

  10. eclipse断点Source not found解决方案1,2,3

    1.tomcat插件 路径是Window --> Preferences --> Tomcat --> Source Path,在Source Path 标签下有行文字:Add ja ...