简体   繁体   English

调用ClassPathResource.getInputStream()时出错

[英]Error when calling ClassPathResource.getInputStream()

i want so study Spring,i start from hello World 我想念春天,我从世界开始

I learned to make a hello world from this address " 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] " 我从这个地址学会了打个招呼世界“ http://javacpplus.blogspot.com/2012/04/spring-framework-with-netbeans-70.html[url=http://http://http://javacpplus.blogspot .com / 2012/04 / spring-framework-with-netbeans-70.html]网页[/ url]

And this is my code, i make two class. 这是我的代码,我上两节课。 1 HelloWorld.java And 2 MainApp.java and i Make one xml that is Beans.xml 1 HelloWorld.java和2 MainApp.java和我制作一个Beans.xml的xml

check this is out class HelloWorld.java 检查这是类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);    } }

class MainApp.java 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 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>

that it's my code, but when i run this program, there is have problem 那是我的代码,但是当我运行该程序时,出现了问题

and this is problem 这是问题

Jun 27, 2014 8:48:44 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@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)

please help me, thanks 请帮助我,谢谢

Like the error message says, Beans.xml is not found in your classpath (in the given path, ie root). 如错误消息所述,在您的类路径(在给定路径,即根目录)中找不到Beans.xml。 If you have placed the file next to the Java classes in com/hello , then use the path com/hello/Beans.xml in your main method. 如果已将文件放在com/hello的Java类旁边,则在main方法中使用路径com/hello/Beans.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM