java - 在线程"主要" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException 中,java异常

  显示原文与译文双语对照的内容
72 0

最近我在学习 Spring Framework 。所以我尝试检查依赖注入在 Spring Framework 中的工作方式。因此,我创建了一个新的java项目,并使用基于构造式的配置来实施依赖注入代码。在运行我的项目之后,我得到了这个错误。

class path [com/mir00r/beans.xml] 文档中的第 24行无效,嵌套的异常是 org.xml. sax.SAXParseException;:24 ;columnNumber: 9 ;cvc-complex-type.2.3:元素'豆类'不能有字符 [children],因为类型类型的内容仅为元素。

Jul 09,2016 11: 22: 11 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息:Refreshing: startup: startup,上下文层次结构 2016年月09日 11: root: 22: 11 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息的:在线程"主要" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException 中从类路径资源 [com/mir00r/beans.xml] 异常加载 XML bean定义:class path [com/mir00r/beans.xml] 文档中的第 24行无效,嵌套的异常是 org.xml. sax.SAXParseException;:24 ;columnNumber: 9 ;cvc-complex-type.2.3:元素'豆类'不能有字符 [children],因为类型类型的内容仅为元素。

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 )

由:org.xml. sax.SAXParseException; lineNumber引起:24 ;columnNumber: 9 ;cvc-complex-type.2.3:元素'豆类'不能有字符 [children],因为类型类型的内容仅为元素。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 C:Usersrazza_000AppDataLocalNetBeansCache8. 1executor-snippetsrun 。xml: 53: 返回:1生成失败( 总时间:28秒)

这是我的密码。

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

时间:原作者:0个回答

123 1

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>
原作者:
...