user3396729
user3396729

Reputation: 69

Error with spring profile in XML

I am using spring profile in my mvc-dispatcher-servlet.xml. 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. 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"> :

<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>

Upvotes: 1

Views: 1669

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 86774

Reading the XSD file it appears that the first-level <bean>...</bean> elements (i.e. transactionManager and following beans) must come BEFORE the nested <beans profile="xxx">...</beans> elements.

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

Upvotes: 5

Related Questions