Max Wang
Max Wang

Reputation: 23

ClassCastException is caused by casting JobRepositoryFactoryBean to JobRepository when Spring boot starts a batch job

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:

org.springframework.batch.core.repository.support.JobRepositoryFactoryBean$$EnhancerBySpringCGLIB$$ba801bb9

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()


Trying to run a batch job using Spring Boot

@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);
    }

}

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.

I will appreciate if anyone can help point out if I have missed out something obviously?

I am using Spring boot 2.1.8, Spring batch 4.1.2, Spring 5.1.6


@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();
    }
}

The batch-context.xml does nothing but defined a place holder

<context:property-placeholder location="classpath:/config/my-batch*.properties"/>

In AbstractMyJobConfig, there are auto-wired beans, but nothing else is special

@Autowired
protected JobBuilderFactory jobBuilderFactory;

@Autowired
protected StepBuilderFactory stepBuilderFactory;

Upvotes: 0

Views: 1933

Answers (1)

Max Wang
Max Wang

Reputation: 23

I finally figured out what caused the problem after having read a couple of times of the link below. Spring Batch configuration exception

The problem is that there is a named as jobRepository in an xml bean definition (batch:job-repository). With @EnableBatchProcessing, I believe this is not required. Besides, this may cause problem with Spring boot auto-configuration?

Removing the bean definition will get the described problem resolved.

Upvotes: 0

Related Questions