`

Spring Junit 读取WEB-INF下的配置文件(转)

阅读更多

 

感谢 http://blog.csdn.net/rj042/article/details/7549462 帮我解决了配置文件读取问题。

 

假设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);
    }
}

 

 

 

 

 

 

 

 

 

  • 大小: 14.5 KB
  • 大小: 35.8 KB
分享到:
评论

相关推荐

    junit-jupiter-api-5.7.2-API文档-中文版.zip

    赠送Maven依赖信息文件:junit-jupiter-api-5.7.2.pom; 包含翻译后的API文档:junit-jupiter-api-5.7.2-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.junit.jupiter:junit-jupiter-api:5.7.2; 标签:junit...

    junit-jupiter-api-5.8.2-API文档-中文版.zip

    赠送Maven依赖信息文件:junit-jupiter-api-5.8.2.pom; 包含翻译后的API文档:junit-jupiter-api-5.8.2-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.junit.jupiter:junit-jupiter-api:5.8.2; 标签:junit...

    junit-jupiter-engine-5.8.2-API文档-中文版.zip

    赠送Maven依赖信息文件:junit-jupiter-engine-5.8.2.pom; 包含翻译后的API文档:junit-jupiter-engine-5.8.2-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.junit.jupiter:junit-jupiter-engine:5.8.2; ...

    junit-jupiter-api-5.4.2-API文档-中英对照版.zip

    赠送Maven依赖信息文件:junit-jupiter-api-5.4.2.pom; 包含翻译后的API文档:junit-jupiter-api-5.4.2-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.junit.jupiter:junit-jupiter-api:5.4.2; ...

    junit-platform-engine-1.6.3.jar

    junit-platform-engine-1.6.3.jarjunit-platform-engine-1.6.3.jar

    junit-platform-launcher-1.8.0-M1-API文档-中文版.zip

    赠送Maven依赖信息文件:junit-platform-launcher-1.8.0-M1.pom; 包含翻译后的API文档:junit-platform-launcher-1.8.0-M1-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.junit.platform:junit-platform-...

    junit-jupiter-params-5.7.2-API文档-中文版.zip

    赠送Maven依赖信息文件:junit-jupiter-params-5.7.2.pom; 包含翻译后的API文档:junit-jupiter-params-5.7.2-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.junit.jupiter:junit-jupiter-params:5.7.2; ...

    junit-platform-launcher-1.7.0.jar

    junit-platform-launcher-1.7.0.jarjunit-platform-launcher-1.7.0.jar

    junit-platform-launcher-1.8.0-M1-API文档-中英对照版.zip

    赠送Maven依赖信息文件:junit-platform-launcher-1.8.0-M1.pom; 包含翻译后的API文档:junit-platform-launcher-1.8.0-M1-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.junit.platform:junit-...

    junit-platform-commons-1.8.2-API文档-中英对照版.zip

    赠送Maven依赖信息文件:junit-platform-commons-1.8.2.pom; 包含翻译后的API文档:junit-platform-commons-1.8.2-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.junit.platform:junit-platform-...

    junit-jupiter-5.5.1.rar

    JUnit5相关的 JUnit Jupiter.jar依赖包,主要包含: junit-jupiter-5.5.1.jar,junit-jupiter-api-5.5.1.jar,junit-jupiter-engine-5.5.1.jar,junit-jupiter-params-5.5.1.jar,junit-jupiter-api-5.4.2.jar,...

    junit-jupiter-params-5.8.0-M1-API文档-中文版.zip

    赠送Maven依赖信息文件:junit-jupiter-params-5.8.0-M1.pom; 包含翻译后的API文档:junit-jupiter-params-5.8.0-M1-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.junit.jupiter:junit-jupiter-params:5.8.0...

    junit-jupiter-engine-5.4.2-API文档-中文版.zip

    赠送Maven依赖信息文件:junit-jupiter-engine-5.4.2.pom; 包含翻译后的API文档:junit-jupiter-engine-5.4.2-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.junit.jupiter:junit-jupiter-engine:5.4.2; ...

    junit-platform-launcher-1.6.1-API文档-中文版.zip

    赠送Maven依赖信息文件:junit-platform-launcher-1.6.1.pom; 包含翻译后的API文档:junit-platform-launcher-1.6.1-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.junit.platform:junit-platform-launcher:...

    junit-jupiter-params-5.5.2-API文档-中文版.zip

    赠送Maven依赖信息文件:junit-jupiter-params-5.5.2.pom; 包含翻译后的API文档:junit-jupiter-params-5.5.2-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.junit.jupiter:junit-jupiter-params:5.5.2; ...

    junit-vintage-engine-5.5.2-API文档-中文版.zip

    赠送Maven依赖信息文件:junit-vintage-engine-5.5.2.pom; 包含翻译后的API文档:junit-vintage-engine-5.5.2-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.junit.vintage:junit-vintage-engine:5.5.2; ...

    junit-platform-launcher-1.6.2.jar

    junit-platform-launcher-1.6.2.jarjunit-platform-launcher-1.6.2.jarjunit-platform-launcher-1.6.2.jar

    junit-platform-engine-1.8.2-API文档-中文版.zip

    赠送Maven依赖信息文件:junit-platform-engine-1.8.2.pom; 包含翻译后的API文档:junit-platform-engine-1.8.2-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.junit.platform:junit-platform-engine:1.8.2;...

    junit-jupiter-api-5.5.2-API文档-中文版.zip

    赠送Maven依赖信息文件:junit-jupiter-api-5.5.2.pom; 包含翻译后的API文档:junit-jupiter-api-5.5.2-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.junit.jupiter:junit-jupiter-api:5.5.2; 标签:junit...

    junit-jupiter-api-5.8.0-M1-API文档-中文版.zip

    赠送Maven依赖信息文件:junit-jupiter-api-5.8.0-M1.pom; 包含翻译后的API文档:junit-jupiter-api-5.8.0-M1-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.junit.jupiter:junit-jupiter-api:5.8.0-M1; ...

Global site tag (gtag.js) - Google Analytics