本人在测试一个方法时需要加载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. 【android】环形进度条实现

    先上效果图(压缩尺寸后出现锯齿,原图边缘很细腻的喂~) 特性: 1:支持环形带字 .环形不带字(中间盖上圆形图片,实现天天动听播放器在通知栏播放进度的效果).实心 2:线程安全,不需要写handler ...

  2. 【转】Android下使用Properties文件保存程序设置

    原文:http://jerrysun.blog.51cto.com/745955/804789 废话不说,直接上代码.    读取.properties文件中的配置:  String strValue ...

  3. 关于阅读JDK源码的准备

    说明:本篇是给自己看的. 笑 最近突然有冲动 想研究下JDK的源码,搜索了一番,基本上推荐从集合开始,精华部分包括:集合.IO.多线程.网络编程. 虚拟机部分先放一放吧,感觉现在不适合我这种半路出家的 ...

  4. OpenGL基本框架与三维对象绘制

    上次我们介绍了OpenGL的环境构建和二维对象的绘制,这次我们来讲讲三维对象的绘制: 绘制代码如下: Github代码仓库 // opengltest2.cpp : Defines the entry ...

  5. tpshop添加后台菜单

    目前在后台公用函数文件function.php中getAllMenu方法里添加, 格式如下 array( 'system' => array('name'=>'系统设置','icon'=& ...

  6. CI框架 -- 开发环境、生产环境

    开发者常常希望当系统运行在开发环境或生产环境中时能有不同的行为, 例如,在开发环境如果程序能输出详细的错误信息将非常有用,但是在 生产环境这将造成一些安全问题. ENVIRONMENT 常量 Code ...

  7. nodejs基础 -- 路由

    我们要为路由提供请求的URL和其他需要的GET/POST参数,随后路由需要根据这些数据(URL.GET/POST参数)来执行相应的代码. 因此,需要查看HTTP请求,从中提取出请求的URL及GET/P ...

  8. FusionCancer-人类癌症相关的融合基因的数据库

    RNA-seq 测序可以用于融合基因的发现,在过去的十几年里,RNA-seq 测序数据不断增加,发现的融合基因的数据也不断增加: FusionCancer 是一个人类癌症相关的融合基因的数据库,利用N ...

  9. [iOS]XCODE5升级之路

    1.Code Sign error:     解决方案:重新下载并安装Provisioning profile 2.错误:Undefined symbols for architecture armv ...

  10. 【python-proxy by sockets5】pysocks

    pip install pysocks https://stackoverflow.com/questions/2317849/how-can-i-use-a-socks-4-5-proxy-with ...