一、SpringBatch適配器
1、SpringBatch分別有讀(reader)、處理(processor)、寫(writer)、tasklet處理器。
- 讀適配器:ItemReaderAdapter
- 處理適配器:ItemProcessorAdapter
- 寫適配器:ItemWriterAdapter
- tasklet適配器:MethodInvokingTaskletAdapter
2、SpringBatch之所以給我們開這么多適配器原因是讓我們把既有的服務(wù)作為參數(shù)傳到適配器里面,避免開發(fā)重復(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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
@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í)行的目標類和方法
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@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)設(shè)置上,否則會報"No matching arguments found for method"異常
4、執(zhí)行結(jié)果如圖所示:
到此這篇關(guān)于SpringBatch適配器詳解的文章就介紹到這了,更多相關(guān)SpringBatch適配器內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/TreeShu321/article/details/121068384