title: Spring Batch - Bean Creation Error Causes Autowire to Fail
body: <p>I'm pretty new to Spring and Spring Batch I'm working on setting up a Java configuration for a Spring Batch job I tested out using an XML configuration which I have running correctly. However I'm running into an issue where I cannot even get the job to application to run because of an issue, I believe, with setting up the DataSource bean.  I'm going off the the errors being thrown here:</p>
  
  <pre class="lang-java prettyprint-override"><code>2016 - 11 - 22 16: 49: 59.819 WARN 20056---[main] o.s.c.a.ConfigurationClassEnhancer: @Bean method ScopeConfiguration.stepScope is non - static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring@ Configuration class.Add the 'static' modifier to this method to avoid these container lifecycle issues; see@ Bean javadoc for complete details.
  2016 - 11 - 22 16: 49: 59.834 WARN 20056---[main] o.s.c.a.ConfigurationClassEnhancer: @Bean method ScopeConfiguration.jobScope is non - static and returns an object assignable to Spring's  BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring@ Configuration class.Add the 'static' modifier to this method to avoid these container lifecycle issues; see@ Bean javadoc for complete details.
  </code></pre>
  
  <p>I don't know where I'm going wrong with the configuration because I've followed the design patterns suggested by Spring and other tutorials. Since the bean is not created correctly and the factory postprocess not closing it breaks the Autowired cBeans I'm setting up in the ArchiveJobConfig class. Any help would be appreciated</p>
  
  <p><strong>Console Output</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>::Spring Boot::(v1.3.5.RELEASE)
  
   2016 - 11 - 22 16: 49: 58.315 INFO 20056---[main] c.s.batch.jobs.gdmArchive.Application: Starting Application on USCHDWNCN329V3B with PID 20056(C:\ IdeaProjects\ GdmArchive\ target\ classes started by *********** in C: \Users\ ***********\ IdeaProjects\ GdmArchive)
   2016 - 11 - 22 16: 49: 58.335 INFO 20056---[main] c.s.batch.jobs.gdmArchive.Application: No active profile set, falling back to default profiles: default
   2016 - 11 - 22 16: 49: 58.472 INFO 20056---[main] s.c.a.AnnotationConfigApplicationContext: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@ 4ae82894: startup date[Tue Nov 22 16: 49: 58 MST 2016]; root of context hierarchy
   2016 - 11 - 22 16: 49: 59.539 INFO 20056---[main] o.s.b.f.config.PropertiesFactoryBean: Loading properties file from URL[jar: file: /C:/Users / *********** / .m2 / repository / org / springframework / integration / spring - integration - core / 4.3.2.RELEASE / spring - integration - core - 4.3.2.RELEASE.jar!/META-INF/spring.integration.default.properties]
   2016 - 11 - 22 16: 49: 59.543 INFO 20056---[main] o.s.i.config.IntegrationRegistrar: No bean named 'integrationHeaderChannelRegistry' has been explicitly defined.Therefore, a default DefaultHeaderChannelRegistry will be created.
   2016 - 11 - 22 16: 49: 59.819 WARN 20056---[main] o.s.c.a.ConfigurationClassEnhancer: @Bean method ScopeConfiguration.stepScope is non - static and returns an object assignable to Spring 's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method'
   s declaring@ Configuration class.Add the 'static' modifier to this method to avoid these container lifecycle issues; see@ Bean javadoc for complete details.
   2016 - 11 - 22 16: 49: 59.834 WARN 20056---[main] o.s.c.a.ConfigurationClassEnhancer: @Bean method ScopeConfiguration.jobScope is non - static and returns an object assignable to Spring 's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring@ Configuration class.Add the 'static' modifier to this method to avoid these container lifecycle issues; see@ Bean javadoc for complete details.
   2016 - 11 - 22 16: 49: 59.836 INFO 20056---[main] faultConfiguringBeanFactoryPostProcessor: No bean named 'errorChannel' has been explicitly defined.Therefore, a default PublishSubscribeChannel will be created.
   2016 - 11 - 22 16: 49: 59.845 INFO 20056---[main] faultConfiguringBeanFactoryPostProcessor: No bean named 'taskScheduler' has been explicitly defined.Therefore, a default ThreadPoolTaskScheduler will be created.
   2016 - 11 - 22 16: 50: 00.147 INFO 20056---[main] o.s.b.f.config.PropertiesFactoryBean: Loading properties file from URL[jar: file: /C:/Users / *********** / .m2 / repository / org / springframework / integration / spring - integration - core / 4.3.2.RELEASE / spring - integration - core - 4.3.2.RELEASE.jar!/META-INF/spring.integration.default.properties]
   2016 - 11 - 22 16: 50: 00.148 INFO 20056---[main] trationDelegate$BeanPostProcessorChecker: Bean 'integrationGlobalProperties' of type[class org.springframework.beans.factory.config.PropertiesFactoryBean] is not eligible for getting processed by all BeanPostProcessors(for example: not eligible for auto - proxying) 
   2016 - 11 - 22 16: 50: 00.163 INFO 20056---[main] trationDelegate$BeanPostProcessorChecker: Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration'
   of type[class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$85f858a4] is not eligible for getting processed by all BeanPostProcessors(for example: not eligible for auto - proxying)
   2016 - 11 - 22 16: 50: 00.221 INFO 20056---[main] trationDelegate$BeanPostProcessorChecker: Bean 'integrationGlobalProperties' of type[class java.util.Properties] is not eligible
   for getting processed by all BeanPostProcessors(for example: not eligible for auto - proxying)
   2016 - 11 - 22 16: 50: 00.353 WARN 20056---[main] s.c.a.AnnotationConfigApplicationContext: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'archiveJobConfig': Unsatisfied dependency expressed through field 'jobBuilderFactory';
   nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration': Unsatisfied dependency expressed through field 'dataSources';
   nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource[com / spring / batch / jobs / gdmArchive / InfrastructureConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate[javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: No supported DataSource type found
   2016 - 11 - 22 16: 50: 00.374 ERROR 20056---[main] o.s.boot.SpringApplication: Application startup failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'archiveJobConfig': Unsatisfied dependency expressed through field 'jobBuilderFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration': Unsatisfied dependency expressed through field 'dataSources'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource'
   defined in class path resource[com / spring / batch / jobs / gdmArchive / InfrastructureConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate[javax.sql.DataSource]: Factory method 'dataSource'
  </code></pre>
  
  <p><strong>Job Launcher</strong></p>
  
  <p>This class will be called from a controller to launch t
  he job with a list of parameters that I will map to the needed with in another class, not yet implemented. The parameter is just for testing once connected to a database.</p>
  
  <pre class="lang-java prettyprint-override"><code>@ComponentScan
  @EnableAutoConfiguration
  public class ArchiveJob {
  
     public int startJob(List paramList) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, BeansException {
  
          SpringApplication app = new SpringApplication(ArchiveJob.class);
          ConfigurableApplicationContext ctx = app.run();
  
          BatchStatus exitStatus;
          String test = "2016";
  
          SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
          Job archiveDBJob = ctx.getBean("archiveDBJob", Job.class);
  
  
          JobParameters jobParameters = new JobParametersBuilder()
                  .addString("year", test)
                  .toJobParameters();
  
          JobExecution jobExecution = jobLauncher.run(archiveDBJob, jobParameters);
          exitStatus = jobExecution.getStatus();
  
          if(exitStatus.equals("COMPLETED"))
              return 1;
          else
              return 0;
     }
  
  
  }
  </code></pre>
  
  <p><strong>ArchiveJobConfig</strong></p>
  
  <p>This is the job holds placeholder item reader, writers, and a processor from the Spring Batch java config service example. These were inserted just to verify that my readers, writers, etc were not causing the issue.</p>
  
  <pre class="lang-java prettyprint-override"><code>@Configuration
  @EnableBatchProcessing
  @Import( InfrastructureConfig.class )
  public class ArchiveJobConfig {
  
      @Autowired
      public JobBuilderFactory jobBuilderFactory;
  
      @Autowired
      public StepBuilderFactory stepBuilderFactory;
  
      @Autowired
      public DataSource dataSource;
  
      @Bean
      public ItemReader&lt;Person&gt; reader() {
              FlatFileItemReader&lt;Person&gt; reader = new FlatFileItemReader&lt;Person&gt;();
              reader.setResource(new ClassPathResource("sample-data.csv"));
              reader.setLineMapper(new DefaultLineMapper&lt;Person&gt;() {{
                  setLineTokenizer(new DelimitedLineTokenizer() {{
                      setNames(new String[] { "firstName", "lastName" });
                  }});
                  setFieldSetMapper(new BeanWrapperFieldSetMapper&lt;Person&gt;() {{
                      setTargetType(Person.class);
                  }});
              }});
              return reader;
      }
  
      @Bean
      public PersonItemProcessor processor() {
          return new PersonItemProcessor();
      }
  
      @Bean
      public ItemWriter&lt;Person&gt; writer() {
          JdbcBatchItemWriter&lt;Person&gt; writer = new JdbcBatchItemWriter&lt;Person&gt;();
          writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider&lt;Person&gt;());
          writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
          writer.setDataSource(dataSource);
          return writer;
      }
  
      @Bean
      public Job archiveDBJob() {
          return jobBuilderFactory.get("archiveDBJob")
                  .incrementer(new RunIdIncrementer())
                  .flow(step1())
                  .end()
                  .build();
      }
  
      @Bean
      public Step step1() {
          return stepBuilderFactory.get("step")
                  .&lt;Person,Person&gt;chunk(10)
                  .reader(reader())
                  .processor(processor())
                  .writer(writer())
                  .build();
      }
  }
  </code></pre>
  
  <p><strong>Infrastructure Class - DataSource Bean</strong></p>
  
  <p>This is where I believe the issue is occurring, the values within the url and driver setters are placeholders for this submission. I import this class into the job config class. With the EnableBatchProcessing annotation the jobRepository bean should be auto created and autowiring the bean should make the datasource I setup the default one for this job. But I am obiviously missing some step in the configuration. Again any advice/assitance would be greatly appreciated</p>
  
  <pre class="lang-java prettyprint-override"><code>@Configuration
  @PropertySource("classpath:spring/batch/config/db.properties")
  public class InfrastructureConfig {
  
      @Autowired
      Environment env;
  
      @Bean
      public DataSource dataSource() {
          return DataSourceBuilder.create()
  //              .driverClassName(env.getProperty("databaseUrl"))
  //              .url(env.getProperty("databaseUrl"))
                  .driverClassName("com.mysql.jdbc.Driver")
                  .url("databaseUrl")
                  .build();
      }
  }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>::Spring Boot::(v1.3.5.RELEASE)
  
   2016 - 11 - 22 16: 49: 58.315 INFO 20056---[main] c.s.batch.jobs.gdmArchive.Application: Starting Application on USCHDWNCN329V3B with PID 20056(C:\ IdeaProjects\ GdmArchive\ target\ classes started by *********** in C: \Users\ ***********\ IdeaProjects\ GdmArchive)
   2016 - 11 - 22 16: 49: 58.335 INFO 20056---[main] c.s.batch.jobs.gdmArchive.Application: No active profile set, falling back to default profiles: default
   2016 - 11 - 22 16: 49: 58.472 INFO 20056---[main] s.c.a.AnnotationConfigApplicationContext: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@ 4ae82894: startup date[Tue Nov 22 16: 49: 58 MST 2016]; root of context hierarchy
   2016 - 11 - 22 16: 49: 59.539 INFO 20056---[main] o.s.b.f.config.PropertiesFactoryBean: Loading properties file from URL[jar: file: /C:/Users / *********** / .m2 / repository / org / springframework / integration / spring - integration - core / 4.3.2.RELEASE / spring - integration - core - 4.3.2.RELEASE.jar!/META-INF/spring.integration.default.properties]
   2016 - 11 - 22 16: 49: 59.543 INFO 20056---[main] o.s.i.config.IntegrationRegistrar: No bean named 'integrationHeaderChannelRegistry' has been explicitly defined.Therefore, a
   default DefaultHeaderChannelRegistry will be created.
   2016 - 11 - 22 16: 49: 59.819 WARN 20056---[main] o.s.c.a.ConfigurationClassEnhancer: @Bean method ScopeConfiguration.stepScope is non - static and returns an object assignable to Spring 's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method'
   s declaring@ Configuration class.Add the 'static' modifier to this method to avoid these container lifecycle issues; see@ Bean javadoc for complete details.
   2016 - 11 - 22 16: 49: 59.834 WARN 20056---[main] o.s.c.a.ConfigurationClassEnhancer: @Bean method ScopeConfiguration.jobScope is non - static and returns an object assignable to Spring 's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring@ Configuration class.Add the 'static' modifier to this method to avoid these container lifecycle issues; see@ Bean javadoc for complete details.
   2016 - 11 - 22 16: 49: 59.836 INFO 20056---[main] faultConfiguringBeanFactoryPostProcessor: No bean named 'errorChannel' has been explicitly defined.Therefore, a default PublishSubscribeChannel will be created.
   2016 - 11 - 22 16: 49: 59.845 INFO 20056---[main] faultConfiguringBeanFactoryPostProcessor: No bean named 'taskScheduler' has been explicitly defined.Therefore, a default ThreadPoolTaskScheduler will be created.
   2016 - 11 - 22 16: 50: 00.147 INFO 20056---[main] o.s.b.f.config.PropertiesFactoryBean: Loading properties file from URL[jar: file: /C:/Users / *********** / .m2 / repository / org / springframework / integration / spring - integration - core / 4.3.2.RELEASE / spring - integration - core - 4.3.2.RELEASE.jar!/META-INF/spring.integration.default.properties]
   2016 - 11 - 22 16: 50: 00.148 INFO 20056---[main] trationDelegate$BeanPostProcessorChecker: Bean 'integrationGlobalProperties' of type[class org.springframework.beans.factory.config.PropertiesFactoryBean] is not eligible for getting processed by all BeanPostProcessors(for example: not eligible for auto - proxying) 
   2016 - 11 - 22 16: 50: 00.163 INFO 20056---[main] trationDelegate$BeanPostProcessorChecker: Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration'
   of type[class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$85f858a4] is not eligible for getting processed by all BeanPostProcessors(for example: not eligible for auto - proxying)
   2016 - 11 - 22 16: 50: 00.221 INFO 20056---[main] trationDelegate$BeanPostProcessorChecker: Bean 'integrationGlobalProperties' of type[class java.util.Properties] is not eligible
   for getting processed by all BeanPostProcessors(for example: not eligible for auto - proxying)
   2016 - 11 - 22 16: 50: 00.353 WARN 20056---[main] s.c.a.AnnotationConfigApplicationContext: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'archiveJobConfig': Unsatisfied dependency expressed through field 'jobBuilderFactory';
   nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration': Unsatisfied dependency expressed through field 'dataSources';
   nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource[com / spring / batch / jobs / gdmArchive / InfrastructureConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate[javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: No supported DataSource type found
   2016 - 11 - 22 16: 50: 00.374 ERROR 20056---[main] o.s.boot.SpringApplication: Application startup failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'archiveJobConfig': Unsatisfied dependency expressed through field 'jobBuilderFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration': Unsatisfied dependency expressed through field 'dataSources'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource'
   defined in class path resource[com / spring / batch / jobs / gdmArchive / InfrastructureConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate[javax.sql.DataSource]: Factory method 'dataSource'
  </code></pre>