title: ClassCastException is caused by casting JobRepositoryFactoryBean to JobRepository when Spring boot starts a batch job
body: <blockquote>
    <p>Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate
    [org.springframework.batch.core.configuration.annotation.JobBuilderFactory]:
    Factory method 'jobBuilders' threw exception; nested exception is
    java.lang.ClassCastException:</p>
    
    <p>org.springframework.batch.core.repository.support.JobRepositoryFactoryBean$$EnhancerBySpringCGLIB$$ba801bb9</p>
    
    <p>cannot be cast to
    org.springframework.batch.core.repository.JobRepository
          at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
          at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622)
          ... 18 common frames omitted
        Caused by: java.lang.ClassCastException: org.springframework.batch.core.repository.support.JobRepositoryFactoryBean$$EnhancerBySpringCGLIB$$ba801bb9
    cannot be cast to
    org.springframework.batch.core.repository.JobRepository
          at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$$EnhancerBySpringCGLIB$$9641e37a.jobRepository()
          at org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration.jobBuilders(AbstractBatchConfiguration.java:59)
          at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$$EnhancerBySpringCGLIB$$9641e37a.CGLIB$jobBuilders$8()
          at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$$EnhancerBySpringCGLIB$$9641e37a$$FastClassBySpringCGLIB$$19e6f1ca.invoke()
          at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
          at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
          at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$$EnhancerBySpringCGLIB$$9641e37a.jobBuilders()</p>
  </blockquote>
  
  <hr>
  
  <p>Trying to run a batch job using Spring Boot</p>
  
  <pre><code>@SpringBootApplication
  @Import(value = {BatchJobConfig.class, AJobConfig.class})
  @ImportResource("classpath:META-INF/spring/batch-context.xml")
  public class MyApp {
  
      /**
       * @param args
       */
      public static void main(final String[] args) {
          SpringApplication.run(MyApp.class);
      }
  
  }
  </code></pre>
  
  <p>Tried to make AJobConfig is subclass of DefaultBatchConfigurer, but it doesn't really work. 
  somehow, this error occurs regardless if adding @EnableBatchProcessing or not. 
  Tried to add @EnableBatchProcessing to MyApp.java and AJobConfig.java. </p>
  
  <p>I will appreciate if anyone can help point out if I have missed out something obviously?</p>
  
  <p>I am using Spring boot 2.1.8, Spring batch 4.1.2, Spring 5.1.6 </p>
  
  <hr>
  
  <pre><code>@Configuration
  public class BatchJobConfig {
  
      @Bean
      @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
      public JobExecutionListener myJobListener() {
          return new MyJobExecutionListener();
      }
  
  }
  
  @Configuration
  public class MyJobConfig extends MyAbstractJobConfig {
  
      @Override
      @Bean
      public String jobName() {
          return "MYJOB";
      }
  
      @Bean("MYJOB")
      public Job myJob() {
          return newJobBuilder() //
                  .next(parms01Step())
                  .build();
  
      }
  
      @Bean("PARMS01")
      public Step parms01Step() {
          return newStepBuilder("PARMS01") //
                  .execute(Cblparmc.class, "zzzzz") //
                  .build();
      }
  }
  </code></pre>
  
  <p>The batch-context.xml does nothing but defined a place holder</p>
  
  <pre><code>&lt;context:property-placeholder location="classpath:/config/my-batch*.properties"/&gt;
  </code></pre>
  
  <p>In AbstractMyJobConfig, there are auto-wired beans, but nothing else is special</p>
  
  <pre><code>@Autowired
  protected JobBuilderFactory jobBuilderFactory;
  
  @Autowired
  protected StepBuilderFactory stepBuilderFactory;
  </code></pre>