如何将spring xml从3.0转换为3.1使用bean:profile

bir*_*rdy 3 java spring spring-mvc spring-3

我有一个像3.0这样的xml:

        <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${jdbc.driverClassName}" />
            <property name="url" value="${jdbc.internal.url}" />
            <property name="username" value="${jdbc.internal.username}" />        
            <property name="password" value="${jdbc.internal.password}"/>
        </bean>
Run Code Online (Sandbox Code Playgroud)

我想在使用时将其转换为3.1 beans:profile但是,当我尝试将其更改为:

        <beans profile="dev">
          <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
              <property name="driverClassName" value="${jdbc.driverClassName}" />
              <property name="url" value="${jdbc.internal.url}" />
              <property name="username" value="${jdbc.internal.username}" />        
              <property name="password" value="${jdbc.internal.password}"/>
        </bean>
        </beans>
Run Code Online (Sandbox Code Playgroud)

我得到的错误如下:

Invalid content was found starting with element 'bean'. One of '{"http://www.springframework.org/schema/beans":beans}'
Run Code Online (Sandbox Code Playgroud)

我如何使用它,beans:profile以便仅在活动配置文件时调用此特定bean定义dev

更新 我的bean定义是:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
Run Code Online (Sandbox Code Playgroud)

Tom*_*icz 8

您必须将所有嵌套<beans>声明放在配置文件的最后.这就是XML模式的定义方式,您必须遵守这一点.

也可以看看

  • Spring Framework 3.1 M1发布:

    spring-beans-3.1.xsd 已更新以允许此嵌套,但限制为仅允许此类元素作为文件中的最后一个元素.