线程“main”中的异常org.springframework.beans.factory.BeanDefinitionStoreException我正在做一个示例项目我春天

时间:2013-10-29 06:19:56

标签: java spring

我在春天做一个示例项目时出现了一些错误......错误就像这样...... Test.java,applicationContext.java,Student.java这个文件在src文件夹中....

Oct 29, 2013 11:37:15 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@e458c2: startup date [Tue Oct 29 11:37:15 IST 2013]; root of context hierarchy
Oct 29, 2013 11:37:15 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
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:341)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
    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:131)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at springapplication.Main.main(Main.java:13)
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:158)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
    ... 13 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second).......
       My code is this...
       Test.java

import org.springframework.beans.factory.BeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.xml.XmlBeanFactory;
public class Test {
public static void main(String[] args){
    Resource resource=new ClassPathResource("applicationConteXt.xml");
    BeanFactory factory=new XmlBeanFactory(resource);
    Student student=(Student)factory.getBean("studentbean");
    student.displayinfo();
 }

}

4 个答案:

答案 0 :(得分:2)

你必须将它放在maven项目的resources目录中。它会起作用。

答案 1 :(得分:1)

使用我的Eclipse Java项目,我通过将相对路径上方的文件夹设置为源文件夹来修复它。

答案 2 :(得分:1)

beans.xml里面&#34; com / mypackage&#34; (例如:com \ mypackage \ beans.xml);

我如何使用

ClassPathXmlApplicationContext("beans.xml"); ----> ERROR.

有三种解决方案:

  1. 将beans.xml从包中取出并输入默认包名称
  2. ClassPathXmlApplicationContext( com/**default package name**/beans.xml);
    
    1. 如果你将bean.xml放在mypackage(包含你的java文件的包)中。
    2. ClassPathXmlApplicationContext("com/mypackage/beans.xml");
      
      1. 我们可以将beans.xml放在ProjectName\src
      2. ClassPathXmlApplicationContext("beans.xml")
        

答案 3 :(得分:0)

你的applicationContext.xml不在类路径中,就像在项目的src(source)文件夹中一样。将applicationContext.xml放在src文件夹中。 这将有效