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 ...
随机推荐
- 100 行代码实现的 JavaScript MVC 样式框架
介绍 使用过 JavaScript框架(如 AngularJS, Backbone 或者Ember)的人都很熟悉在UI(用户界面,前端)中mvc的工作机理.这些框架实现了MVC,使得在一个单页面中实现 ...
- 【转】如果有人让你推荐Python技术书,请让他看这个列表
入门级 <Head First Python>+ 入门级 + 微信49票 + 豆瓣评分 9.5 推荐语: 66:浅显易懂,编排的顺序特别,有大量插图.对话,不感觉枯燥 古心:通熟易懂,配有 ...
- HBase高性能复杂条件查询引擎
转自:http://blog.csdn.net/bluishglc/article/details/31799255 mark 写在前面 本文2014年7月份发表于InfoQ,HBase的PMC成员T ...
- Android Studio apk 打包
1.Build -> Generate Signed APK...,打开如下窗口 2.假设这里没有打过apk包,点击Create new,窗口如下 这里只要输入几个必要项 Key store p ...
- Maven构建自动化
构建自动化定义相关工程项目构建过程中,在当项目构建成功完成启动的情况下,来确保所依赖的项目是稳定的. 实例 考虑一个团队正在开发一个项目总线核心API上的其他两个项目的应用程序:网页UI和应用程序的桌 ...
- e801. 创建一个JProgressBar组件
A progress bar is used to visually indicate how much a task has been progressed. A progress bar can ...
- 在Asp.net WebAPI使用Session
最近在改写WebApp时要将以前用泛型处理例程写的Captcha 改成使用WebApi 来实作机制,在实作的过程中发现使用IRequiresSessionState session也无法使用(cont ...
- Can't read [proguard.ClassPathEntry@1a0c10f] (No such file or directory)
//该错误无法识别,是因为你使用了旧版本的proguard,建议你下载新版本4.5以上的proguard来替代Android SDK中使用的旧版sdcard 这样你就可以知道错误所在.因为只有4.5版 ...
- Github 提交本地代 到远程 菜鸟 简明教程
方法1: 1. 右击项目文件夹 Git Bash Here 或 Git clone Url 远程仓库地址 Directory 本地文件夹 2. 拷贝(新建)项目文件 3. 添加项目文件 ...
- iOS:当点击 FormSheet 之外时,关闭该视图
@interface XXViewController (){ @property (strong, nonatomic) UITapGestureRecognizer *tapGesture; - ...