本人在测试一个方法时需要加载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. Action.c(37):Error-27791:Server"192.168.2.111" has shut down the connection paematurely

    1)测试的时候最好应用干净的测试环境,即:清楚一切可能干扰的对象:2)如果出了这种错误,要认真的去排查错误,不单单找找脚本,或者[b][url=http://www.ltesting.net/html ...

  2. 关于Unity中Shader的基础认识

    Shader也叫着色器,是Unity里面比较难的一个点,网上有很多别人写好的shader,我们可以下载下来用或者修改学习. Shader可以做出很多非常不错的效果,因为它是插在渲染管道里面的程序,一来 ...

  3. e838. 使JTabbedPane中的卡片能用按键的方式选取

    Setting a mnemonic on a tab allows the tab to be selected with a keystroke. For example, if the mnem ...

  4. Winform控件学习笔记【第六天】——TreeView

    TreeView控件用来显示信息的分级视图,如同Windows里的资源管理器的目录.TreeView控件中的各项信息都有一个与之相关的Node对象.TreeView显示Node对象的分层目录结构,每个 ...

  5. Hadoop(HA)分布式集群部署

    Hadoop(HA)分布式集群部署和单节点namenode部署其实一样,只是配置文件的不同罢了. 这篇就讲解hadoop双namenode的部署,实现高可用. 系统环境: OS: CentOS 6.8 ...

  6. /var/log/messages Logging not working on Centos 7

    This was the solution, not permanent, though: rm -f /var/lib/rsyslog/imjournal.state systemctl resta ...

  7. 消息中间件activemq-5.13.0安全验证配置

    activemq分为控制端和客户端,下面分别介绍安全认证配置方法. 1.控制端安全配置 (1). ActiveMQ目录conf下找到jetty.xml: <bean id="secur ...

  8. Thinkphp5笔记七:设置错误页面②

    更加完美的去设置错误页面. 一.准备一个错误页面 error.html,位置:thinkphp\template\index\default\error.html ,准备把前段所有的错误提示都指向这里 ...

  9. pytest集成Allure Report

    https://blog.csdn.net/liuchunming033/article/details/79624474#commentBox https://blog.csdn.net/lihua ...

  10. maven pom文件详解

    http://www.blogjava.net/hellxoul/archive/2013/05/16/399345.html http://blog.csdn.net/houpengfei111/a ...