spring-data-jpa 在引入Spring Batch @EnableBatchProcessing注解后未创建DataSourceTransactionManager Bean

6fe3ivhb  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(200)

我在一个现有的应用程序中集成了Spring Batch,而其他bean也成功地使用了DataSourceTransactionManager。但是,当我引入@EnableBatchProcessing注解时,我得到了以下错误:
AuthBeans中的字段事务管理器需要一个类型为“org.springframework.jdbc.datasource.DataSourceTransactionManager”的Bean,但找不到该Bean。
我已经尝试配置BatchConfigurer,但它没有解决问题。请查看下面的BatchConfigurer代码。

@Configuration
public class CustomBatchConfigurer implements BatchConfigurer {

    @Autowired
    private DataSource dataSource;

    @Override
    public JobRepository getJobRepository() throws Exception {
        JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
        factory.setDataSource(dataSource);
        factory.setTransactionManager(getTransactionManager());
        factory.afterPropertiesSet();
        return  (JobRepository) factory.getObject();
    }

    @Override
    public PlatformTransactionManager getTransactionManager() throws Exception {
        return new DataSourceTransactionManager(dataSource);
    }

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

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

我想知道我错过了什么吗?你的帮助,这将是非常感谢。

oxf4rvwz

oxf4rvwz1#

在哪个类中,您保留了@EnableBatchProcessing注解。@EnableBatchProcessing应该位于最高级别。
默认情况下,@EnableBatchProcessing将创建DataSourceTransactionManager
通过这个BatchConfigure并遵循example
我希望这将解决您的问题。

相关问题