3

I have an xml in 3.0 like so:

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

I want to convert this to 3.1 while making use of the beans:profile However, when I try to change it to this:

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

I get errors like:

Invalid content was found starting with element 'bean'. One of '{"http://www.springframework.org/schema/beans":beans}'

Question

How can I make use of the beans:profile so that this particular bean definition only gets called when the active profile is dev

Update My beans definition is:

<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">
maba
  • 47,113
  • 10
  • 108
  • 118
birdy
  • 9,286
  • 24
  • 107
  • 171

3 Answers3

8

You must put all nested <beans> declarations at the very end of the configuration file. This is how XML schema is defined and you have to obey this.

See also

  • Spring Framework 3.1 M1 released:

    spring-beans-3.1.xsd has been updated to allow this nesting, but constrained to allow such elements only as the last ones in the file.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Thanks! this solved my problem. However, now while starting the app I am getting some exceptions like: `could not autowire field` for code where I have `@Autowired`. I've set the active profile in web.xml. Maybe i'll open a new question for this... – birdy Nov 09 '12 at 15:52
  • Thanks too ;) I missed this line in the doc. – willome Nov 09 '12 at 15:53
  • @Tomasz I created a separate question: http://stackoverflow.com/questions/13311603/autowired-beans-not-loading-after-using-beansprofiles-in-spring-3-1 – birdy Nov 09 '12 at 16:06
2

This is by design.

From the SpringSource blog:

spring-beans-3.1.xsd has been updated to allow this nesting, but constrained to allow such elements only as the last ones in the file. This should help provide flexibility without incurring clutter in the XML files. While this enhancement was developed in service of bean definition profiles, nested elements are useful in general. Imagine you have a subset of beans in a given file that should be marked lazy-init="true". Rather than marking each bean, you could instead declare a nested element, and all beans within will inherit that default. Beans defined elsewhere in the file will maintain the normal default of lazy-init="false". This applies for all the default-* attributes of the element, such as default-lazy-init, default-init-method, default-destroy-method, and so on.

Boris Treukhov
  • 17,493
  • 9
  • 70
  • 91
0

I had the same problem : I have never suceed to nest <beans> inside an other <beans>, even if the spring-beans.xsd was correct.

My (partial) solution was to create an other applicationContext.xml starting with

<beans profile="dev, qualif" 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
willome
  • 3,062
  • 19
  • 32