2014-10-20 122 views
0

我試圖運行我的簡單程序,我也在我的參考庫中添加了spring framework jar filescommon-logging jar files我的簡單的「Hello World」程序沒有運行到Spring

這是我的第一個程序說HelloWorld.java

package com.rajendra.lesson01; 

public class HelloWorld { 

    private String message; 

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

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

這裏是我的另一個節目說MainProgram.java

package com.rajendra.lesson01; 

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

public class MainProgram { 
    public static void main(String[] args){ 

     ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml"); 
     HelloWorld hw=(HelloWorld) ac.getBean("helloWorld"); 

     hw.getMessage(); 
    } 
} 

,這裏是我的最後一個bean.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.xsd"> 

<bean id="helloWorld" class="com.rajendra.lesson01.HelloWorld"> 
    <property name="message" value="My name is foo.." /> 
</bean> 

</beans> 

一切按罰款教程,但我輸出說:

Oct 20, 2014 9:12:19 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 
INFO: Refreshing org[email protected]1c12fb0: startup date [Mon Oct 20 09:12:19 IST 2014]; root of context hierarchy 
Oct 20, 2014 9:12:19 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from class path resource [bean.xml] 
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [bean.xml]; nested exception is java.io.FileNotFoundException: class path resource [bean.xml] cannot be opened because it does not exist 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251) 
    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:129) 
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:540) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) 
    at com.rajendra.lesson01.MainProgram.main(MainProgram.java:9) 
Caused by: java.io.FileNotFoundException: class path resource [bean.xml] cannot be opened because it does not exist 
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329) 
    ... 13 more 

這裏發生了什麼,請幫助?並告訴我你們如何找到錯誤?

幫助將不勝感激!

+1

的xml文件必須在classpath的根目錄(與您正在使用的路徑 - 不僅僅是文件名),即(與Maven)的src /主/資源/ beans.xml中 – 2014-10-20 03:47:21

+0

我不知道你在說什麼,但在這裏你可以看到我的[** Image **](http://postimg.org/image/85fyp7lpb/) – 2014-10-20 03:52:44

回答

相關問題