Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analyze SpringBatch Adapter

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "analyzing SpringBatch adapter". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

I. SpringBatch Adapter

SpringBatch has reader, processor, writer and tasklet processors.

Read adapter: ItemReaderAdapter

Processing Adapter: ItemProcessorAdapter

Write Adapter: ItemWriterAdapter

tasklet adapter: MethodInvokingTaskletAdapter

2. The reason why SpringBatch gives us so many adapters is to let us pass the existing services as parameters to the adapters to avoid developing duplicate code. I have to say that SpringBatch developers are really thoughtful.

SpringBatch adapters have three common methods:

public Object targetObject

public String targetMethod

public Object[] arguments

II. SpringBatch adapter actual combat (Tasklet example)

Demonstrate MethodInvokingTaskletAdapter

1. Create Job Configuration 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","Beijing","1233")}); return adapter; } }

2. Target classes and methods executed by Tasklet adapters

@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. When executing the target method of the adapter, you must first check whether there are parameters. If there are parameters, you must set this method (setArguments), otherwise you will report an exception of "No matching arguments found for method".

4. The execution results are shown in the figure:

This is the end of analyzing SpringBatch adapters. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report