微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java-线程“ main”中的异常org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException

最近,我正在学习Spring框架.所以我正在尝试检查依赖注入在Spring框架中如何工作.结果,我创建了一个新的Java项目,并使用基于构造函数XML的配置练习了依赖注入代码.运行我的项目后,我收到此错误….

Line 24 in XML document from class path resource [com/mir00r/beans.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 24; columnNumber: 9; cvc-complex-type.2.3: Element ‘beans’ cannot have character [children], because the type’s content type is element-only.

Jul 09, 2016 11:22:11 PM org.springframework.context.support.ClasspathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClasspathXmlApplicationContext@7ee361ad: startup date [Sat Jul 09 23:22:11 BDT 2016]; root of context hierarchy
Jul 09, 2016 11:22:11 PM org.springframework.beans.factory.xml.XmlBeanDeFinitionReader loadBeanDeFinitions
INFO: Loading XML bean deFinitions from class path resource [com/mir00r/beans.xml]
Exception in thread “main” org.springframework.beans.factory.xml.XmlBeanDeFinitionStoreException: Line 24 in XML document from class path resource [com/mir00r/beans.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 24; columnNumber: 9; cvc-complex-type.2.3: Element ‘beans’ cannot have character [children], because the type’s content type is element-only.

at org.springframework.beans.factory.xml.XmlBeanDeFinitionReader.doLoadBeanDeFinitions(XmlBeanDeFinitionReader.java:396)
at org.springframework.beans.factory.xml.XmlBeanDeFinitionReader.loadBeanDeFinitions(XmlBeanDeFinitionReader.java:334)
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.(ClasspathXmlApplicationContext.java:139)
at org.springframework.context.support.ClasspathXmlApplicationContext.(ClasspathXmlApplicationContext.java:83)
at com.mir00r.Main_Test.main(Main_Test.java:18)

Caused by: org.xml.sax.SAXParseException; lineNumber: 24; columnNumber: 9; cvc-complex-type.2.3: Element ‘beans’ cannot have character [children], because the type’s content type is element-only.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:458)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3237)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidComplexType(XMLSchemaValidator.java:3200)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidType(XMLSchemaValidator.java:3160)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processElementContent(XMLSchemaValidator.java:3062)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleEndElement(XMLSchemaValidator.java:2140)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(XMLSchemaValidator.java:859)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2973)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
at org.springframework.beans.factory.xml.XmlBeanDeFinitionReader.doLoadBeanDeFinitions(XmlBeanDeFinitionReader.java:388)
… 14 more
C:\Users\razza_000\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD Failed (total time: 28 seconds)

这是我的代码…….

Employee.java文件

 package com.mir00r;

 /**
  * 
  * @author Abdur Razzak <abdur.razzaq06@ail.com>
  */
 public class Employee {
     private int id;
     private String name;
     private Address address; // create object from Address class

     public Employee() {
     }

     public Employee(int id) {
         this.id = id;
     }

     public Employee(String name) {
         this.name = name;
     }

    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public Employee(int id, String name, Address address) {
        this.id = id;
        this.name = name;
        this.address = address;
    }

    public void show () {
        System.out.println(id + " : " + name);
        System.out.println(address);
    }
 }

Address.java文件

   package com.mir00r;

  /**
  * 
  * @author Abdur Razzak <abdur.razzaq06@ail.com>
  */
 public class Address {
     private final String state;
     private final String city;
     private final String country;

     public Address(String state, String city, String country) {
         this.state = state;
         this.city = city;
         this.country = country;
     }

     @Override
     public String toString() {
         return "Address{" + "state=" + state + ", city=" + city + ",  country=" + country + '}';
     }
 }

Main_Test.java文件

     package com.mir00r;

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

     /**
      * 
      * @author Abdur Razzak <abdur.razzaq06@ail.com>
      */
      public class Main_Test {
          public static void main(String[] args) {
               ApplicationContext ac = new ClasspathXmlApplicationContext("com/mir00r/beans.xml");
               Employee em = (Employee)ac.getBean("emp");
               em.show();
          }
      }

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"
   xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

     <bean id="a1" class="com.mir00r.Address">
         <constructor-arg value="Badda"></constructor-arg>
         <constructor-arg value="Dhaka"></constructor-arg>
         <constructor-arg value="Bangladesh"></constructor-arg>
     </bean>

     // Dependency Injection Example using Constructor
     <bean id="emp" class="com.mir00r.Employee">
         <constructor-arg value="10" type="int" ></constructor-arg>
         <constructor-arg value="Abdur Razzak" ></constructor-arg>
         <constructor-arg>
             <ref bean="a1"></ref>
         </constructor-arg>
     </bean>
 </beans>  

这是我的项目结构照片

enter image description here

我现在能做什么 ….????

这是我的错误图片

enter image description here

解决方法:

A-说明

Bean文件中的注释行无效.您正在使用错误的注释标记,而不是使用“ //”而不是“<!-->”,这会导致解析错误(org.xml.sax.SAXParseException).

这是一个xml文件,以下注释行无效;

// Dependency Injection Example using Constructor

并且以下注释行有效;

<!-- Do the comments here, that's all -->

B-解决方

使用下面提供的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"
   xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="a1" class="com.mir00r.Address">
        <constructor-arg value="Badda"/>
        <constructor-arg value="Dhaka"/>
        <constructor-arg value="Bangladesh"/>
    </bean>

    <!-- // Dependency Injection Example using Constructor -->
    <bean id="emp" class="com.mir00r.Employee">
        <constructor-arg value="10" type="int" />
        <constructor-arg value="Abdur Razzak" />
        <constructor-arg>
            <ref bean="a1"/>
        </constructor-arg>
    </bean>
</beans>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。