Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Batch and Spring3.1 profiles

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 ?

like image 336
magulla Avatar asked Nov 02 '22 08:11

magulla


1 Answers

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>
like image 100
Gat Avatar answered Nov 08 '22 09:11

Gat