0
votes

I'm writing a Spring Batch application and have split the Spring Beans Configuration into multiple files;

  • batch/launchContext.xml (JobLauncher/JobRepository etc)
  • batch/common.xml (common beans used in all jobs)
  • dataSource.xml (the data source)
  • batch/jobs/myJob.xml (individual files for jobs)

The reason for this is because I wish the data source to be changed between dev/test/production setup and also to save re-writing the same bean definitions over-and-over again.

The problem lies when I wish to launch a Spring Batch application. When reading the instructions here http://static.springsource.org/spring-batch/reference/html/configureJob.html#runningJobsFromCommandLine it assumes one Spring configuration file per job, but that's not how I wish to do it.

How may I run a Spring Batch job, from the command line, which uses multiple bean configuration files?

1

1 Answers

1
votes

You could always just import contexts:

So in batch/jobs/myJob.xml:

<import resource="batch/launchContext.xml" />
<import resource="batch/common.xml" />
<import resource="dataSource.xml" />

See also in Spring Docs

EDIT:

You can use a properties placeholder to externalise your data source property definitions:

In your dataSource.xml:

<bean id="dataSource" destroy-method="close"
    class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="${jdbc.driverClassName}"/>
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
</bean>

<context:property-placeholder 
   location="classpath:com/foo/jdbc.properties" 
   systemPropertiesMode="2" />  <!-- 2 means override -->

Your jdbc.properties would contain the default values:

jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root

but because using the "override" system properties mode, these can be specified at runtime:

java -Djdbc.url=jdbc:hsqldb:hsql://dev:9002 ...

See also here