2017-08-03 51 views
0

我正在嘗試使用SPRING調用三角形bean中的方法draw()的活動。但是,當我嘗試訪問spring.xml文件時遇到了一個錯誤。如何解決SPRING中由FileNotFoundException引起的BeanDefinitionStoreException?

的錯誤是:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring.xml] cannot be opened because it does not exist 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243) 
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127) 
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93) 
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130) 
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) 
    at org.spring.application.SpringApplication.main(SpringApplication.java:23) 
Caused by: java.io.FileNotFoundException: class path resource [spring.xml] cannot be opened because it does not exist 
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) 
    ... 13 more 

這裏是主這是SpringApplication.java:

package org.spring.application; 

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

public class SpringApplication 
{ 

    public static void main(String[] args) 
    { 
     AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); 
     Triangle triangle = (Triangle) context.getBean("Triangle"); 
     triangle.draw(); 
    } 

} 

這裏是在spring.xml代碼

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 

<beans> 
     <bean id="Triangle" class="org.spring.application.Triangle"/> 
</beans> 

這裏是項目文件夾: Project Folder

我正在使用NetBeans IDE。我還包括Spring庫和組件日誌記錄。 有人可以幫助我,並指出如果我做錯了什麼。謝謝。

回答

0

您需要添加軟件包。 new ClassPathXmlApplicationContext("spring.xml");應該是new ClassPathXmlApplicationContext("org/spring/application/spring.xml");

+0

嗨,謝謝!有效。 –

0

您應該將spring.xml放置在資源文件夾中。 使用以下結構: enter image description here

相關問題