recently faced with the issue that profiles feature enabled with spring 3.1 using multiple < beans > definition doesn't work for spring batch own tag.
<beans profile="prod">
<bean id ="test" class="java.lang.String"></bean>
<batch:job id="job" abstract="true" >
<batch:listeners>
<batch:listener ref="jobExecutionContextDateSetter" />
<batch:listener ref="jobStatusListener"/>
</batch:listeners>
</batch:job>
</beans>
<beans profile="dev">
<bean id ="test" class="java.lang.String"></bean>
<batch:job id="job" abstract="true" >
<batch:listeners>
<batch:listener ref="jobExecutionContextDateSetter" />
</batch:listeners>
</batch:job>
</beans>
running the test example (with out enabling either of profiles) spring complains about multiple annotation found for id "job". Any ideas ?
I had the same problem and that's because of spring XML validator. As you know when you create a spring xml file, at first spring sends this file to a XML parser to validate it.
I mean that you can define two or more beans with a same id. Then you can see that spring XML validator parses your XML file without any problem. Although in later phases spring itself throws an exception if these beans are in a same profile or out of any profile.
BUT in spring batch, XML validator doesn't allow to have two jobs with a same id.
The solution for this is that you can define your batch out of any profile and define its beans in the profiles as follow:
<batch:job id="job1" abstract="true" job-repository="jobRepository">
<batch:listeners>
<batch:listener ref="jobListener"/>
</batch:listeners>
</batch:job>
<beans profile="prod">
<bean id="jobListener" class="com.batch.ProductionJobListener"/>
</beans>
<beans profile="dev">
<bean id="jobListener" class="com.batch.DevelopmentJobListener"/>
</beans>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With