2012-03-27 57 views
0

我开始学习Spring框架,当我运行应用程序时,我得到一个IOException,表示xml文件不存在,但它位于根文件夹中。这里是一些小代码:package org.koushik.javabrains;加载xml应用程序上下文ioexception spring

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> 

下面是该项目的样子:

enter image description here

这完美地工作时,我使用的BeanFactory接口,但与ApplicationContext中我得到这个错误。我试图把xml文件放在src文件夹中,但它也没有工作。感谢您的帮助

回答

1

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

+0

非常感谢,似乎我把spring.xml文件与其他类放在包中,而不是放在src文件夹中。 – madcoderz 2012-03-27 23:46:44

+0

没问题。容易犯的错误,一个我过去做过很多次的... – 2012-03-27 23:50:42

相关问题