我简单的“Hello World”程序没有运行到Spring

时间:2014-10-20 03:43:37

标签: java xml spring spring-mvc

我试图运行我的简单程序,我将spring framework jar filescommon-logging jar files添加到我的参考库中。

这是我的第一个程序 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.springframework.context.support.ClassPathXmlApplicationContext@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 个答案:

答案 0 :(得分:0)

http://www.tutorialspoint.com/spring/spring_hello_world_example.htm

在这个例子中,你必须将bean.xml放在“com.rajendra.lesson01”包之外......把它放在资源包中。 我希望这可以帮到你。

enter image description here