Spring Batch - Bean创建错误导致Autowire失败

时间:2016-11-23 00:50:58

标签: java spring configuration javabeans

我是Spring和Spring Batch的新手我正在为Spring Batch作业设置Java配置我使用正确运行的XML配置进行了测试。但是我遇到了一个问题,我甚至无法让应用程序运行,因为我认为,设置DataSource bean会出现问题。我要解决这里抛出的错误:



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.




我不知道我的配置出错了,因为我已经遵循了Spring和其他教程建议的设计模式。由于未正确创建bean并且工厂后处理没有关闭它会破坏我在ArchiveJobConfig类中设置的Autowired cBeans。任何帮助将不胜感激

控制台输出



::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'




作业启动器

此类将从控制器调用以启动t 他有一个参数列表,我将在另一个类中映射到所需的参数,尚未实现。该参数仅用于连接到数据库后的测试。



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


}




ArchiveJobConfig

这是作业持有占位符项读取器,编写器和Spring Batch java配置服务示例中的处理器。插入这些只是为了验证我的读者,作家等不会导致问题。



@Configuration
@EnableBatchProcessing
@Import( InfrastructureConfig.class )
public class ArchiveJobConfig {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    public DataSource dataSource;

    @Bean
    public ItemReader<Person> reader() {
            FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
            reader.setResource(new ClassPathResource("sample-data.csv"));
            reader.setLineMapper(new DefaultLineMapper<Person>() {{
                setLineTokenizer(new DelimitedLineTokenizer() {{
                    setNames(new String[] { "firstName", "lastName" });
                }});
                setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
                    setTargetType(Person.class);
                }});
            }});
            return reader;
    }

    @Bean
    public PersonItemProcessor processor() {
        return new PersonItemProcessor();
    }

    @Bean
    public ItemWriter<Person> writer() {
        JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
        writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
        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")
                .<Person,Person>chunk(10)
                .reader(reader())
                .processor(processor())
                .writer(writer())
                .build();
    }
}
&#13;
&#13;
&#13;

基础设施类 - 数据源Bean

这是我认为问题正在发生的地方,网址和驱动程序设置器中的值是此提交的占位符。我将此类导入到作业配置类中。使用EnableBatchProcessing注释,应该自动创建jobRepository bean,并且自动装配bean应该使我设置的数据源成为此作业的默认数据源。但我很遗憾地错过了配置中的一些步骤。再次,任何建议/协助将不胜感激

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

&#13;
&#13;
::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'
&#13;
&#13;
&#13;

0 个答案:

没有答案