nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cann
2012-11-12 10:59
751 查看
spring整合struts,在action里读取spring配置文件时,这么写:
ApplicationContext ctx=new FileSystemXmlApplicationContext("WebContent/WEB-INF/config/spring/web-application-config.xml");
那么在触发action时会报错:
javax.servlet.ServletException: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [D:\Program Files\apache-tomcat-6.0.26\bin\WebContent\WEB-INF\config\spring\web-application-config.xml]; nested exception is java.io.FileNotFoundException: WebContent\WEB-INF\config\spring\web-application-config.xml (The system cannot find the path specified)
如果改为:
ApplicationContext ctx=new ClassPathXmlApplicationContext("WebContent/WEB-INF/config/spring/web-application-config.xml");
又会报错:
javax.servlet.ServletException: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [WebContent/WEB-INF/config/spring/web-application-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [WebContent/WEB-INF/config/spring/web-application-config.xml] cannot be opened because it does not exist
这是因为调用的是ClassPathXmlApplicationContext,而WebContent/WEB-INF/config/spring/web-application-config.xml并不在classespath路径中。
可见是加载ApplicationContext的方法有问题,那么,我们来查下加载spring配置文件有几种方法:
一:Spring中的几种容器都支持使用xml装配bean,包括:
XmlBeanFactory ,
ClassPathXmlApplicationContext ,
FileSystemXmlApplicationContext ,
XmlWebApplicationContext
加载这些容器的配置文件的xml有一下几种常见的方法:
1:引用资源用XmlBeanFactory(不能实现多个文件相互引用)
Resource resource = new ClassPathResource("appcontext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
从factory中获取相应资源文件中的bean,但是这种bean读不到引用了其他文件中的bean!
2:引用应用上下文用ClassPathXmlApplicationContext
ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ApplicationContext factory=new ClassPathXmlApplicationContext("conf/userConfig.xml");
// src/conf 目录下的
ApplicationContext factory=new ClassPathXmlApplicationContext("file:G:/Test/src/appcontext.xml");
3:用文件系统的路径引用应用上下文用FileSystemXmlApplicationContext
ApplicationContext factory=new FileSystemXmlApplicationContext("src/applicationContext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("classpath:appcontext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("file:G:/Test/src/appcontext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("G:/Test/src/appcontext.xml");
注意:在2、3的加载方式中可以加载多个配置文件,获取到ApplicationContext 对象中
String[] configs = {"applicationContext.xml","user_spring.xml"};
ApplicationContext ctx = new ClassPathXmlApplicationContext(configs);
//ApplicationContext ctx=new FileSystemXmlApplicationContext(configs);
AbstractDao myUserDAO =
(AbstractDao) ctx.getBean("userDao");
4:Web工程定制的加载方法 XmlWebApplicationContext
ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext );
相关文章推荐
- class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: cla
- FAQ(19):java.io.FileNotFoundException: class path resource [resources/applicationContext.xml] cannot
- Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be ope
- java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because
- 异常解决 java.io.FileNotFoundException: class path resource [spring/applicationContext_*.xml]
- Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be ope
- Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be ope
- 异常解决 java.io.FileNotFoundException: class path resource [spring/applicationContext_*.xml]
- java.io.FileNotFoundException: class path resource [spring/ApplicationCont.xml] cannot be opened be
- nested exception is java.io.FileNotFoundException: class path resource [spring/spring-datasource-mog
- nested exception is java.io.FileNotFoundException: class path resource [spring/spring-datasource-mog
- java cxf nested exception is java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf-ex
- [SSM/Junit]java.io.FileNotFoundException: class path resource [spring-mybatis.xml] cannot be opened
- java.io.FileNotFoundException: class path resource [META-INF/xfire/services.xml] cannot be opened be
- Caused by: java.io.FileNotFoundException: class path resource [com/ncs/bean/News.hbm.xml] cannot be
- nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/
- junit测试时找不到资源路径java.io.FileNotFoundException: class path resource [config/EBSConfig.xml] cannot be o
- nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/
- java.io.FileNotFoundException: class path resource [META-INF/xfire/services.xml] cannot be opene
- 异常解决 java.io.FileNotFoundException: class path resource [spring/springmvc.xml]