假设Spring配置文件为applicationContext.xml

一、Spring配置文件在类路径下面

在Spring的java应用程序中,一般我们的Spring的配置文件都是放在放在类路径下面(也即编译后会进入到classes目录下)。

以下是我的项目,因为是用maven管理的,所以配置文件都放在“src/main/resources”目录下

这时候,在代码中可以通过

  1. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

然后获取相应的bean。

如果代码想用Junit测试框架来测试,则Spring提供了对Junit支持,还可以使用注解的方式:

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations={"classpath:applicationContext.xml"})

只需要在相应的Test类前面加上此两个注解(第二个注解用来指明Spring的配置文件位置),就可以在Junit Test类使用中Spring提供的依赖注入功能。

二、Spring配置文件在WEB-INF下面

当然在做J2EE开发时,有些人习惯把Spring文件放在WEB-INF目录(虽然更多人习惯放在类路径下面)下面;或者有些Spring配置文件是放在类路径下面,而有些又放在

WEB-INF目录下面,如下图。

这时候,在代码中就不可以使用ClassPathXmlApplicationContext来加载配置文件了,而应使用FileSystemXmlApplicationContext。

  1. ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml");

然后获取相应的bean。

如果代码想用Junit测试框架来测试,则Spring提供了对Junit支持,还可以使用注解的方式:

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/applicationContext.xml"})

只需要在相应的Test类前面加上此两个注解(第二个注解用来指明Spring的配置文件位置),就可以在Junit Test类使用中Spring提供的依赖注入功能。

下面是我的一个Spring管理下的Junit测试类:

    1. package com.sohu.group.service.external;
    2. import java.util.List;
    3. import org.junit.Test;
    4. import org.junit.runner.RunWith;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.test.context.ContextConfiguration;
    7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    8. @RunWith(SpringJUnit4ClassRunner.class)
    9. @ContextConfiguration({"file:src/main/webapp/WEB-INF/applicationContext.xml"})
    10. public class SuFriendServiceImplOverRMITest {
    11. @Autowired
    12. private SuFriendService suFriendService;
    13. @Test
    14. public void getUserFollowerListTest(){
    15. List<String> list = suFriendService.getUserFollowerList("liug_talk@163.com");
    16. System.out.println("------"+list);
    17. }
    18. }

Spring Junit 读取WEB-INF下的配置文件的更多相关文章

  1. java读取resource目录下的配置文件

    java读取resource目录下的配置文件 1:配置resource目录 下的文件 host: 127.0.0.1 port: 9300 2:读取    / 代表resource目录 InputSt ...

  2. 读取web应用下的资源文件(例如properties)

    package gz.itcast.b_resource; import java.io.IOException; import java.io.InputStream; import java.ut ...

  3. Java读取Maven工程下的配置文件,工具类

    Java开发中,经常需要在maven工程中读取src/main/resources下的配置文件: 思路如下: Class.getClassLoader() 返回类加载器ClassLoader,进而可以 ...

  4. Spring boot 读取resource目录下的文件

    背景:最近做项目重构将以前的ssh + angular js架构,重构为spring boot + vue.项目是一个数据管理平台,后台涉及到多表关联查询,数据导入导出等. 问题:读取resource ...

  5. spring读取classpath目录下的配置文件通过表达式去注入属性值.txt

    spring读取配置文件: 1. spring加载配置文件: <context:property-placeholder location="classpath:config/syst ...

  6. 使用IDEA搭建一个 Spring + Spring MVC 的Web项目(零配置文件)

    注解是Spring的一个构建的一个重要手段,减少写配置文件,下面解释一下一些要用到的注解: @Configuration 作用于类上面,声明当前类是一个配置类(相当于一个Spring的xml文件)@C ...

  7. 使用IDEA搭建一个Spring + Spring MVC 的Web项目(零配置文件)

    话不多说,直接上代码: 注解是Spring的一个构建的一个重要手段,减少写配置文件,下面解释一下一些要用到的注解: @Configuration 作用于类上面,声明当前类是一个配置类(相当于一个Spr ...

  8. spring boot读取配置文件

    一.springboot配置文件 核心配置文件和自定义配置文件.核心配置文件是指在resources根目录下的application.properties或application.yml配置文     ...

  9. 读取web外的配置文件

    一般web项目配置文件都放在classPath下面,读取的时候: 1 import java.io.InputStream; 2 import java.util.Properties; 3 publ ...

随机推荐

  1. PAT (Advanced Level) 1104. Sum of Number Segments (20)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  2. JSP标准标签库(JSTL)--SQL标签库 sql

    了解即可.SQL标签库 No. 功能分类 标签名称 描述 1 数据源标签 <sql:setDataSource> 设置要使用的数据源名称 2 数据库操作标签 <sql:query&g ...

  3. CST 公共生成树

    本实验只讨论CST(公共的生成树) 一.实验前先理解生成树决策的4 个步骤: 二.实验拓扑 1. 实验描述: 由于业务的要求,要有可靠的链路,要对链路实现冗余,但链路的冗余有可能给网络带来广播风暴,重 ...

  4. a java runtime environment(JRE) or java development kit(JDK) must be....

    错误: 解决方法: 系统变量里设置下面: 变量名:JAVA_HOME 变量值:C:\Program Files\Java\jdk1.6.0_25 变量名:CLASSPATH 变量值:.;%JAVA_H ...

  5. string 与wstring 的转换

    std::wstring StringToWString(const std::string &str) { std::wstring wstr(str.length(),L' '); std ...

  6. C#入门经典(3-窗体应用程序-第二章要点)

    新建一个窗体用用程序,拖一个按钮,加事件和Text属性.打开Form1Designer.cs.

  7. 转 sqlplus执行sql报错:ORA-01756:

    1.sqlplus执行sql报错:ORA-01756: quoted string not properly terminated   分类: 技术         在SQLPLUS中执行SQL文件时 ...

  8. 1.3 selenium IDE录制脚本转换为其他代码格式

    1.在seleniumIDE录制框中点击Options->options... 在Enable experimental features前打对勾,点击确定.

  9. mystring c++ 自己的string 封装

    1 /************************************************************************* > File Name: mystrin ...

  10. DOS日期和时间 - Robin Hu的专栏 - 博客频道 - CSDN.NET

    body { font-family: Microsoft YaHei UI,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-ser ...