假设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. }

最新文章

  1. ruby第一次实践 ”hello world“
  2. Sybase 数据库新增用户,赋权
  3. Java 中包装类wrapped type之间以及和primitive type的比较
  4. Map静态键值对
  5. mysql in 子查询 效率慢 优化(转)
  6. poj1265Area(pick定理)
  7. jQuery 请指出&#39;.bind()&#39;,&#39;.live()&#39;和&#39;.delegate()&#39;的区别
  8. ALAssets的两种用法
  9. Windows Azure VM的两种shut down 方式
  10. android图片处理方法(转)
  11. 使用POI进行Excel操作的总结一——创建Workbook,Sheet,Row以及Cell
  12. ubuntu16.04-x64系统中Jexus web server部署.NetCore和端口分析引发的猜想!
  13. java数据类型转换那点事
  14. Web Magic 总体架构
  15. Android Studio环境下搭建ReactNative
  16. 第六十六天 js操作高级
  17. [HAOI2015]树上操作-树链剖分
  18. widerface---VOC
  19. datagrid点删除,弹出一个确认和取消的消息框
  20. Artech的MVC4框架学习——第六章Model的验证

热门文章

  1. SQL Server触发器的禁用和启用
  2. [原]点击按钮,表格隔行变色:偶数行为黄色背景,奇数行为默认颜色。通过table的getElementsByTagName取得所有的tr,依次遍历,如果是偶数就…………。
  3. 如何用好 Google 搜索引擎?
  4. EF5修改edmx表结构保存后不自动更新tt (转)
  5. 把Wordpress集成到zen-cart里方法 各种修改 经典机制
  6. thinkphp整合系列之rbac的升级版auth权限管理系统demo
  7. css单独设定样式
  8. HDU 5875 st+二分区间
  9. li里元素都浮动 li 在IE6 7 下方会产生4px间隙问题
  10. java程序中抛出异常的两种方式,及异常抛出的顺序