【发布时间】:2020-06-05 10:46:18
【问题描述】:
我在 Eclipse 中的资源文件夹 (src/main/resources) 包含 - beans.xml 和 - 人.xml
在 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>
【问题讨论】:
标签: java xml spring applicationcontext