【问题标题】:Spring cannot find bean xml configuration file: "Failed to import bean definitions from relative location"Spring 找不到 bean xml 配置文件:“无法从相对位置导入 bean 定义”
【发布时间】: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


    【解决方案1】:

    你可以试试&lt;import resource="classpath:person.xml"/&gt;

    而不是&lt;import resource="person.xml"/&gt;

    【讨论】:

    • 然后我得到“配置问题:无法从 URL 位置 [classpath:person.xml] 导入 bean 定义”
    • 能分享bean.xml和person.xml的内容吗
    • 我在上面的问题中添加内容
    • 使用以下 person.xml &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;bean id="person" class="com.example.demo.domain.Person"&gt;&lt;/bean&gt; &lt;/beans&gt;
    猜你喜欢
    • 1970-01-01
    • 2012-08-20
    • 2020-11-24
    • 2013-07-22
    • 2011-09-01
    • 2017-04-09
    • 1970-01-01
    • 2012-10-05
    • 2015-02-02
    相关资源
    最近更新 更多