DEV Community

Suhana
Suhana

Posted on

DataSource issue in spring Batch 5 - mongodb

Hey, I am trying to implement spring batch 5 with mongodb

SodRepository sodRepository;

@Bean
public ItemReader<Sod> csvFileReader() {
  FlatFileItemReader<Sod> reader = new FlatFileItemReader<>();
  reader.setResource(new ClassPathResource(file)); // Specify your CSV file
  reader.setLineMapper(new DefaultLineMapper<Sod>() {{
    setLineTokenizer(new DelimitedLineTokenizer() {{
      setNames(new String[]{"groupCode","original", "conflictsWith", "level", "relationship", "validFrom", "validTo"});
    }});
    setFieldSetMapper(new BeanWrapperFieldSetMapper<Sod>() {{
      setTargetType(Sod.class);
    }});
  }});
  return reader;
}


@Bean
public ItemWriter<Sod> mongoItemWriter() {
  return items -> sodRepository.saveAll(items);
}

@Bean
public Job job(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
  return new JobBuilder("myJob",jobRepository)
          .start(step1(jobRepository,transactionManager))
          .build();
}

@Bean
public Step step1(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
  return new StepBuilder("myStep",jobRepository)
          .<Sod, Sod>chunk(100)
          .reader(csvFileReader())
          .writer(mongoItemWriter())
          .transactionManager(transactionManager)
          .build();
}

and mongo config:
@Configuration
@EnableConfigurationProperties
@EnableMongoRepositories(
        basePackages = {"com.airtel.africa.ums.client.config"},
        mongoTemplateRef = "mongoTemplate"
)
@EnableTransactionManagement
public class MongoConfig extends AbstractMongoClientConfiguration{

    @Value("${spring.data.mongodb.database}")
    private String databaseName;

    @Value("${spring.data.mongodb.uri}")
    private String mongoUrl;

    @Bean
    MongoTransactionManager transactionManager(MongoDatabaseFactory dbFactory) {
        return new MongoTransactionManager(dbFactory);
    }

    @Override
    protected String getDatabaseName() {
        return databaseName;
    }

    @Override
    public MongoClient mongoClient() {
        final ConnectionString connectionString = new ConnectionString(mongoUrl);
        final MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
                .applyConnectionString(connectionString).build();
        return MongoClients.create(mongoClientSettings);

    }

    @Bean(name = "mongoTemplate")
    public MongoTemplate mongoTemplate(MongoDatabaseFactory mongoDatabaseFactory) {
        return new MongoTemplate(mongoDatabaseFactory);
    }

}
Enter fullscreen mode Exit fullscreen mode

am getting error - Parameter 0 of method csvProcessingStep in com.airtel.africa.ums.client.excel.ExcelUploadConfig required a bean named 'dataSource' that could not be found.

Top comments (0)