简体   繁体   English

XML中的Spring配置文件出错

[英]Error with spring profile in XML

I am using spring profile in my mvc-dispatcher-servlet.xml. 我在我的mvc-dispatcher-servlet.xml中使用spring配置文件。 But getting error: 但是出现错误:

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.

I am not adding other beans in profile as I want these to be used in both profile. 我没有在配置文件中添加其他bean,因为我希望在两个配置文件中使用它们。 Need help resolving the error. 需要帮助解决错误。 Below is my XML and the error is coming at line- bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> : 下面是我的XML,错误发生在linebean id =“ transactionManager” class =“ 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>

Reading the XSD file it appears that the first-level <bean>...</bean> elements (ie transactionManager and following beans) must come BEFORE the nested <beans profile="xxx">...</beans> elements. 读取XSD文件后,似乎在嵌套的<beans profile="xxx">...</beans>元素之前必须出现第一级<bean>...</bean>元素(即transactionManager和随后的bean) 。

Here's a snippet from the XSD 这是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>
        ...

This says a beans element must be a sequence containing a description (defined elsewhere), followed by zero or more elements that can be any of import , alias or bean (also defined elsewhere, in any order), followed by zero or more beans elements (defined recursively). 这表示beans元素必须是一个包含description (在其他地方定义)的序列,后跟零个或多个可以是importaliasbean (也可以按任何顺序在其他地方定义)中的任何元素,然后是零个或多个beans元素(递归定义)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM