如何使用Jobrepository中的Spring-boot配置并获得的JDBCTEMPLATE?

发布于 2025-01-31 07:14:06 字数 426 浏览 3 评论 0原文

HIHO,

我们在Spring-Boot 2.6.7服务中使用Spring Batch 4.3.5。到目前为止,所有事情都可以正常工作。在测试用例时,我们意识到batchautoconfiguration/batchconfigurerconfiguration创建了一个Jobrepository。这个工作需要的需要,需要一些JDBCoperations。因为在初始化所有bean时,没有从春季应用程序上下文中获取JDBCoperations的实例,所以JobReRepositoryFactoryBean决定创建一个类型的JDBCTEMPLATE的新实例并将其附加到JobReRepository。

因此,我想询问是否有可能附加Spring-Boot提供的JDBCTEMPLATE实例的“简单”?覆盖整个初始化机制是否还有另一种可能性?我们需要提供自己的批评家吗?

任何帮助都非常感谢! :)

Hiho,

we use Spring-Batch 4.3.5 inside a Spring-Boot 2.6.7 service. All things work fine so far. While unit testing the use-cases, we realized that the BatchAutoConfiguration/BatchConfigurerConfiguration creates a JobRepository. This JobRepository needs and wants some JdbcOperations. Because no instance of JdbcOperations is taken from the Spring application context while initializing all the beans, the JobRepositoryFactoryBean decides to create a fresh instance of type JdbcTemplate and attach it to the JobRepository.

Therefore I would like to ask if there is an 'easy' possibility to attach the instance of the JdbcTemplate that is provided by Spring-Boot? Is there another possibility as overwriting the whole initialization mechanism? Do we need to provide our own BatchConfigurer?

Any help is really appreciated! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南冥有猫 2025-02-07 07:14:06

那是不可能的。您需要提供一个自定义batchConfigurer,并使用boot启动的任何bean自动配置来配置您的作业存储库。这是一个快速示例:

@Bean
public BatchConfigurer batchConfigurer(DataSource dataSource, JdbcTemplate jdbcTemplate) {
    return new DefaultBatchConfigurer(dataSource) {
        @Override
        protected JobRepository createJobRepository() throws Exception {
            JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean();
            factoryBean.setJdbcOperations(jdbcTemplate);
            // set other properties on the factory bean
            factoryBean.afterPropertiesSet();
            return factoryBean.getObject();
        }
    };
}

在此片段中,dataSourcejdbctemplate作为参数传递给batchconfigurer方法将是由boot自动配置的方法(并自动由春季自动)。

That is not possible. You need to provide a custom BatchConfigurer and use any bean auto-configured by Boot to configure your job repository. Here is a quick example:

@Bean
public BatchConfigurer batchConfigurer(DataSource dataSource, JdbcTemplate jdbcTemplate) {
    return new DefaultBatchConfigurer(dataSource) {
        @Override
        protected JobRepository createJobRepository() throws Exception {
            JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean();
            factoryBean.setJdbcOperations(jdbcTemplate);
            // set other properties on the factory bean
            factoryBean.afterPropertiesSet();
            return factoryBean.getObject();
        }
    };
}

In this snippet, the dataSource and jdbcTemplate passed as parameters to the batchConfigurer method will be those auto-configured by Boot (and autowired by Spring).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文