分析SpringBatch适配器


本篇内容介绍了“分析SpringBatch适配器”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一、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

@Configuration@EnableBatchProcessingpublic 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适配器执行的目标类和方法

@Servicepublic 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适配器”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注主机评测网网站,小编将为大家输出更多高质量的实用文章!


上一篇:SpringBoot中启动时怎么忽略某项检测

下一篇:怎么解决php exec中文不执行问题


Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器