服务器之家

服务器之家 > 正文

深入解析SpringBatch适配器

时间:2022-03-10 13:47     来源/作者:境里婆娑

一、SpringBatch适配器

1、SpringBatch分别有读(reader)、处理(processor)、写(writer)、tasklet处理器。

  • 读适配器:ItemReaderAdapter
  • 处理适配器:ItemProcessorAdapter
  • 写适配器:ItemWriterAdapter
  • tasklet适配器:MethodInvokingTaskletAdapter

2、SpringBatch之所以给我们开这么多适配器原因是让我们把既有的服务作为参数传到适配器里面,避免开发重复代码。不得不说SpringBatch开发人员想的真周到。

3、SpringBatch适配器都有三个公共的方法:

  • public Object targetObject (目标对象,将要调用的实例)
  • public String targetMethod(目标方法,将要在实例上调用的方法)
  • public Object[] arguments(配置选型,用于提供一组数组类型参数)

二、SpringBatch适配器实战(Tasklet举例)

演示MethodInvokingTaskletAdapter适配器

1、创建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适配器执行的目标类和方法

?
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、适配器执行目标方法一定要先看看有没有参数,如果有参数一定要把此方法(setArguments)设置上,否则会报"No matching arguments found for method"异常

4、执行结果如图所示:

深入解析SpringBatch适配器

到此这篇关于SpringBatch适配器详解的文章就介绍到这了,更多相关SpringBatch适配器内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/TreeShu321/article/details/121068384

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
返回顶部