Get the App
SLTechnology News&Howtos  ›  Development  › 

Analyze SpringBatch Adapter

Shulou Source: shulou.com Published: 2022-06-02 18:19:24 09月16日 Update

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!

Tags: Adapters adapters methods parameters goals processing analysis content examples more knowledge development configuration practical thoughtful successful learning and then three the reason Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Microsoft MySQL Shulou Technology Shulou Information