Spring和junit测试之配置文件路径
2014-03-21 23:31
423 查看
本人在测试一个方法时需要加载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");
详细代码如下:
Java代码
![](http://www.myexception.cn/img/2012/06/25/204523239.png)
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容器最基本的接口就是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");
详细代码如下:
Java代码
![](http://www.myexception.cn/img/2012/06/25/204523239.png)
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());
}
相关文章推荐
- junit测试用例加载spring配置文件
- Spring Junit 读取WEB-INF下的配置文件路径
- junit进行测试加载多个spring配置文件
- Spring和Struts2配置文件不在默认路径下的web.xml配置方法
- Spring+Junit 读取WEB-INF上的配置文件
- spring配置文件路径修改后web.xml配置
- Spring配置文件详解 - applicationContext.xml文件路径
- Junit单元测试Spring 读取WEB-INF下的配置文件
- java web路径和spring读取配置文件
- spring-boot 加载本地静态资源文件路径配置
- 3.减少spring的配置文件:测试
- Spring下的单元测试(JUnit)--加载配置文件
- Junit手动/自动加载spring配置文件
- 解决:在php配置文件路径下,添加php.ini之后,测试页面无法显示
- Spring+Junit 读取WEB-INF上的配置文件
- spring配置文件路径修改后web.xml配置
- Spring 加载xml配置文件路径问题
- 嵌入式jetty启动spring(java配置方式),junit测试用
- Spring 读取配置文件 及路径问题(转)
- applicationContext.xml文件中怎么配置struts2和spring配置文件的加载路径