您的位置:首页 > 移动开发

Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be ope

2016-12-15 20:14 716 查看
    今天在做java main函数调用注入到spring容器中的方法时报了一个错误,如下:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at com.b505.weixin.utils.Test.main(Test.java:27)
Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
... 13 more 
   报这种错误的原因就是:加载spring配置文件时路径 路径出现错误了;

   注意:java中加载spring配置文件applicationContext.xml或者bean.xml时,要将这两个文件放在系统的根目录下(即src下),只有这样在加载时才不会出现错误。

   下面是文件放在根目录下的加载方法

String paths[]={"applicationContext.xml","applicationContext-security.xml"};

//加载spring的配置文件
ApplicationContext context=new ClassPathXmlApplicationContext(paths);

需要注意的是当加载多个spring配置文件时要用数
maven项目的系统根目录默认是src/main/java和src/main/resources,而不是src,所以beans.xml文件必须放到src/main/java和src/main/resources下面的文件夹或者包中,否则就会报上面的错误:java.io.FileNotFoundException: class path resource [beans.xml] cannot
be opened because it does not exit.

还有一点要注意,当applicationContext.xml配置文件放在src/main/resources/spring路径下,加载的要这样写

String paths[]={"spring/applicationContext.xml","spring/applicationContext-security.xml"};
//加载spring的配置文件
ApplicationContext context=new ClassPathXmlApplicationContext(paths);
AccessTokenUtil atu=(AccessTokenUtil) context.getBean(AccessTokenUtil.class);
否则就会报错:java.io.FileNotFoundException: class path resource
[beans.xml] cannot be opened because it does not exit.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐