Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在spring中从xml文件获取bean时出错_Java_Android_Spring_Javabeans - Fatal编程技术网

Java 在spring中从xml文件获取bean时出错

Java 在spring中从xml文件获取bean时出错,java,android,spring,javabeans,Java,Android,Spring,Javabeans,我是春天的新手。我创建了config.xml,在其中我创建了bean。当我尝试使用getBean时,出现以下错误: 从类路径资源解析XML文档时发生IOException [config.xml];嵌套异常为java.io.FileNotFoundException:class 无法打开路径资源[config.xml],因为它不存在 我刚才在用这条线 ApplicationContext context= new ClassPathXmlApplicationContext(" con

我是春天的新手。我创建了
config.xml
,在其中我创建了bean。当我尝试使用
getBean
时,出现以下错误:

从类路径资源解析XML文档时发生IOException [config.xml];嵌套异常为java.io.FileNotFoundException:class 无法打开路径资源[config.xml],因为它不存在

我刚才在用这条线

ApplicationContext context= new ClassPathXmlApplicationContext(" config.xml");

Student Student1=(Student) context.getBean("s1");
然后,我将代码更改为:

ApplicationContext context= new ClassPathXmlApplicationContext("classpath*: config.xml");
但是,现在我遇到了新的错误: 没有名为“s1”的bean可用

我的主要功能出现在
src/main/java/org/example/App.java

My config.xml出现在
src/main/java/config.xml

config.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:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="s1" class="org.example.Student"> <!--org.example is groupId-->
    <property name="studentId">
        <value>22344</value>
    </property>
    <property name="studentName">
        <value>AP</value>
    </property>
    <property name="studentAddress">
        <value>L</value>
    </property>
</bean>

22344
美联社
L

您必须将xml文件
config.xml
放入src/main/resources(默认情况下,maven在
src/main/Java
中查找Java源文件,在
src/main/resources
中查找资源文件)

创建
ClassPathXmlApplicationContext
对象时,不需要
classpath
关键字,只需使用
ClassPathXmlApplicationContext(“config.xml”)
如下:

ApplicationContext context= new ClassPathXmlApplicationContext("config.xml");
Student student1=(Student) context.getBean("s1");
System.out.println(student1.getStudentId());

请提供config.xml的内容。它应该有一个用
id=“s1”
Hi Pratap定义的bean,我已经提供了内容。你能调查一下吗?嗨,奥利弗,我是按照你说的做的,但还是一样的问题。我需要更改config.xml中的任何内容吗?我将它运行到我的计算机中,它工作得非常好。我使用了新的ClassPathXmlApplicationContext(“config.xml”)你可以在我的答案中看到。我在你的第一篇博文中看到config.xml前面有一个不必要的空格:
(“config.xml”)
,而不是
(“config.xml”)
,谢谢,它成功了。你救了我一天。