bean define java 赋值_Spring Batch - Bean创建错误导致Autowire失败

我是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 . 任何帮助,将不胜感激

Console Output

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

Job Launcher

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

@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 reader() {

FlatFileItemReader reader = new FlatFileItemReader();

reader.setResource(new ClassPathResource("sample-data.csv"));

reader.setLineMapper(new DefaultLineMapper() {{

setLineTokenizer(new DelimitedLineTokenizer() {{

setNames(new String[] { "firstName", "lastName" });

}});

setFieldSetMapper(new BeanWrapperFieldSetMapper() {{

setTargetType(Person.class);

}});

}});

return reader;

}

@Bean

public PersonItemProcessor processor() {

return new PersonItemProcessor();

}

@Bean

public ItemWriter writer() {

JdbcBatchItemWriter writer = new JdbcBatchItemWriter();

writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider());

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")

.chunk(10)

.reader(reader())

.processor(processor())

.writer(writer())

.build();

}

}

Infrastructure Class - DataSource Bean

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

@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();

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值