Spring和junit测试之配置文件路径
本人在测试一个方法时需要加载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");
详细代码如下:
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.context.support.FileSystemXmlApplicationContext;
- import aoplog.LogAfterAdvice;
- import aoplog.LogBeforeAdvice;
- /**
- * @author Michael
- *
- */
- public class TestApplicationContext {
- /**
- * @param args
- */
- public static void main(String[] args) {
- /**
- * ClassPathXmlApplicationContext
- */
- // 没有前缀:默认为项目的classpath下相对路径
- ApplicationContext appCt = new ClassPathXmlApplicationContext(
- "app.spring.xml" );
- // 前缀classpath:表示的是项目的classpath下相对路径
- // ApplicationContext appCt = new ClassPathXmlApplicationContext(
- // "classpath:app.spring.xml");
- // 使用前缀file 表示的是文件的绝对路径
- // ApplicationContext appCt = new ClassPathXmlApplicationContext(
- // "file:D:/app.spring.xml");
- LogBeforeAdvice logBefore = (LogBeforeAdvice) appCt
- .getBean("logBefore" );
- System.out.println("ClassPathXmlApplicationContext test:"
- + logBefore.getClass());
- // 利用通配符文件加载
- ApplicationContext appCtXx = new ClassPathXmlApplicationContext(
- "*.spring.xml" );
- // 多文件加载
- String[] xmlCfg = new String[] { "classpath:base.spring.xml" ,
- "myapp.spring.xml" };
- ApplicationContext appCtMore = new ClassPathXmlApplicationContext(
- xmlCfg);
- /*
- * FileSystemXmlApplicationContext
- */
- // 默认为项目工作路径 即项目的根目录
- ApplicationContext appCt2 = new FileSystemXmlApplicationContext(
- "src/main/resources/app.spring.xml" );
- // 前缀classpath:表示的是项目的classpath下相对路径
- // ApplicationContext appCt2 = new FileSystemXmlApplicationContext(
- // "classpath:app.spring.xml");
- // 使用前缀file 表示的是文件的绝对路径
- // ApplicationContext appCt2 = new FileSystemXmlApplicationContext(
- // "file:D:/app.spring.xml");
- LogAfterAdvice logAfter = (LogAfterAdvice) appCt2.getBean("logAfter" );
- System.out.println("FileSystemXmlApplicationContext test:"
- + logAfter.getClass());
- }
Spring和junit测试之配置文件路径的更多相关文章
- spring 通过启动命令配置文件路径
公司使用dubbo开发,提供了很多的服务,每个服务中一些配置都是一样的,比如注册中心地址,公共码表库等一下配置,这样在部署每一个程序的时候,修改每一个服务的配置增加很多的工作量.且领导不想对程序有大的 ...
- Spring集成JUnit单元测试框架
一.JUnit介绍 JUnit是Java中最有名的单元测试框架,用于编写和运行可重复的测试,多数Java的开发环境都已经集成了JUnit作为单元测试的工具.好的单元测试能极大的提高开发效率和代码质量. ...
- Spring——ClassPathXmlApplicationContext(配置文件路径解析 1)
ClassPathXmlApplicationContext 在我的 BeanFactory 容器文章中主要提及了 BeanFactory 容器初始化(Spring 配置文件加载(还没解析)) ...
- spring注解注入properties配置文件
早期,如果需要通过spring读取properties文件中的配置信息,都需要在XML文件中配置文件读取方式. 基于XML的读取方式: <bean class="org.springf ...
- Spring整合junit测试
本节内容: Spring整合junit测试的意义 Spring整合junit测试 一.Spring与整合junit测试的意义 在没整合junit之前,我们在写测试方法时,需要在每个方法中手动创建容器, ...
- Spring系列之新注解配置+Spring集成junit+注解注入
Spring系列之注解配置 Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率 你本来要写一段很长的代码来构造一个 ...
- 使用Spring配合Junit进行单元测试的总结
最近公司的项目和自己的项目中都用到了spring集成junit进行单元测试,总结一下几种基本的用法: 1.直接对spring中注入的bean进行测试(以DAO为例): 在测试类上添加@RunWith注 ...
- Struts2+Spring+Mybatis+Junit 测试
Struts2+Spring+Mybatis+Junit 测试 博客分类: HtmlUnit Junit Spring 测试 Mybatis package com.action.kioskmoni ...
- 3.SpringMVC修改配置文件路径和给界面传递数据
1.修改配置文件路径 达到 配置多文件的目的 web.xml文件中基础配置有springMVC配置的servlet路径 <servlet-name>SpringMVC</serv ...
随机推荐
- 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 ...
- 关于Unity中Shader的基础认识
Shader也叫着色器,是Unity里面比较难的一个点,网上有很多别人写好的shader,我们可以下载下来用或者修改学习. Shader可以做出很多非常不错的效果,因为它是插在渲染管道里面的程序,一来 ...
- e838. 使JTabbedPane中的卡片能用按键的方式选取
Setting a mnemonic on a tab allows the tab to be selected with a keystroke. For example, if the mnem ...
- Winform控件学习笔记【第六天】——TreeView
TreeView控件用来显示信息的分级视图,如同Windows里的资源管理器的目录.TreeView控件中的各项信息都有一个与之相关的Node对象.TreeView显示Node对象的分层目录结构,每个 ...
- Hadoop(HA)分布式集群部署
Hadoop(HA)分布式集群部署和单节点namenode部署其实一样,只是配置文件的不同罢了. 这篇就讲解hadoop双namenode的部署,实现高可用. 系统环境: OS: CentOS 6.8 ...
- /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 ...
- 消息中间件activemq-5.13.0安全验证配置
activemq分为控制端和客户端,下面分别介绍安全认证配置方法. 1.控制端安全配置 (1). ActiveMQ目录conf下找到jetty.xml: <bean id="secur ...
- Thinkphp5笔记七:设置错误页面②
更加完美的去设置错误页面. 一.准备一个错误页面 error.html,位置:thinkphp\template\index\default\error.html ,准备把前段所有的错误提示都指向这里 ...
- pytest集成Allure Report
https://blog.csdn.net/liuchunming033/article/details/79624474#commentBox https://blog.csdn.net/lihua ...
- maven pom文件详解
http://www.blogjava.net/hellxoul/archive/2013/05/16/399345.html http://blog.csdn.net/houpengfei111/a ...