【问题标题】:loading xml application context ioexception spring加载xml应用程序上下文ioexception spring
【发布时间】:2012-03-28 07:38:49
【问题描述】:

我开始学习 Spring 框架,当我运行应用程序时,我收到一个 IOException,说 xml 文件不存在,但它位于根文件夹中。这是小代码:package org.koushik.javabrains;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DrawingApp {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        Triangle triangle = (Triangle)context.getBean("triangle");

        triangle.draw();

    }

}

xml:

<beans>

<bean id="triangle" class="org.koushik.javabrains.Triangle">
    <property name="type" value="Equilateral"/>
</bean>

</beans>

项目如下所示:

当我使用 BeanFactory 接口但使用 ApplicationContext 时,我得到了这个错误。我尝试将 xml 文件放入 src 文件夹,但它也不起作用。感谢您的帮助

【问题讨论】:

    标签: java xml spring


    【解决方案1】:

    您需要将 spring.xml 放在 src 文件夹中,而不是根文件夹中,因为 ClassPathXmlApplicationContext 从类路径中读取。

    【讨论】:

    • 非常感谢,看来我把 spring.xml 文件和其他类一起放在了包中,而不是放在 src 文件夹中。
    猜你喜欢
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 2019-08-11
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 2019-06-16
    相关资源
    最近更新 更多