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 ...
随机推荐
- Hbase shell 中能否通过filter实现的高级查询
import org.apache.hadoop.hbase.filter.CompareFilter import org.apache.hadoop.hbase.filter.SingleColu ...
- java 文件字符输入、输出流
Example10_6.java import java.io.*; public class Example10_6 { public static void main(String args[]) ...
- 把Ubuntu用户目录下的目录名改成英文
直接改名字是不行的,一重启就回去了 方法一: 把中文文件夹改成相应的英文文件夹,再修改配置文件 ~/.config/user-dirs.dirs XDG_DESKTOP_DIR="$HOME ...
- AdminLTE的使用
1.AdminLTE的必要配置文件<!-- Tell the browser to be responsive to screen width --> <meta content=& ...
- JSP标准标签库(JSTL)--JSTL简介与安装
对于MVC设计模式来讲,我们一直强调,在一个JSP钟scriptlet代码越少越好,但是只靠以前的概念很难实现,因为标签的开发特别麻烦,所以为了简化标签,也为了让标签更具备一些通用性,所以一般在开发中 ...
- 关于Jsp页面在ww:iterator 标签里面判断的写法是可以直接写数组里面的变量的
因为上面已经遍历了,所以可以直接写变量名
- zf-关于查询机把index.jsp换成index_new.jsp页面之后把功能链接都改成新页面的简单方法
一开始我都是找action 然后一个一个的改 把onmousedown="goURL('index.jsp')" 改成 onmousedown="goURL('index ...
- 黄聪:基于Asp.net的CMS系统We7架设实验(环境WIN7,SQL2005,.NET3.5)(初学者参考贴)
http://www.cnblogs.com/huangcong/archive/2010/03/30/1700348.html
- mysql 本地操作
先把数据库传到根目录 再直接导入 没有50M限制root@b101 [/home/user/www]# mysql -uusername_007li -ppasswd -D dbname_li12d ...
- JAVA基础---面向对象
方法的重载Overload: 一个类中可以定义有相同的名字, 参数不同的多个方法. 调用时, 会根据不同的参数选择对应的方法. static: 在数据区, 可以计数,属于类,不属于对象: public ...