javascript
SpringBatch接口BatchConfigurer详解
前言:BatchConfigurer作為策略接口提供了自定義SpringBatch基礎(chǔ)設(shè)施組件能力。在使用@EnableBatchProcessing注解后,即可獲取每個(gè)SpringBatch基礎(chǔ)設(shè)施組件實(shí)例。
BatchConfigurer接口
當(dāng)程序使用了@EnableBatchProcessing注解時(shí),SpringBatch會(huì)執(zhí)行以下流程。首先通過(guò)BatchConfigurer接口的實(shí)現(xiàn)來(lái)創(chuàng)建這些bean,然后通過(guò)SimpleBatchConfigurationt將它們添加到Spring ApplicationContext中。
?
BatchConfigurer接口方法
BatchConfigurer提供了四個(gè)方法,每個(gè)方法都是為SpringBatch基礎(chǔ)設(shè)施提供了一個(gè)主要組件。
- JobRepository操作SpringBatch元數(shù)據(jù)庫(kù)增刪改查
- JobLauncher啟動(dòng)SpringBatch作業(yè)入口
- PlatformTransactionManager 為SpringBatch提供所有的事務(wù)管理
- JobExplorer提供了針對(duì)作業(yè)存儲(chǔ)庫(kù)中所保存數(shù)據(jù)的只讀視圖
自定義BatchConfigurer
如果默認(rèn)的DefaultBatchConfigurer不滿足業(yè)務(wù)現(xiàn)狀需要我們自定義BatchConfigurer。其實(shí)我們自定義的BatchConfigurer主要是要從寫DefaultBatchConfigurer類里面方法實(shí)現(xiàn)。
/*** @author: slzhao* @date 2021/11/1122:17*/ public class CustomBatchConfigurer extends DefaultBatchConfigurer {@Resource(name = "respDataSource")private DataSource dataSource;@Autowiredprivate PlatformTransactionManager platformTransactionManager;@Overridepublic PlatformTransactionManager getTransactionManager() {return platformTransactionManager;}@Overrideprotected JobExplorer createJobExplorer() throws Exception {JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();jobExplorerFactoryBean.setDataSource(this.dataSource);jobExplorerFactoryBean.setTablePrefix("ZSL_");jobExplorerFactoryBean.afterPropertiesSet();return jobExplorerFactoryBean.getObject();}@Overrideprotected JobLauncher createJobLauncher() throws Exception {return super.createJobLauncher();}@Overrideprotected JobRepository createJobRepository() throws Exception {JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();factory.setDataSource(this.dataSource);factory.setTablePrefix("ZSL_");factory.setTransactionManager(platformTransactionManager);factory.afterPropertiesSet();return factory.getObject();} }注意:由于JobRepository和JobExplorer使用相同的底層數(shù)據(jù)存儲(chǔ),因此同時(shí)配置它們確保一直。
總結(jié)
以上是生活随笔為你收集整理的SpringBatch接口BatchConfigurer详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SpringBatch批处理框架入门(二
- 下一篇: SpringBatch处理器Script