0

我尝试执行我的第一个 Spring 应用程序,但出现错误。我是春天的新手,你能在这里帮助我吗,将不胜感激。这是代码

 package com.bam.springexample;

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

public class MainApp {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        HelloSpring obj = (HelloSpring)context.getBean("msgid");
        obj.getMessage();
    }

}

这是 POJO 类.....

package com.bam.springexample;

public class HelloSpring {
private String message;

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

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

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.0.xsd">

   <bean id="msgid" class="com.bam.springexmple.HelloSpring">
       <property name="message" value="Hello Bamadeva Its your first Spring Program ."/>
   </bean>

</beans>

我得到的错误是

Jun 16, 2013 10:12:22 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1608e05: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1608e05]; startup date [Sun Jun 16 10:12:22 IST 2013]; root of context hierarchy
Jun 16, 2013 10:12:22 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:320)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:290)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:142)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:158)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:184)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:112)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:79)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:97)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:411)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:338)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:122)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:66)
    at com.bam.springexample.MainApp.main(MainApp.java:13)
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:142)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:307)
    ... 12 more

提前致谢...

在我将 bean.sml 放入 src 文件夹后在这里编辑

Caused by: org.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanPIData(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanPIData(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanPI(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:78)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:361)
    ... 13 more
4

4 回答 4

3

Beans.xml找不到bean 文件。你应该放在Beans.xml你的类路径中。

在此处输入图像描述

只需右键单击srceclipse中的文件夹,然后单击new然后other创建一个新的xml文件。将您的所有代码beans.xml放在该文件中。

 <bean id="helloWorld" class="com.practice.HelloWorld">
   <property name="message" value="Hello World!"/>
 </bean>

我如上所述声明了bean。

如下调用 bean。

 ApplicationContext context = 
         new ClassPathXmlApplicationContext("Beans.xml");

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

这对我有用。我可以得到消息的价值。

于 2013-06-16T05:01:22.623 回答
1

Spring在类路径上寻找Beans.xml而不是找到它。

该文件很可能位于您当前的工作目录或其他对人类显而易见的地方,但它必须位于 Java 类路径上ClassPathXmlApplicationContext才能正确找到它。

于 2013-06-16T04:57:11.953 回答
1

将 Beans.xml 放在项目构建路径中。以下是步骤

  1. 右键单击项目=>选择构建路径=>选择配置构建路径

  2. 选择 Java Build Path => Source => 点击Included

在此处输入图像描述

  1. 然后单击添加 并添加您的 Beans.xml

  2. 最终配置是这样的:

在此处输入图像描述

  1. 单击完成并保存所有更改。使用ctrl+F11运行项目。
于 2019-11-06T14:50:05.933 回答
0

转到应用程序的物理位置并放置 .xml 文件

例如:C:\Workspace\application-name\target\classes

于 2014-02-21T16:52:51.323 回答