gpt4 book ai didi

java - Spring找不到bean xml配置文件: "Failed to import bean definitions from relative location"

转载 作者:行者123 更新时间:2023-12-01 16:23:16 25 4
gpt4 key购买 nike

我在 Eclipse 中的资源文件夹 (src/main/resources) 包含- beans.xml 和- person.xml

enter image description here

在 beans.xml 中我使用 <import resource="person.xml"/>

如果我通过

加载上下文
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"beans.xml");

出现错误“无法从相对位置导入 Bean 定义”[person.xml]。

如果我用 person.xml 的内容替换导入,它就可以工作。为什么我无法使用导入资源?

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

<!-- Works if used
<bean id="person" class="com.example.demo.domain.Person"></bean>
-->
<!-- Does not work if used
<import resource="classpath:person.xml"/>
-->

</beans>

person.xml的内容

<?xml version="1.0" encoding="UTF-8"?>
<beans>

<bean id="person" class="com.example.demo.domain.Person"></bean>

</beans>

最佳答案

您可以尝试使用 <import resource="classpath:person.xml"/>

而不是 <import resource="person.xml"/>

关于java - Spring找不到bean xml配置文件: "Failed to import bean definitions from relative location",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62213569/

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