title: BeanCreationException: Error creating bean with name 'batchConfigurer'
body: <p>I already went through the many link like : <a href="https://stackoverflow.com/questions/30537375/spring-batch-with-two-different-datasource-issue?rq=1">Spring Batch with two different datasource issue</a>, but I'm still getting the below error. I have configured the two datasource in the Spring Boot Batch application.</p>
  
  <p>Below error - </p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchConfigurer': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Unable to initialize Spring Batch
      at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:138) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:424) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1700) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:581) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
      at com.prateek.SpringBatchClassicDbApplication.main(SpringBatchClassicDbApplication.java:12) [classes/:na]
  Caused by: java.lang.IllegalStateException: Unable to initialize Spring Batch
      at org.springframework.boot.autoconfigure.batch.BasicBatchConfigurer.initialize(BasicBatchConfigurer.java:101) ~[spring-boot-autoconfigure-2.0.3.RELEASE.jar:2.0.3.RELEASE]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
      at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_151]
      at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:365) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:308) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:135) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      ... 18 common frames omitted
  Caused by: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
      at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:1063) ~[HikariCP-2.7.9.jar:na]
      at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:109) ~[HikariCP-2.7.9.jar:na]
      at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:151) ~[spring-jdbc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:115) ~[spring-jdbc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:78) ~[spring-jdbc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:319) ~[spring-jdbc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:356) ~[spring-jdbc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
      at org.springframework.batch.support.DatabaseType.fromMetaData(DatabaseType.java:100) ~[spring-batch-infrastructure-4.0.1.RELEASE.jar:4.0.1.RELEASE]
      at org.springframework.batch.core.repository.support.JobRepositoryFactoryBean.afterPropertiesSet(JobRepositoryFactoryBean.java:183) ~[spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE]
      at org.springframework.boot.autoconfigure.batch.BasicBatchConfigurer.createJobRepository(BasicBatchConfigurer.java:131) ~[spring-boot-autoconfigure-2.0.3.RELEASE.jar:2.0.3.RELEASE]
      at org.springframework.boot.autoconfigure.batch.BasicBatchConfigurer.initialize(BasicBatchConfigurer.java:96) ~[spring-boot-autoconfigure-2.0.3.RELEASE.jar:2.0.3.RELEASE]
      ... 25 common frames omitted
  </code></pre>
  
  <p><strong>CommonConfig.java</strong></p>
  
  <pre><code>@Configuration
  @EnableBatchProcessing
  @PropertySource("classpath:scheduler.properties")
  public class CommonConfig {
  
      @Bean
      @Primary
      @ConfigurationProperties(prefix = "spring.primary")
      public DataSource primaryDataSource() {
          return DataSourceBuilder.create().build();
      }
  
      @Bean
      @ConfigurationProperties(prefix = "spring.secondary")
      public DataSource secondaryDataSource() {
          return DataSourceBuilder.create().build();
      }
  }
  </code></pre>
  
  <p><strong>application.properites</strong></p>
  
  <pre><code># DATA SOURCE
  spring.primary.datasource.url=jdbc:mysql://localhost:3306/classicmodels
  spring.primary.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  spring.primary.datasource.username=root
  spring.primary.datasource.password=root
  
  spring.secondary.datasource.url=jdbc:mysql://localhost:3306/booking
  spring.secondary.datasource.username=root
  spring.secondary.datasource.password=root
  spring.secondary.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  </code></pre>
  
  <p><strong>EmployeesJob.java</strong></p>
  
  <pre><code>@Configuration
  @EnableBatchProcessing
  public class EmployeesJob {
      public static final String DATE_FORMAT = "dd-MM-yyyy-hhmmssss";
      public static final DateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
  
      @Autowired
      private JobBuilderFactory jobBuilderFactory;
  
      @Autowired
      private StepBuilderFactory stepBuilderFactory;
  
      @Autowired
      @Qualifier("primaryDataSource")
      private DataSource dataSource;
  
      @Bean 
      public EmployeesProcessor employeesProcessor() {
          return new EmployeesProcessor();
      }
  
      @Bean
      public EmployeesRunScheduler scheduler() {
          return new EmployeesRunScheduler();
      }
  
      // This file helps to create CSV column aliases
      @Bean
      public EmployeesFlatFileWriterCallback headerCallback() {
          return new EmployeesFlatFileWriterCallback();
      }
  
      @Bean(destroyMethod="")
      @StepScope
      public JdbcCursorItemReader&lt;Employees&gt; employeesReader(){
          JdbcCursorItemReader&lt;Employees&gt; itemReader = new JdbcCursorItemReader&lt;&gt;();
          itemReader.setDataSource(dataSource);
          itemReader.setSql("SELECT employeeNumber, lastName, firstName, extension, email, officeCode, reportsTo, jobTitle FROM employees ");
          itemReader.setRowMapper(new EmployeeRowMapper());
          // The fetch size can be controlled from the application.properties 
          //itemReader.setFetchSize(200);
          return itemReader;
      }
  
  
      @Bean(destroyMethod="")
      public FlatFileItemWriter&lt;Employees&gt; employeesWriter(){
          FlatFileItemWriter&lt;Employees&gt; fileItemWriter = new FlatFileItemWriter&lt;&gt;();
          //fileItemWriter.setResource(new FileSystemResource("csv/employees.csv"));
          fileItemWriter.setResource(new FileSystemResource("csv/employees-{"+ formatter.format(new Date()) +"}.csv"));
          fileItemWriter.setHeaderCallback(headerCallback());
  
          BeanWrapperFieldExtractor&lt;Employees&gt; fieldExtractor = new BeanWrapperFieldExtractor&lt;&gt;();
          fieldExtractor.setNames(new String[] {"employeeNumber", "lastName", "firstName", "extension", "email", "officeCode", "reportsTo", "jobTitle"});
  
          DelimitedLineAggregator&lt;Employees&gt; lineAggregator = new DelimitedLineAggregator&lt;&gt;();
          lineAggregator.setDelimiter(",");
          lineAggregator.setFieldExtractor(fieldExtractor);
  
          fileItemWriter.setLineAggregator(lineAggregator);
  
          return fileItemWriter;
      } 
  
      // Step Execution
      @Bean
      public Step step1() {
          return stepBuilderFactory.get("step1")
                  .&lt;Employees, Employees&gt;chunk(10)
                  .reader(employeesReader())
                  .processor(employeesProcessor())
                  .writer(employeesWriter())
                  .build();
      }
  
      // Job Execution
      @Primary
      @Bean
      public Job exportEmployeesJob() {
          return jobBuilderFactory
                  .get("exportEmployeesJob")
                  .incrementer(new RunIdIncrementer())
                  .flow(step1())
                  .end()
                  .build();
      }
  }
  </code></pre>