Spring applicationContext.xml:“根元素后面的文档中的标记必须格式正确”

时间:2013-06-12 09:27:20

标签: spring applicationcontext

我搜索了同样的问题,但不幸的是我找不到解决方案。 我面临以下例外:

    Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 32 in XML document from class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 32; columnNumber: 2; The markup in the document following the root element must be well-formed.
    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:131)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:530)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:444)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at Main.main(Main.java:18)
Caused by: org.xml.sax.SAXParseException; lineNumber: 32; columnNumber: 2; The markup in the document following the root element must be well-formed.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1388)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$TrailingMiscDriver.next(XMLDocumentScannerImpl.java:1382)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:489)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:237)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:300)
    at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)
    ... 14 more

这是我的applicationContext.xml:

    <context:component-scan base-package="com.springHibernate" />
    <context:annotation-config />
    <bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/myapp" />
        <property name="username" value="root" />
        <property name="password" value="password" />
    </bean>
    <!-- Beans Declaration -->
    <bean id="User" class="com.mycompany.myapp.core.model.User"/>
    <bean id="Institute" class="com.mycompany.myapp.core.model.Institute"/>

    <!-- User Service Declaration -->
    <bean id="UserBoImpl" class="com.mycompany.myapp.core.bo.impl.UserBoImpl">
        <property name="userDao" ref="UserDaoImpl" />
    </bean>
    <bean id="InstituteBoImpl" class="com.mycompany.myapp.core.bo.impl.InstituteBoImpl">
        <property name="instituteDao" ref="InstituteDaoImpl" />
    </bean>

    <!-- User DAO Declaration -->
    <bean id="UserDaoImpl" class="com.mycompany.myapp.core.dao.impl.UserDaoImpl">
        <property name="sessionFactory" ref="SessionFactory" />
    </bean>
    <bean id="InstituteDaoImpl" class="com.mycompany.myapp.core.dao.impl.InstituteDaoImpl">
        <property name="sessionFactory" ref="SessionFactory" />
    </bean>

</beans>    
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="DataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.mycompany.myapp.core.model.User</value>
            <value>com.mycompany.myapp.core.model.Institute</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="SessionFactory" />
</bean>

我不知道异常的原因是什么...... 感谢您的评论。

1 个答案:

答案 0 :(得分:3)

关闭<beans>标记,然后添加更多bean定义。移动结束beans标记,使其嵌套bean定义。另一个可以使用XML配置文件刻录您的相关内容是使用IDE中的格式工具。永远不要使用工具格式化XML配置,它可以为xml添加额外的空格,这会被解释为破坏您的配置。

简而言之,你这样做:

<beans>
    <bean id="1"/>
</beans>
<bean id="2"/>
<bean id="3"/>

应该是这样的:

<beans>
   <bean id="1"/>
   <bean id="2"/>
   <bean id="3"/>
</beans>

完整配置更改

    <?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="com.springHibernate" />
    <context:annotation-config />
    <bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/mydb" />
        <property name="username" value="root" />
        <property name="password" value="password" />
    </bean>
    <!-- Beans Declaration -->
    <bean id="User" class="com.mycompany.myapp.core.model.User"/>
    <bean id="Institute" class="com.mycompany.myapp.core.model.Institute"/>

    <!-- User Service Declaration -->
    <bean id="UserBoImpl" class="com.mycompany.myapp.core.bo.impl.UserBoImpl">
        <property name="userDao" ref="UserDaoImpl" />
    </bean>
    <bean id="InstituteBoImpl" class="com.mycompany.myapp.core.bo.impl.InstituteBoImpl">
        <property name="instituteDao" ref="InstituteDaoImpl" />
    </bean>

    <!-- User DAO Declaration -->
    <bean id="UserDaoImpl" class="com.mycompany.myapp.core.dao.impl.UserDaoImpl">
        <property name="sessionFactory" ref="SessionFactory" />
    </bean>
    <bean id="InstituteDaoImpl" class="com.mycompany.myapp.core.dao.impl.InstituteDaoImpl">
        <property name="sessionFactory" ref="SessionFactory" />
    </bean>

<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="DataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.mycompany.myapp.core.model.User</value>
            <value>com.mycompany.myapp.core.model.Institute</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="SessionFactory" />
</bean>
</beans>