我正在使用Spring启动编写Spring批处理。 读者:它应该从Mongo DB集合中读取 作家:只需打印一些消息
但是当我运行批处理时,我会遇到异常:
s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchConfig' defined in file [C:\Workspace\batch\target\classes\com\myproject\batch\config\BatchConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.myproject.batch.config.BatchConfig$$EnhancerBySpringCGLIB$$8c3acbde]: Constructor threw exception; nested exception is java.lang.Error: Unresolved compilation problems:
The type org.springframework.data.mongodb.core.ExecutableFindOperation cannot be resolved. It is indirectly referenced from required .class files
The type org.springframework.data.mongodb.core.ExecutableRemoveOperation$ExecutableRemove cannot be resolved. It is indirectly referenced from required .class files
The type org.springframework.data.mongodb.core.ExecutableAggregationOperation cannot be resolved. It is indirectly referenced from required .class files
The type org.springframework.data.mongodb.core.ExecutableAggregationOperation$TerminatingAggregation cannot be resolved. It is indirectly referenced from required .class files
The type org.springframework.data.mongodb.core.ExecutableInsertOperation$ExecutableInsert cannot be resolved. It is indirectly referenced from required .class files
这是我的代码:
@EnableBatchProcessing
@Configuration
public class BatchConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private MongoTemplate mongoTemplate;
@Bean
public Job job() {
return jobBuilderFactory.get("job").flow(step1()).end().build();
}
@Bean
public MongoItemReader<Report> reader() {
MongoItemReader<Report> reader = new MongoItemReader<>();
reader.setTemplate(mongoTemplate);
reader.setSort(new HashMap<String, Sort.Direction>() {
{
put("_id", Direction.DESC);
}
});
reader.setTargetType(Report.class);
reader.setQuery("{}");
return reader;
}
@Bean
public ItemWriter<String> writer() {
System.out.println("#Writer Step:");
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1").<String, String>chunk(1).reader(reader()).writer(writer()).build();
}
}
有人可以告诉我配置有什么问题吗?
以下是我添加的依赖项:
spring-boot-starter-batch
spring-boot-starter-data-mongodb
spring-boot-starter-test
lombok
spring-batch-test
答案 0 :(得分:0)
我将spring-boot-starter-parent版本从2.0.2.RELEASE更改为2.0.1.RELEASE。这解决了我的问题。