2014-06-27 11 views
0

我想這樣學習春天,我從世界你好錯誤調用ClassPathResource.getInputStream()時

開始

我學會了從這個地址「http://javacpplus.blogspot.com/2012/04/spring-framework-with-netbeans-70.html[url=http://http://javacpplus.blogspot.com/2012/04/spring-framework-with-netbeans-70.html]webpage[/url]

做一個Hello World這是我的代碼,我做兩個班。 1 HelloWorld.java和2 MainApp.java 我可以把一個XML是在beans.xml

檢查這是出於 類HelloWorld.java

package com.hello; 

/** * * @author bobfuad */ public class HelloWorld { 
    private String message; 

    public void setMessage(String message){ 
     this.message = message; } 

    public void getMessage(){ 
     System.out.println("Your Message : " + message); } } 

類MainApp.java

package com.hello; 

mport org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
/** 
* 
* @author bobfuad 
*/ 
public class MainApp { 
    private static Resource ClassPathApplicationContext; 
    public static void main(String[] args) { 
     ApplicationContext context; 
     context = new ClassPathXmlApplicationContext("Beans.xml"); 

     HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); 

     obj.getMessage(); 
    } 
} 

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" 

     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
"> 
<bean id="helloWorld" class="com.hello.HelloWorld"> 
     <property name="message" value="Hello World!"/> 
    </bean> 
</beans> 

它是我的代碼,但是當我運行這個程序,存在有問題

,這是問題

Jun 27, 2014 8:48:44 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh 
INFO: Refreshing org[email protected]e3849c: startup date [Fri Jun 27 08:48:44 PDT 2014]; root of context hierarchy 
Jun 27, 2014 8:48:44 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from class path resource [Beans.xml] 
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [Beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [Beans.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 com.hello.MainApp.main(MainApp.java:18) 
Caused by: java.io.FileNotFoundException: class path resource [Beans.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 
Java Result: 1 
BUILD SUCCESSFUL (total time: 0 seconds) 

請幫幫我,謝謝

+0

請提供更有用的標題 – Coffee

回答

0

像錯誤消息指出,在beans.xml是不是在你的類路徑中(在給定的路徑,即根)。如果您已將文件放置在com/hello的Java類旁邊,請在主要方法中使用路徑com/hello/Beans.xml