spring - org.springframework.boot.autoconfigure.batch.BatchConfigurerConfiguration$JdbcBatchConfiguration 中方法batchConfigurer的参数1
问题描述
我正在使用 Spring Batch 并使用 @Scheduler 注释以某种频率安排作业。
从错误消息看来,Spring Boot 至少需要与 Spring DataSource 相关的条目,但我不需要它,因为我现在还没有处理数据库。
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 1 of method batchConfigurer in org.springframework.boot.autoconfigure.batch.BatchConfigurerConfiguration$JdbcBatchConfiguration required a bean of type 'javax.sql.DataSource' that could not be found.
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'javax.transaction.TransactionManager'
Action:
Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.
应用程序属性
cron.job.expression=*/1 * * * *
其他类:
@Configuration
@EnableBatchProcessing
@Primary
public class ScheduledDomainJob {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Job scheduledJob() {
return jobBuilderFactory.get("scheduledJob").flow(step1()).end().build();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1").<Domain, Domain>chunk(10)
.reader(reader()).writer(writer()).build();
}
@Bean
public FlatFileItemReader<Domain> reader() {
FlatFileItemReader<Domain> reader = new FlatFileItemReader<>();
reader.setResource(new ClassPathResource("csv/domain-1-03-2017.csv"));
reader.setLineMapper(new DefaultLineMapper<Domain>() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[]{"id", "domain"});
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Domain>() {{
setTargetType(Domain.class);
}});
}});
return reader;
}
@Bean
public CustomWriter writer() {
CustomWriter writer = new CustomWriter();
return writer;
}
@Bean
public RunScheduler scheduler() {
RunScheduler scheduler = new RunScheduler();
return scheduler;
}
}
CustomerWriter.java
@Slf4j
public class CustomWriter implements ItemWriter<Domain> {
@Override
public void write(List<? extends Domain> items) throws Exception {
log.info("writer ....... " + items.size());
for (Domain domain : items) {
log.info(domain + "\n");
}
}
}
解决方案
我希望您使用的是 Spring Boot 版本 2。您可以对代码进行一些更改,如下所示。
@EnableBatchProcessing
@Primary
public class ScheduledDomainJob extends DefaultBatchConfigurer
在您的 spring-boot 应用程序类上定义以下注释。
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
您可以使用 Spring boot 找到有关Spring Batch Hello world 示例的更多详细信息
推荐阅读
- apache-kafka - 一个topic有3个replicas,min.insyinc.replicas有2个,当follower和in-syinc的replicas都挂了,消费者想读的时候怎么办?
- excel - 自动填充公式,但限制一个或多个单元格 - Google 表格
- javascript - 未处理的拒绝类型错误:无法读取 null 的属性“id”
- swagger - 如何在 Open API Specifiaction V3 中定义接受标头
- keyboard-shortcuts - 快捷方式 CMD-A 或 CMD-Z 退出 TradingView 应用程序。(AZERTY 键盘布局)
- python - 如何检查 csv 生成的数据帧格式是否正确
- ios - 将模态视图控制器视图居中而不访问超级视图
- sparql - 根据 N-Triples 文件中的 DBpedia 类层次结构获取最具体的类型
- typescript - 在 vuex 状态下为数组中的对象提供计算属性或设置器的正确方法
- java - Spring Boot 没有公开正确的存储库路由