001/* 002 * Copyright 2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.batch.sample.validation; 018 019import java.util.Arrays; 020 021import org.springframework.batch.core.Job; 022import org.springframework.batch.core.Step; 023import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; 024import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; 025import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; 026import org.springframework.batch.item.support.ListItemReader; 027import org.springframework.batch.item.support.ListItemWriter; 028import org.springframework.batch.item.validator.BeanValidatingItemProcessor; 029import org.springframework.batch.sample.validation.domain.Person; 030import org.springframework.beans.factory.annotation.Autowired; 031import org.springframework.context.annotation.Bean; 032import org.springframework.context.annotation.Configuration; 033 034/** 035 * @author Mahmoud Ben Hassine 036 */ 037@Configuration 038@EnableBatchProcessing 039public class ValidationSampleConfiguration { 040 041 @Autowired 042 private JobBuilderFactory jobs; 043 044 @Autowired 045 private StepBuilderFactory steps; 046 047 @Bean 048 public ListItemReader<Person> itemReader() { 049 Person person1 = new Person(1, "foo"); 050 Person person2 = new Person(2, ""); 051 return new ListItemReader<>(Arrays.asList(person1, person2)); 052 } 053 054 @Bean 055 public ListItemWriter<Person> itemWriter() { 056 return new ListItemWriter<>(); 057 } 058 059 @Bean 060 public BeanValidatingItemProcessor<Person> itemValidator() throws Exception { 061 BeanValidatingItemProcessor<Person> validator = new BeanValidatingItemProcessor<>(); 062 validator.setFilter(true); 063 validator.afterPropertiesSet(); 064 065 return validator; 066 } 067 068 @Bean 069 public Step step() throws Exception { 070 return this.steps.get("step") 071 .<Person, Person>chunk(1) 072 .reader(itemReader()) 073 .processor(itemValidator()) 074 .writer(itemWriter()) 075 .build(); 076 } 077 078 @Bean 079 public Job job() throws Exception { 080 return this.jobs.get("job") 081 .start(step()) 082 .build(); 083 } 084 085}