Java读取WEB-INF下的配置文件

xiaoxiao2021-02-28  54

一,配置文件位于类路径下

例如下图的spring-dao.xml

在代码中:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-dao.xml");  

在单元测试中:

@RunWith(SpringJUnit4ClassRunner.class)  @ContextConfiguration(locations={"classpath:spring-dao.xml"})   

二,配置文件位于WEB-INF目录下

在代码中:

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

在单元测试中:

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

需要注意的是在单元测试中一定要注意版本是否冲突的问题,否则很容易出问题!

转载请注明原文地址: https://www.6miu.com/read-2614628.html

最新回复(0)