Announcement Announcement Module
Collapse
No announcement yet.
Configure Spring data source for test & production environment Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Configure Spring data source for test & production environment

    Hi all,
    I have configured DataSource in applicationContext, using propertyPlaceHolder

    Code:
    <beans>
    	<!-- the property configurer -->
    	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations">
    			<list>
    				<!-- default values for testing -->
    				<value>classpath:SqlConnStringDbExp.properties</value>												
    			</list>
    		</property>		
    	</bean>	
    
       <bean id="dataSource" destroy-method="close" class="it.genialloyd.startup.GenialBasicDataSource">
           <property name="driverClassName"> <value>net.sourceforge.jtds.jdbc.Driver</value></property>
           <property name="url"> <value>${jdbc.urljtds}</value></property>
           <property name="username"><value>uteappl</value></property>
           <property name="password"><value>${jdbc.password}</value></property>
       </bean>
    I would like to load the production property file in production environment, and the test property in test environment.
    I've just found a solution to place the property outside the classpath, so different server have different file, not overwritten by the class file.

    <value>file:c/pathtopropery/SqlConnStringDbExp.properties</value>
    Any hint?
    Thanks

  • #2
    This is a solution

    Setting a vm variable, as a prefix for the right property file:

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <!-- default values for testing -->
    <value>classpath:${environment}SqlConnStringDbExp. properties</value>
    </list>
    </property>
    </bean>


    Now, it is sufficient to set the "environment" variable (-Denvironment=prod_ for example ) to use the right property file (to load the right Database)
    Aldo

    Comment


    • #3
      If you are using Spring 3.1, you may use configuration profiles to provide distinct profiles/DS configurations for different environments. This way your definitinos could be completely different, e.g. the dev/testing definition would use properties from a file, and the production configuration might use JNDI. See the reference guide for the details.

      If you are using Maven to organize and build your project, and you only need to substitute the property values, the easiest and most reliable way is, probably, to use Maven resource filtering: your Spring configuration will be the same regardless of the environment, and the actual properties values will be resolved by Maven during the build. Read up on Maven resource filtering. Essentially, it means that the actual property values will be defined in the environment-specific Maven's settings.xml file, and each build server will use its own. Your local maven settings.xml will have the values that point to the test data source, and the production build will use the prod-specific values. You Spring configuration will look the same and will be agnostic of Maven, it will think it gets the values from the local properties files. See the Maven docs for more info.

      Comment

      Working...
      X