假设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. JVM CPU打满问题定位

    1.线程不释放,导致Old区占满,系统不停的FullGC 发现应用并没有在进行FGC,而是进行频繁的YGC. YGC也存在异常,S1和S0区域都是从0直接跳到99% 观察堆大小装太发现Young区内存 ...

  2. SpringMVC中获得HttpRequest对象的方法

    1. 使用@autowired注入HttpRequest 2. 在方法中直接声明形参有HttpRequest即可. 3. 使用一个Listener,然后获取.

  3. JS的数据类型转换

    JS 数据类型转换 方法主要有三种 转换函数.强制类型转换.利用js变量弱类型转换. 1. 转换函数: js提供了parseInt()和parseFloat()两个转换函数.前者把值转换成整数,后者把 ...

  4. php的表单安全处理

    规则 1:绝不要信任外部数据或输入 关于 Web 应用程序安全性,必须认识到的第一件事是不应该信任外部数据.外部数据(outside data) 包括不是由程序员在 PHP 代码中直接输入的任何数据. ...

  5. 地址选择的,弹出的下拉。select。通过《option》《/option》来隔开

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. codeforces 665B Shopping

    暴力 #include<cstdio> #include<cstring> #include<cmath> #include<vector> #incl ...

  7. PAT (Advanced Level) 1045. Favorite Color Stripe (30)

    最长公共子序列变形. #include<iostream> #include<cstring> #include<cmath> #include<algori ...

  8. java程序中抛出异常的两种方式,及异常抛出的顺序

    在java中,会经常遇到异常,java提供了两种抛出异常的方式. 方式一: throws ,抛出具体代码中的异常,这种方式编译器都会提示,举例: public static void main(Str ...

  9. Htpasswd 给网站后台目录加密

    http://www.l0phtcrack.com/download.html打开apache配置文件 httpd.conf  配置如下:  <Directory "/var/www/ ...

  10. 关于有限状态机FSM同步复位的问题

    FSM通常情况下使用异步信号进行复位,如FSM1中的rst_n信号.当rst_n信号为低时,FSM进入空闲状态IDLE. 在某些特殊情况下有可能需要跟随某个外部信号强制切换到空闲状态,也即同步复位.下 ...