zoukankan      html  css  js  c++  java
  • Java Web中如何访问WEB-INF下的XML文件

    Java读取WEB-INF下XML文件并进行解析

    转自:http://blog.itpub.net/30066956/viewspace-1375249/

    分类: Java技术

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

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

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

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

    [java] view plaincopy
     
     
     
    1. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");  
    然后获取相应的bean。

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

    [java] view plaincopy
     
     
     
    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。

    [java] view plaincopy
     
     
     
    1. ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml");  

    然后获取相应的bean。

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

    [java] view plaincopy
     
     
     
    1. @RunWith(SpringJUnit4ClassRunner.class)  
    2. @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/applicationContext.xml"})   
    只需要在相应的Test类前面加上此两个注解(第二个注解用来指明Spring的配置文件位置),就可以在Junit Test类使用中Spring提供的依赖注入功能。

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

    [java] view plaincopy
     
     
     
    1. package com.sohu.group.service.external;  
    2.   
    3. import java.util.List;  
    4.   
    5. import org.junit.Test;  
    6. import org.junit.runner.RunWith;  
    7. import org.springframework.beans.factory.annotation.Autowired;  
    8. import org.springframework.test.context.ContextConfiguration;  
    9. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
    10.   
    11. @RunWith(SpringJUnit4ClassRunner.class)  
    12. @ContextConfiguration({"file:src/main/webapp/WEB-INF/applicationContext.xml"})  
    13. public class SuFriendServiceImplOverRMITest {  
    14.   
    15.     @Autowired  
    16.     private SuFriendService suFriendService;  
    17.       
    18.     @Test  
    19.     public void getUserFollowerListTest(){  
    20.         List list = suFriendService.getUserFollowerList("liug_talk@163.com");  
    21.         System.out.println("------"+list);  
    22.     }  
    23. }  
  • 相关阅读:
    工作流 —— AWS
    ant design —— form 生成 嵌套json数据
    react 获取子组件的 state (转发)
    React 调用子组件的函数(函数)
    react js 组件传参(转发)
    链接脚本文件(.ld .lds)详解
    gcc ------ -ffunction-sections -fdata-sections -gc-sections 参数详解
    GNU ARM 汇编伪指令
    STM32 CM3/CM4 ------ startup.s 文件分析 ------ GCC RT-Thread Studio 版本
    __attribute__之section详解 ------ 把函数指定到具体某个section 之 RT-thread 实例详解
  • 原文地址:https://www.cnblogs.com/chenhonggao/p/9306642.html
Copyright © 2011-2022 走看看