You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3升级后DefaultBatchConfigurer报错及数据源配置问题

Spring Boot 2.7升级至3.0解决Batch配置相关问题

解决defaultBatchConfigurer cannot be resolved to a type错误

Spring Boot 3.0集成的Spring Batch版本升级到5.x,原DefaultBatchConfigurer类已被移除,替代类为DefaultBatchConfiguration(注意类名后缀是Configuration而非Configurer)。确保导入正确的类:

import org.springframework.batch.core.configuration.annotation.DefaultBatchConfiguration;

数据源配置替代方案

原通过继承类并重写setDataSource的方式不再适用,因为DefaultBatchConfiguration没有该方法,可通过以下两种方式配置:

方式一:继承DefaultBatchConfiguration并重写dataSource方法

通过构造注入自定义数据源,重写父类的dataSource方法返回该数据源:

import org.springframework.batch.core.configuration.annotation.DefaultBatchConfiguration;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;

@Configuration
public class CustomBatchConfig extends DefaultBatchConfiguration {

    private final DataSource customDataSource;

    // 构造注入自定义数据源
    public CustomBatchConfig(DataSource customDataSource) {
        this.customDataSource = customDataSource;
    }

    @Override
    protected DataSource dataSource() {
        return this.customDataSource;
    }
}

方式二:自定义BatchConfigurer Bean

如果需要更精细的控制(比如多数据源场景),可以直接定义BatchConfigurer Bean,手动配置JobRepository、JobLauncher等组件:

import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;

@Configuration
public class CustomBatchConfig {

    private final DataSource customDataSource;
    private final PlatformTransactionManager transactionManager;

    public CustomBatchConfig(DataSource customDataSource, PlatformTransactionManager transactionManager) {
        this.customDataSource = customDataSource;
        this.transactionManager = transactionManager;
    }

    @Bean
    public BatchConfigurer batchConfigurer() {
        return new BatchConfigurer() {
            @Override
            public JobRepository getJobRepository() throws Exception {
                JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
                factory.setDataSource(customDataSource);
                factory.setTransactionManager(transactionManager);
                factory.setIsolationLevelForCreate("ISOLATION_REPEATABLE_READ");
                factory.afterPropertiesSet();
                return factory.getObject();
            }

            @Override
            public JobLauncher getJobLauncher() throws Exception {
                SimpleJobLauncher launcher = new SimpleJobLauncher();
                launcher.setJobRepository(getJobRepository());
                launcher.afterPropertiesSet();
                return launcher;
            }

            @Override
            public JobExplorer getJobExplorer() throws Exception {
                JobExplorerFactoryBean factory = new JobExplorerFactoryBean();
                factory.setDataSource(customDataSource);
                factory.afterPropertiesSet();
                return factory.getObject();
            }

            @Override
            public PlatformTransactionManager getTransactionManager() {
                return transactionManager;
            }
        };
    }
}

额外注意事项

  • 确保spring-boot-starter-batch依赖版本与Spring Boot 3.0保持一致,避免版本冲突
  • 若使用Spring Boot自动配置,容器中存在自定义DataSource Bean时,Spring Batch会自动使用该数据源,无需额外配置(特殊场景除外)

内容的提问来源于stack exchange,提问作者KARTHICK RAJA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 16:02:35