Spring找不到bean xml配置文件:“无法从相对位置导入bean定义”

问题描述 投票:0回答:1

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> 

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

java xml spring applicationcontext
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.