gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:44:50 25 4
gpt4 key购买 nike

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

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

这是我所拥有的图像

enter image description here

但是当我尝试运行时它说:类路径资源 [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

还有一些其他问题也有同样的错误,但我无法将这些问题与我的问题联系起来。帮我找出我做错了什么。

//------------------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>

最佳答案

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

关于java - 应用程序无法加载 applicationContent.xml。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58498761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com