首页 > 解决方案 > 创建名为“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?

标签: javaspring-bootspring-batchdatasource

解决方案


首先,在 pom.xml 中明确定义 mysql-connector 依赖项,并从项目中删除与内存映射相关的任何内容。

  <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
   </dependency>

如果您想使用 bean 手动定义自己的配置,那么您不能使用 AutoConfiguration 类,因为它们会在启动时自动为您创建所需的 bean,如果您定义自己的自定义 DB 配置类,这可能会导致问题。因此,您必须排除DataSourceAutoConfiguration,HibernateJpaAutoConfigurationDataSourceTransactionManagerAutoConfiguration解决问题。

只需更新@SpringBootApplication课程:

@SpringBootApplication(
      exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class
      }
    )
    public class App {
    
      public static void main(String[] args) {
        SpringApplication.run(App.class, args);
      }
    }

推荐阅读