嗨,
我們在 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 嗎?
非常感謝任何幫助!:)
uj5u.com熱心網友回復:
這是不可能的。您需要提供自定義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();
}
};
}
在這個片段中,作為引數傳遞給方法的dataSource
和jdbcTemplate
將batchConfigurer
是那些由 Boot 自動配置(并由 Spring 自動裝配)的引數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/480017.html
下一篇:Springboot和Zuul代理DeferringLoadBalancerExchangeFilterFunction錯誤