Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the error Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'

I am trying to run a basic SpringBatch application . I have the job config like below

 @Bean
    public Step myStep(@Value("1") int chunkSize,
                                   ItemReader<Input> myReader,
                                   ItemProcessor<Input, Output> myProcessor,
                                   ItemWriter<Output> myWriter,
                                   SkipPolicy mySkipPolicy,
                                   JobRepository jobRepository,
                                   StepExecutionListener myListener) {

        return new StepBuilder("myStep", jobRepository).<Input, Output>chunk(chunkSize)
            .reader(myReader).processor(myProcessor)
            .writer(myWriter).faultTolerant()
            .skipPolicy(mySkipPolicy)
            .listener(myListener)
            .build();
    }

I am using in memory h2db as the data source . Here is my data source configuration

 @Bean(name = "userDataSource")
    @ConfigurationProperties("spring.user.datasource)
    public DataSource userDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "batchDataSource")
    @ConfigurationProperties("spring.batch.datasource")
    @Primary
    public DataSource batchDataSource() {

        return DataSourceBuilder.create().build();
    }

Here is my yaml config


spring:
  application:
    name: my-application
  batch:
    datasource:
      jdbcUrl: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
      max-lifetime: ${BATCH_DATASOURCE_MAX_LIFETIME:30000}
      password: sa
      username: sa
  user:
    datasource:
      jdbcUrl: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
      max-lifetime: ${USER_DATASOURCE_MAX_LIFETIME:30000}
      password: sa
      username: sa

Whenever I am trying to run test cases , I am getting below error

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobRepository': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'

My class which contains main method is in package example.application and all the configuration classes I have mentioned above are in package example.application.config. I dont think it is a component scan issue. Where am I going wrong? Please let me know if any further details are required.

like image 378
neha chawla Avatar asked May 05 '26 18:05

neha chawla


1 Answers

Assuming you are using Spring Batch 5.x, I believe the jobRepository bean is looking for a bean named "dataSource." Simply rename your second bean from @Bean(name = "batchDataSource") to @Bean(name = "dataSource").

like image 96
Philippe Avatar answered May 07 '26 08:05

Philippe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!