XML中的弹簧配置文件出错

时间:2016-07-07 06:25:27

标签: java xml spring spring-mvc

我在mvc-dispatcher-servlet.xml中使用spring profile。但是得到错误:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'bean'. One of '{"http://www.springframework.org/schema/beans":beans}' is expected.

我没有在配置文件中添加其他bean,因为我希望在两个配置文件中使用这些bean。需要帮助解决错误。 下面是我的XML,错误来自line- bean id =“transactionManager”         类= “org.springframework.orm.hibernate5.HibernateTransactionManager” >         

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

    <!-- Specifying base package of the Components like Controller, Service, 
        DAO -->
    <context:component-scan base-package="com.mycompany.saas.*" />

    <!-- Getting Database properties -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- Getting Configuration properties -->
    <!-- <context:property-placeholder location="classpath:config.properties" 
        /> -->

    <mvc:annotation-driven />
    <beans profile="default">
        <!-- DataSource -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close">
            <property name="driverClass" value="${database.driverClass}" />
            <property name="jdbcUrl" value="${database.url}" />
            <property name="user" value="${database.username}" />
            <property name="password" value="${database.password}" />
            <property name="acquireIncrement" value="${connection.acquireIncrement}" />
            <property name="minPoolSize" value="${connection.minPoolSize}" />
            <property name="maxPoolSize" value="${connection.maxPoolSize}" />
            <property name="maxIdleTime" value="${connection.maxIdleTime}" />
        </bean>

        <!-- Hibernate SessionFactory -->
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                    <!-- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> -->
                    <prop key="hibernate.default_schema">${hibernate.default_schema}</prop>
                    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                    <prop key="hibernate.connection.shutdown">true</prop>

                </props>
            </property>
            <property name="packagesToScan" value="com.mycompany.saas.model"></property>
        </bean>
    </beans>

    <beans profile="test">
        <!-- DataSource -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close">
            <property name="driverClass" value="${database.driverClass}" />
            <property name="jdbcUrl" value="${database.url}" />
            <property name="user" value="${database.username}" />
            <!-- <property name="password" value="${database.password}" /> -->
            <property name="acquireIncrement" value="${connection.acquireIncrement}" />
            <property name="minPoolSize" value="${connection.minPoolSize}" />
            <property name="maxPoolSize" value="${connection.maxPoolSize}" />
            <property name="maxIdleTime" value="${connection.maxIdleTime}" />
        </bean>
        <!-- Hibernate SessionFactory -->
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                    <prop key="hibernate.connection.shutdown">true</prop>

                </props>
            </property>
            <property name="packagesToScan" value="com.mycompany.saas.model"></property>
        </bean>
    </beans>

    <!-- Transaction -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="applicationContextProvder" class="com.mycompany.saas.util.ApplicationContextProvider" />
    <bean id="mcSaasUserDAO" class="com.mycompany.saas.dao.mcSaasUserDAOImpl"></bean>
    <bean id="mcUserService" class="com.mycompany.saas.service.mcUserServiceImpl"></bean>
    <bean id="emailContentGenerator" class="com.mycompany.saas.notifier.mcEmailContentGenerator">
        <constructor-arg value="classpath:/mail.html" />
    </bean>



</beans>

1 个答案:

答案 0 :(得分:2)

读取XSD文件时,第一级<bean>...</bean>元素(即transactionManager和后续bean)必须在嵌套的<beans profile="xxx">...</beans>元素之前出现。

这是XSD的片段

<xsd:element name="beans"><xsd:annotation><xsd:documentation>
    ...
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element ref="description" minOccurs="0"/>
            <xsd:choice minOccurs="0" maxOccurs="unbounded">
                <xsd:element ref="import"/>
                <xsd:element ref="alias"/>
                <xsd:element ref="bean"/>
                <xsd:any namespace="##other" processContents="strict" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:choice>
            <xsd:element ref="beans" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
        ...

这表示beans元素必须是包含description(在别处定义)的序列,后跟零个或多个元素,可以是importalias中的任何一个或bean(也以其他顺序在其他地方定义),后跟零个或多个beans元素(递归定义)。