title: How to configure spring batch jobs modular?
body: <p>I'd like to defined mutiple <code>Jobs</code> in my <code>spring-batch</code> application, and trying to make them modular as follows:</p>
  
  <pre><code>@Configuration
  @EnableBatchProcessing(modular = true)
  public class ModularConfig {
      @Bean
      public ApplicationContextFactory getJob1() {
          Sysout("job1");
          return new GenericApplicationContextFactory(Job1.class);
      }
  
      @Bean
      public ApplicationContextFactory getJob2() {
          Sysout("job2");
          return new GenericApplicationContextFactory(Job2.class);
      }
  }
  </code></pre>
  
  <p>From the sysout I can see both job beans are created.</p>
  
  <p>BUT: when one of those jobs is launched, the <code>@Beans</code> of that job cannot be found.</p>
  
  <pre><code>public class Job1 {
      @Bean
      public Job job(Step step) {
          return jobFactory.get("job1")
                  .start(step)
                  .build();
      }
  
  
      @Bean
      public Step step(ItemReader&lt;String&gt; reader, ItemProcessor&lt;String, String&gt; processor, ItemWriter&lt;String&gt; writer) {
          return stepFactory.get("importStep")
                  .reader(reader)
                  .processor(processor)
                  .writer(writer)
                  .build();
      }
  
      @Bean
      @JobScope
      public IteamReader&lt;String&gt; reader(@Value("#{jobParameters['filename']}") String filename) {
          //returns a FlatFileItemReader&lt;String&gt;
      }
  
      //omitted also `@Bean` for processor + writer
  }
  </code></pre>
  
  <p>Result: the <code>reader</code> is not found in the job configuration! Why?</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'reader' is defined
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1175) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getType(AbstractBeanFactory.java:596) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
      at org.springframework.beans.factory.support.GenericTypeAwareAutowireCandidateResolver.checkGenericTypeMatch(GenericTypeAwareAutowireCandidateResolver.java:95) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
      at org.springframework.beans.factory.support.GenericTypeAwareAutowireCandidateResolver.isAutowireCandidate(GenericTypeAwareAutowireCandidateResolver.java:64) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
      at org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver.isAutowireCandidate(QualifierAnnotationAutowireCandidateResolver.java:142) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.isAutowireCandidate(DefaultListableBeanFactory.java:687) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.isAutowireCandidate(DefaultListableBeanFactory.java:646) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.isAutowireCandidate(DefaultListableBeanFactory.java:630) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1191) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
      at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
      at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
      ... 34 more
  </code></pre>
  
  <p>So why may the reader not being found? I mean: what can I do more than naming the bean method itself <code>reader()</code> and also inject it with variable name <code>reader</code>?</p>
  
  <p>Sidenote: when I remove the modularty and just use  <code>@Configuration</code> on <code>class Job1</code> then job runs fine. So I'm quite sure the configuration of the job and the reader should be correct. But of course I cannot run a similar jobclass beneath, so I'd like to have modularity enabled.</p>
  
  <hr>
  
  <p>Edit: when I enable the <code>@Configuration</code> annotation on my jobs, I'm getting a duplicate job name exception. That's why I'm not having it in the code above.</p>
  
  <pre><code>Caused by: org.springframework.batch.core.configuration.DuplicateJobException: A job configuration with this name [job1] was already registered
      at org.springframework.batch.core.configuration.support.MapJobRegistry.register(MapJobRegistry.java:51) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
      at org.springframework.batch.core.configuration.support.DefaultJobLoader.doRegister(DefaultJobLoader.java:258) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
      at org.springframework.batch.core.configuration.support.DefaultJobLoader.doLoad(DefaultJobLoader.java:193) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
      at org.springframework.batch.core.configuration.support.DefaultJobLoader.load(DefaultJobLoader.java:151) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
      at org.springframework.batch.core.configuration.support.AutomaticJobRegistrar.start(AutomaticJobRegistrar.java:173) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
      ... 16 more
  </code></pre>