深入解析SpringBatch適配器
一、SpringBatch適配器
1、SpringBatch分別有讀(reader)、處理(processor)、寫(writer)、tasklet處理器。
- 讀適配器:ItemReaderAdapter
- 處理適配器:ItemProcessorAdapter
- 寫適配器:ItemWriterAdapter
- tasklet適配器:MethodInvokingTaskletAdapter
2、SpringBatch之所以給我們開這么多適配器原因是讓我們把既有的服務作為參數(shù)傳到適配器里面,避免開發(fā)重復代碼。不得不說SpringBatch開發(fā)人員想的真周到。
3、SpringBatch適配器都有三個公共的方法:
- public Object targetObject (目標對象,將要調(diào)用的實例)
- public String targetMethod(目標方法,將要在實例上調(diào)用的方法)
- public Object[] arguments(配置選型,用于提供一組數(shù)組類型參數(shù))
二、SpringBatch適配器實戰(zhàn)(Tasklet舉例)
演示MethodInvokingTaskletAdapter適配器
1、創(chuàng)建Job配置TaskletAdapterConfiguration
@Configuration @EnableBatchProcessing public class TaskletAdapterConfiguration { @Autowired private JobBuilderFactory jobBuilderFactory; @Autowired private StepBuilderFactory stepBuilderFactory; @Autowired public PeopleService peopleService; @Bean public Job taskletAdapterJob() { return jobBuilderFactory.get("taskletAdapterJob") .start(taskletAdapterStep()) .build(); } @Bean public Step taskletAdapterStep() { return stepBuilderFactory.get("taskletAdapterStep") .tasklet(methodInvokingTaskletAdapter()) .build(); } @Bean public MethodInvokingTaskletAdapter methodInvokingTaskletAdapter() { MethodInvokingTaskletAdapter adapter = new MethodInvokingTaskletAdapter(); adapter.setTargetObject(peopleService); adapter.setTargetMethod("upperCase"); adapter.setArguments(new Object[]{new People("lee","10","北京","1233")}); return adapter; } }
2、Tasklet適配器執(zhí)行的目標類和方法
@Service public class PeopleService { public People upperCase(People people) { People p = new People(); p.setName(people.getName().toUpperCase(Locale.ROOT)); p.setAdress(people.getAdress().toUpperCase(Locale.ROOT)); p.setAge(people.getAge()); p.setIdCard(people.getIdCard()); System.out.println("p:" + p); return p; } }
3、適配器執(zhí)行目標方法一定要先看看有沒有參數(shù),如果有參數(shù)一定要把此方法(setArguments)設置上,否則會報"No matching arguments found for method"異常
4、執(zhí)行結果如圖所示:
到此這篇關于SpringBatch適配器詳解的文章就介紹到這了,更多相關SpringBatch適配器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
ehcache開源緩存框架_動力節(jié)點Java學院整理
Ehcache是現(xiàn)在最流行的純Java開源緩存框架,這篇文章主要介紹了ehcache框架的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07SpringBoot全局配置long轉(zhuǎn)String丟失精度問題解決方案
這篇文章主要介紹了SpringBoot全局配置long轉(zhuǎn)String丟失精度問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08Java獲取http和https協(xié)議返回的json數(shù)據(jù)
本篇文章主要介紹了Java獲取http和https協(xié)議返回的json數(shù)據(jù) ,本篇文章提供兩個方法,幫助各位如何獲取http和https返回的數(shù)據(jù)。有興趣的可以了解一下。2017-01-01java數(shù)據(jù)輸出打印流PrintStream和PrintWriter面試精講
這篇文章主要為大家介紹了java數(shù)據(jù)輸出打印流PrintStream和PrintWriter面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10