java 带有java配置的Spring Batch
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27570119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Batch with java configuration
提问by HenioJR
I created a SpringBatch application with java configuration. I have a main method and a class that represents a job.
我用 java 配置创建了一个 SpringBatch 应用程序。我有一个主要方法和一个代表工作的类。
@ComponentScan
@EnableAutoConfiguration
public class App
{
public static void main( String[] args )
{
System.out.println( "Starting Spring Batch Execution -------" );
SpringApplication.run(App.class, args);
}
}
@Configuration
@EnableBatchProcessing
public class FlatFileJob {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
/**
* Create and configure job
* @return
*/
@Bean(name = "Read RabbitMQ")
public Job addFlatFileJob(){
return jobs.get("carJob")
.start(this.flatFileStep())
.build();
}
/**
* Create and configure the only step
* @return
*/
@Bean
public Step flatFileStep(){
return steps.get("step")
.<Car, Car> chunk(3)
.reader(new CarItemReader())
.processor(new CarItemProcessor())
.writer(new CarItemWriter())
.build();
}
@Bean
public PlatformTransactionManager transactionManager(){
return new ResourcelessTransactionManager();
}
@Bean
public JobRepository jobRepository() throws Exception{
JobRepository jobRepository = (JobRepository) new JobRepositoryFactoryBean();
return jobRepository;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Bean
public DataSource getDataSource(){
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/spring_batch");
dataSource.setUsername("xxx");
dataSource.setPassword("xxx");
return dataSource;
}
}
The problem is that when I execute the app, an exception is thrown. Any idea what is the problem?
问题是当我执行应用程序时,抛出异常。知道有什么问题吗?
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flatFileJob': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.batch.core.configuration.annotation.JobBuilderFactory neoway.com.job.FlatFileJob.jobs; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobBuilders' defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.batch.core.configuration.annotation.JobBuilderFactory org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration.jobBuilders() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobRepository' defined in class path resource [neoway/com/job/FlatFileJob.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.batch.core.repository.JobRepository neoway.com.job.FlatFileJob.jobRepository() throws java.lang.Exception] threw exception; nested exception is java.lang.ClassCastException: org.springframework.batch.core.repository.support.JobRepositoryFactoryBean cannot be cast to org.springframework.batch.core.repository.JobRepository
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:301)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
at neoway.com.App.main(App.java:50)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.batch.core.configuration.annotation.JobBuilderFactory neoway.com.job.FlatFileJob.jobs; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobBuilders' defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.batch.core.configuration.annotation.JobBuilderFactory org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration.jobBuilders() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobRepository' defined in class path resource [neoway/com/job/FlatFileJob.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.batch.core.repository.JobRepository neoway.com.job.FlatFileJob.jobRepository() throws java.lang.Exception] threw exception; nested exception is java.lang.ClassCastException: org.springframework.batch.core.repository.support.JobRepositoryFactoryBean cannot be cast to org.springframework.batch.core.repository.JobRepository
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:522)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:298)
... 15 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobBuilders' defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.batch.core.configuration.annotation.JobBuilderFactory org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration.jobBuilders() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobRepository' defined in class path resource [neoway/com/job/FlatFileJob.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.batch.core.repository.JobRepository neoway.com.job.FlatFileJob.jobRepository() throws java.lang.Exception] threw exception; nested exception is java.lang.ClassCastException: org.springframework.batch.core.repository.support.JobRepositoryFactoryBean cannot be cast to org.springframework.batch.core.repository.JobRepository
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:597)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:990)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1021)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:964)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:494)
... 17 common frames omitted
回答by Alexey Semenyuk
Key this exception is org.springframework.batch.core.repository.support.JobRepositoryFactoryBean cannot be cast to org.springframework.batch.core.repository.JobRepository
.
此异常的关键是org.springframework.batch.core.repository.support.JobRepositoryFactoryBean cannot be cast to org.springframework.batch.core.repository.JobRepository
.
Line with a exception: JobRepository jobRepository = (JobRepository) new JobRepositoryFactoryBean();
.
有一个例外的行:JobRepository jobRepository = (JobRepository) new JobRepositoryFactoryBean();
。
Try to rewrite to this: JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean()
尝试重写为: JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean()
回答by Rajib Chaudhuri
@Bean
public JobRepository jobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setTransactionManager(transactionManager());
factory.setIsolationLevelForCreate("ISOLATION_REPEATABLE_READ");
factory.setDataSource(dataSource());
factory.setDatabaseType("SQLSERVER");
return factory.getObject();
}
getObject is the convenience method for casting a JobRepositoryFactoryBean to JobRepository
getObject 是将 JobRepositoryFactoryBean 转换为 JobRepository 的便捷方法
回答by Ankit Mahajan
Using Spring Boot the easiest thing is to have your configuration class extend org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer
which will configure the JobRepository
for you with the default configuration. It uses the data source that is being used in your application. If it cannot find a data source it will set up a in memory JobRepository
.
使用 Spring Boot 最简单的事情是让您的配置类扩展org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer
,它将JobRepository
使用默认配置为您配置。它使用您的应用程序中正在使用的数据源。如果它找不到数据源,它将在内存中设置一个JobRepository
。
For reference use the below link:
https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.html
参考使用以下链接:https:
//docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.html