Spring Junit 读取WEB-INF下的配置文件
假设Spring配置文件为applicationContext.xml
一、Spring配置文件在类路径下面
在Spring的java应用程序中,一般我们的Spring的配置文件都是放在放在类路径下面(也即编译后会进入到classes目录下)。
以下是我的项目,因为是用maven管理的,所以配置文件都放在“src/main/resources”目录下

这时候,在代码中可以通过
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
然后获取相应的bean。
如果代码想用Junit测试框架来测试,则Spring提供了对Junit支持,还可以使用注解的方式:
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:applicationContext.xml"})
只需要在相应的Test类前面加上此两个注解(第二个注解用来指明Spring的配置文件位置),就可以在Junit Test类使用中Spring提供的依赖注入功能。
二、Spring配置文件在WEB-INF下面
当然在做J2EE开发时,有些人习惯把Spring文件放在WEB-INF目录(虽然更多人习惯放在类路径下面)下面;或者有些Spring配置文件是放在类路径下面,而有些又放在
WEB-INF目录下面,如下图。

这时候,在代码中就不可以使用ClassPathXmlApplicationContext来加载配置文件了,而应使用FileSystemXmlApplicationContext。
- ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml");
然后获取相应的bean。
如果代码想用Junit测试框架来测试,则Spring提供了对Junit支持,还可以使用注解的方式:
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/applicationContext.xml"})
只需要在相应的Test类前面加上此两个注解(第二个注解用来指明Spring的配置文件位置),就可以在Junit Test类使用中Spring提供的依赖注入功能。
下面是我的一个Spring管理下的Junit测试类:
- package com.sohu.group.service.external;
- import java.util.List;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration({"file:src/main/webapp/WEB-INF/applicationContext.xml"})
- public class SuFriendServiceImplOverRMITest {
- @Autowired
- private SuFriendService suFriendService;
- @Test
- public void getUserFollowerListTest(){
- List<String> list = suFriendService.getUserFollowerList("liug_talk@163.com");
- System.out.println("------"+list);
- }
- }
Spring Junit 读取WEB-INF下的配置文件的更多相关文章
- java读取resource目录下的配置文件
java读取resource目录下的配置文件 1:配置resource目录 下的文件 host: 127.0.0.1 port: 9300 2:读取 / 代表resource目录 InputSt ...
- 读取web应用下的资源文件(例如properties)
package gz.itcast.b_resource; import java.io.IOException; import java.io.InputStream; import java.ut ...
- Java读取Maven工程下的配置文件,工具类
Java开发中,经常需要在maven工程中读取src/main/resources下的配置文件: 思路如下: Class.getClassLoader() 返回类加载器ClassLoader,进而可以 ...
- Spring boot 读取resource目录下的文件
背景:最近做项目重构将以前的ssh + angular js架构,重构为spring boot + vue.项目是一个数据管理平台,后台涉及到多表关联查询,数据导入导出等. 问题:读取resource ...
- spring读取classpath目录下的配置文件通过表达式去注入属性值.txt
spring读取配置文件: 1. spring加载配置文件: <context:property-placeholder location="classpath:config/syst ...
- 使用IDEA搭建一个 Spring + Spring MVC 的Web项目(零配置文件)
注解是Spring的一个构建的一个重要手段,减少写配置文件,下面解释一下一些要用到的注解: @Configuration 作用于类上面,声明当前类是一个配置类(相当于一个Spring的xml文件)@C ...
- 使用IDEA搭建一个Spring + Spring MVC 的Web项目(零配置文件)
话不多说,直接上代码: 注解是Spring的一个构建的一个重要手段,减少写配置文件,下面解释一下一些要用到的注解: @Configuration 作用于类上面,声明当前类是一个配置类(相当于一个Spr ...
- spring boot读取配置文件
一.springboot配置文件 核心配置文件和自定义配置文件.核心配置文件是指在resources根目录下的application.properties或application.yml配置文 ...
- 读取web外的配置文件
一般web项目配置文件都放在classPath下面,读取的时候: 1 import java.io.InputStream; 2 import java.util.Properties; 3 publ ...
随机推荐
- 根据key存不存在查询json
select * from table where value->'key' != 'null';
- POJ 2186 Popular Cows tarjan缩点算法
题意:给出一个有向图代表牛和牛喜欢的关系,且喜欢关系具有传递性,求出能被所有牛喜欢的牛的总数(除了它自己以外的牛,或者它很自恋). 思路:这个的难处在于这是一个有环的图,对此我们可以使用tarjan算 ...
- Python -- OOP高级 -- 定制类
__str__ 和 __repr__ :实例对象直接显示字符串 class Student: def __init__(self, name): self.name = name def __str_ ...
- Modis 陆地产品格网
Technorati 标签: Modis,陆地产品,格网
- Selenium2+python自动化28-table定位
前言 在web页面中经常会遇到table表格,特别是后台操作页面比较常见.本篇详细讲解table表格如何定位. 一.认识table 1.首先看下table长什么样,如下图,这种网状表格的都是table ...
- 使用Retrofit和RxJava
使用Retrofit和RxJava整合访问网络,然后将数据显示到界面上 def retrofitVersion = '2.0.0-beta1' dependencies { compile fileT ...
- System.Web.Optimization找不到引用怎么办?
Install-Package Microsoft.AspNet.Web.Optimization
- 转 区别 getChildFragmentManager getSupportFragmentManager
The definition of getChildFragmentManager() is: Return a private FragmentManager for placing and man ...
- C++ Builder string相互转换(转)
源:http://www.cnblogs.com/zhcncn/archive/2013/05/20/3089084.html 1. char*->string (1)直接转换 const ch ...
- C#入门经典(第五章-2)字符串的处理