當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBatch接口BatchConfigurer详解
生活随笔
收集整理的這篇文章主要介紹了
SpringBatch接口BatchConfigurer详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言:BatchConfigurer作為策略接口提供了自定義SpringBatch基礎設施組件能力。在使用@EnableBatchProcessing注解后,即可獲取每個SpringBatch基礎設施組件實例。
BatchConfigurer接口
當程序使用了@EnableBatchProcessing注解時,SpringBatch會執行以下流程。首先通過BatchConfigurer接口的實現來創建這些bean,然后通過SimpleBatchConfigurationt將它們添加到Spring ApplicationContext中。
?
BatchConfigurer接口方法
BatchConfigurer提供了四個方法,每個方法都是為SpringBatch基礎設施提供了一個主要組件。
- JobRepository操作SpringBatch元數據庫增刪改查
- JobLauncher啟動SpringBatch作業入口
- PlatformTransactionManager 為SpringBatch提供所有的事務管理
- JobExplorer提供了針對作業存儲庫中所保存數據的只讀視圖
自定義BatchConfigurer
如果默認的DefaultBatchConfigurer不滿足業務現狀需要我們自定義BatchConfigurer。其實我們自定義的BatchConfigurer主要是要從寫DefaultBatchConfigurer類里面方法實現。
/*** @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使用相同的底層數據存儲,因此同時配置它們確保一直。
總結
以上是生活随笔為你收集整理的SpringBatch接口BatchConfigurer详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBatch批处理框架入门(二
- 下一篇: SpringBatch处理器Script