[英]How to use the JdbcTemplate that is configured and available through Spring-Boot in the JobRepository?
嗨,
我们在 Spring-Boot 2.6.7 服务中使用 Spring-Batch 4.3.5。 到目前为止一切正常。 在对用例进行单元测试时,我们意识到 BatchAutoConfiguration/BatchConfigurerConfiguration 创建了一个 JobRepository。 这个 JobRepository 需要并且想要一些 JdbcOperations。 因为在初始化所有 bean 时没有从 Spring 应用程序上下文中获取 JdbcOperations 的实例,所以 JobRepositoryFactoryBean 决定创建一个 JdbcTemplate 类型的新实例并将其附加到 JobRepository。
因此,我想问一下是否有“简单”的可能性来附加 Spring-Boot 提供的 JdbcTemplate 的实例? 是否有另一种可能覆盖整个初始化机制? 我们需要提供自己的 BatchConfigurer 吗?
非常感谢任何帮助! :)
这是不可能的。 您需要提供一个自定义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();
}
};
}
在这个片段中,作为参数传递给batchConfigurer
方法的dataSource
和jdbcTemplate
将是由 Boot 自动配置的(并由 Spring 自动装配)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.