spring 的启动 之加载文件 applicationContext.xml
2013-03-22 13:00
387 查看
我们在junit单元测试里写了一个方法,对applicationContext.xml进行手工的调用
可是junit总是报错 说找不到文件applicationContext.xml,令人奇怪的是 如果用
把
而是用代码进行加载配置文件的时候 encodedResource为class path resource [classpath:config/xml/applicationContext.xml]
两相比较发现多了classpath值。
也就是说 ClassPathResource res = new ClassPathResource 是默认在classpath下进行搜索的。
下一篇我要接着分析一下
package tc.springtest; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Component; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; import org.hamcrest.SelfDescribing; import org.apache.commons.logging.LogFactory; //@Component // @Transactional // @RunWith(SpringJUnit4ClassRunner.class) // @ContextConfiguration(locations = { "classpath:config/xml/applicationContext.xml" }) public class test { @Test //@Rollback(true) public void should_return_XXX_when_given_xx(){ System.out.println(123); ClassPathResource res = new ClassPathResource("classpath:config/xml/applicationContext.xml"); DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); reader.loadBeanDefinitions(res); } }
可是junit总是报错 说找不到文件applicationContext.xml,令人奇怪的是 如果用
@ContextConfiguration(locations = { "classpath:config/xml/applicationContext.xml" })程序是可以找到该文件的。单步调试后发现报错的地方在XmlBeanDefinitionReader的第341行loadBeanDefinitions方法里,是在328行
InputStream inputStream = encodedResource.getResource().getInputStream();抛出的异常,在341行
catch (IOException ex) { throw new BeanDefinitionStoreException( "IOException parsing XML document from " + encodedResource.getResource(), ex); }被捕捉到的。
把
@ContextConfiguration(locations = { "classpath:config/xml/applicationContext.xml" })打开后进行对比发现loadBeanDefinitions里的参数encodedResource为class path resource [config/xml/applicationContext.xml]
而是用代码进行加载配置文件的时候 encodedResource为class path resource [classpath:config/xml/applicationContext.xml]
两相比较发现多了classpath值。
也就是说 ClassPathResource res = new ClassPathResource 是默认在classpath下进行搜索的。
下一篇我要接着分析一下
ClassPathResource res = new ClassPathResource("config/xml/applicationContext.xml");的代码过程,以后还要对
@ContextConfiguration(locations = { "classpath:config/xml/applicationContext.xml" })进行下代码跟踪,看一下两个的具体区别。
相关文章推荐
- Tomcat启动时,spring加载配置文件applicationContext.xml出错,抛出nested exception is og.xml.sax.SAXParseException
- 【Spring学习笔记-2】Myeclipse下第一个Spring程序-通过ClassPathXmlApplicationContext加载配置文件
- spring学习笔记(3)——配置文件applicationContext.xml的加载
- Spring 2.0.8 和 2.5 applicationContext.xml 头文件写法 Spring启动异常: cvc-elt.1: Cannot find the declaration of element 'beans'
- 使用Spring中的ClassPathXmlApplicationContext加载配置文件
- 加载spring时applicationContext.xml文件出错
- 请慎用spring-ClassPathXmlApplicationContext手动加载spring配置文件
- 加载spring时applicationContext.xml文件出错
- spring 配置文件无法加载,junit找不到xml配置文件java.lang.IllegalStateException: Failed to load ApplicationContext
- Spring加载配置文件applicationContext.xml的方式
- 请慎用spring-ClassPathXmlApplicationContext手动加载spring配置文件
- applicationContext.xml文件中怎么配置struts2和spring配置文件的加载路径
- 解释为什么Spring 默认会加载/WEB-INF/applicationContext.xml
- Spring配置文件applicationContext.xml(3)开启事务和注解
- Spring applicationContext.xml 文件 路径配置 (2)
- spring配置文件 applicationContext.xml
- Spring源码学习--Spring配置解析文件ApplicationContext.xml(一)
- spring配置文件applicationContext.xml不能自动提示的解决方法
- Spring -- 如何为applicationContext.xml 添加 util 的 *.xsd文件
- Spring 配置文件applicationContext.xml详解