java - 创建名为“batchDataSource”的 bean 时出错:当前正在创建请求的 bean:是否存在无法解析的循环引用?
问题描述
我有一个批处理配置。我看到批处理是默认使用InMemoryMap
. 相反,我需要使用 MySQL 批量发送所有执行细节。但是当我使用以下代码时,我收到以下错误,
创建名为“batchDataSource”的 bean 时出错:当前正在创建请求的 bean:是否存在无法解析的循环引用?
@Configuration
@EnableBatchProcessing
public class BatchProcess extends DefaultBatchConfigurer {
private @Autowired Environment env;
@Bean
@StepScope
public ItemReader reader() {
...
}
@Bean
@StepScope
public ItemProcessor processor() {
...
}
@Bean
@StepScope
public ItemWriter writer() {
...
}
@Bean
@Primary
public DataSource batchDataSource() {
HikariDataSource hikari = new HikariDataSource();
hikari.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
hikari.setJdbcUrl(env.getProperty("spring.datasource.url"));
hikari.setUsername(env.getProperty("spring.datasource.username"));
hikari.setPassword(env.getProperty("spring.datasource.password"));
return hikari;
}
public JobRepository getJobRepository() {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(batchDataSource());
factory.setTransactionManager(manager());
factory.afterPropertiesSet();
return factory.getObject();
}
public PlatformTransactionManager manager() {
return new ResourcelessTransactionManager();
}
@Bean
public Step step() {
return stepBuilderFactory.get("step")
.chunk(1000)
.reader(reader())
.processor(processor())
.writer(writer())
.build();
}
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.flow(step())
.end()
.build();
}
@Bean
public JobLauncher getJobLauncher() {
SimpleJobLauncher launcher = new SimpleJobLauncher();
launcher.setJobRepository(createJobRepository());
return launcher;
}
}
在我正在使用的属性文件中,
spring.batch.job.enabled=false
spring.batch.initialize-schema=always
那么我错过了什么?我正在使用 JPA。甚至为什么它不使用可用的 JPA 数据源?如何强制 Spring 批处理使用默认 MySQL 而不是 InMemoryMap?
解决方案
首先,在 pom.xml 中明确定义 mysql-connector 依赖项,并从项目中删除与内存映射相关的任何内容。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
如果您想使用 bean 手动定义自己的配置,那么您不能使用 AutoConfiguration 类,因为它们会在启动时自动为您创建所需的 bean,如果您定义自己的自定义 DB 配置类,这可能会导致问题。因此,您必须排除DataSourceAutoConfiguration
,HibernateJpaAutoConfiguration
并DataSourceTransactionManagerAutoConfiguration
解决问题。
只需更新@SpringBootApplication
课程:
@SpringBootApplication(
exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class
}
)
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
推荐阅读
- angular - Angular 6 真的很长时间才能刷新
- php - ZF2:如何检查表是否已加入 Select
- python-3.x - 使用 Matplotlib imshow meshgrid 和自定义颜色创建图像
- three.js - 2 three.js 画布不同时渲染
- javascript - 如果条件使用 React Router V4 更改路由
- r - 如何在 R 中的给定数据框中找到每月平均支出?
- sql - 在 SQL 查询中尝试/捕获多个联合
- image - 在八度音阶的 ylabel 中打印带下标的绘图时间距没有错
- sql - SQL 将 varchar 转换为日期
- qt - qmake没有蓝牙