Java 应用程序无法加载applicationContent.xml。为什么?

Java 应用程序无法加载applicationContent.xml。为什么?,java,spring,Java,Spring,我正在尝试一个spring,其中我尝试使用javabeans打印Student类的name属性 像 这是我所拥有的图像 但当我试图运行它时,它说:类路径资源[applicationContext.xml]无法打开,因为它不存在 log4j:WARN No appenders could be found for logger (org.springframework.beans.factory.xml.XmlBeanDefinitionReader). log4j:WARN Please

我正在尝试一个spring,其中我尝试使用javabeans打印Student类的name属性 像


这是我所拥有的图像

但当我试图运行它时,它说:类路径资源[applicationContext.xml]无法打开,因为它不存在

log4j:WARN No appenders could be found for logger (org.springframework.beans.factory.xml.XmlBeanDefinitionReader).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContent.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContent.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.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)
    at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
    at spring1.Test.main(Test.java:11)
Caused by: java.io.FileNotFoundException: class path resource [applicationContent.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
    ... 4 more
log4j:WARN找不到记录器(org.springframework.beans.factory.xml.XmlBeanDefinitionReader)的附加程序。
log4j:警告请正确初始化log4j系统。
线程“main”org.springframework.beans.factory.BeanDefinitionStoreException中的异常:从类路径资源[applicationContent.XML]解析XML文档的IOException;嵌套异常为java.io.FileNotFoundException:无法打开类路径资源[applicationContent.xml],因为它不存在
位于org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
位于org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
位于org.springframework.beans.factory.xml.XmlBeanFactory.(XmlBeanFactory.java:73)
位于org.springframework.beans.factory.xml.XmlBeanFactory.(XmlBeanFactory.java:61)
位于spring1.Test.main(Test.java:11)
原因:java.io.FileNotFoundException:无法打开类路径资源[applicationContent.xml],因为它不存在
位于org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141)
位于org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 4更多
其他一些问题也有同样的错误,但我无法将这些问题与我的问题联系起来。帮我找出我做错了什么

//------------------Test.java-------------------
package spring1;

import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.ClassPathResource;  
import org.springframework.core.io.Resource;  

public class Test {  
public static void main(String[] args) {  
    Resource resource=new ClassPathResource("applicationContent.xml");  
    BeanFactory factory=new XmlBeanFactory(resource);  

    Student student=(Student)factory.getBean("studentbean");  
    student.displayInfo();  
}  
}
---------------------Student.java------------------------
package spring1;

public class Student {  
private String name;  

public String getName() {  
    return name;  
}  

public void setName(String name) {  
    this.name = name;  
}  

public void displayInfo(){  
    System.out.println("Hello: "+name);  
}  
}


---------------------applicationContent.xml--------------
<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  

<bean id="studentbean" class="spring1.Student">  
<property name="name" value="krishna kant"></property>  
</bean>  

</beans> 

/------------Test.java-------------------
软件包spring1;
导入org.springframework.beans.factory.BeanFactory;
导入org.springframework.beans.factory.xml.XmlBeanFactory;
导入org.springframework.core.io.ClassPathResource;
导入org.springframework.core.io.Resource;
公共类测试{
公共静态void main(字符串[]args){
资源资源=新的类路径资源(“applicationContent.xml”);
BeanFactory工厂=新的XmlBeanFactory(资源);
Student=(Student)factory.getBean(“studentbean”);
student.displayInfo();
}  
}
---------------------Student.java------------------------
软件包spring1;
公营班级学生{
私有字符串名称;
公共字符串getName(){
返回名称;
}  
公共无效集合名(字符串名){
this.name=名称;
}  
public void displayInfo(){
System.out.println(“Hello:+name”);
}  
}
---------------------applicationContent.xml--------------

我马上想到的是,屏幕截图上的XML文件名为“applicationContent.XML”,而您的错误消息找不到名为“applicationContext.XML”的文件,不存在。

尝试spring1/applicationContent.xml而不是applicationContent.xml,因为您的applicationContent.xml驻留在spring1中

是的,这是我的问题,如果它存在,那么为什么我得到了错误,它不存在。我不太确定我是否遵循了。我的建议是,您在代码中输入了一个错误,并试图从一个不存在的文件中进行解析,这就是为什么您会得到它不存在的错误。仔细查看您的实际文件名和错误消息中的搜索字符串:一个是“applicationContent N t.xml”,另一个是“applicationContent X t.xml”(内容与上下文),您试图解析的文件“applicationContext”不存在,只有一个文件“applicationContent”。即使将名称从applicationContext.xml更改为applicationContent.xml,我也会遇到相同的错误。那么,恐怕我们需要更多有关您的代码到底在做什么的信息。也许能给我们提供一个最小的可重复的例子?没有
applicationContext.xml
有一个
spring1/applicationContext.xml
@M.Deinum,我也这么想。:)是的,我明白了,它现在起作用了@M.DeinumI我想这都是关于内容和上下文的。
//------------------Test.java-------------------
package spring1;

import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.ClassPathResource;  
import org.springframework.core.io.Resource;  

public class Test {  
public static void main(String[] args) {  
    Resource resource=new ClassPathResource("applicationContent.xml");  
    BeanFactory factory=new XmlBeanFactory(resource);  

    Student student=(Student)factory.getBean("studentbean");  
    student.displayInfo();  
}  
}
---------------------Student.java------------------------
package spring1;

public class Student {  
private String name;  

public String getName() {  
    return name;  
}  

public void setName(String name) {  
    this.name = name;  
}  

public void displayInfo(){  
    System.out.println("Hello: "+name);  
}  
}


---------------------applicationContent.xml--------------
<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  

<bean id="studentbean" class="spring1.Student">  
<property name="name" value="krishna kant"></property>  
</bean>  

</beans>