Friday, January 6, 2012

Using Spring 3.1 bean definition profiles with unified property management

Spring 3.1 introduced concepts of bean definition profiles (see spring-framework-3-1-m1-released ) and unified property management . I was able to use these features to allow switching between the active profiles by providing JVM system property  (supplied via -Dspring.profiles.active,  maven properties, cargo.args, etc...) parameter when starting the web container while falling back on using a default profile when no JVM system property was supplied.

Specifically I wanted to use an embedded database to allow fast, environment independent development, unit and integration testing vs using external database in production, qa and staging environments to which access is defined through jndi parameter.

Context file:
  
  <context:property-placeholder 
            location="classpath*:profile.properties"/>

    <!-- Local profile using embedded database-->
  <beans profile="local">     
    <jdbc:embedded-database id="dataSource">     
    <jdbc:script location="classpath:scripts/hsql_tables.sql"/>
    </jdbc:embedded-database>
  </beans>


  <!-- Remote profile using external data source -->
  <beans profile="remote"> 
   <jee:jndi-lookup id="dataSource"
     jndiname="java:comp/env/jdbc/dataSource"/>
  </beans>

Property file ( profile.properties ):
  spring.profiles.active="remote"
By default, system properties take precedence over environment variables thus spring.profiles.active parameter submitted through the JVM system property will be used instead of value in profile.properties  file.

Alternative solution:
Another possible way of accomplishing same goal (without the use of spring bean profiles) would be to import a different context file for the database in different environments, but bean profiles seemed like a more appropriate solution in this case.
<import resource="${database.type}-context.xml"/>

No comments:

Post a Comment