From fcf3db35859896aec12e91fc3c0ff85597fe9bf3 Mon Sep 17 00:00:00 2001 From: Michael Minella Date: Wed, 10 Jun 2015 10:21:51 -0500 Subject: [PATCH] Refactored DI to be more explicit --- .../main/java/hello/BatchConfiguration.java | 51 +++++++++++-------- .../JobCompletionNotificationListener.java | 1 - 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/complete/src/main/java/hello/BatchConfiguration.java b/complete/src/main/java/hello/BatchConfiguration.java index fc0c3341af..388c91e254 100644 --- a/complete/src/main/java/hello/BatchConfiguration.java +++ b/complete/src/main/java/hello/BatchConfiguration.java @@ -9,15 +9,13 @@ import org.springframework.batch.core.configuration.annotation.EnableBatchProces import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.launch.support.RunIdIncrementer; -import org.springframework.batch.item.ItemProcessor; -import org.springframework.batch.item.ItemReader; -import org.springframework.batch.item.ItemWriter; import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider; import org.springframework.batch.item.database.JdbcBatchItemWriter; import org.springframework.batch.item.file.FlatFileItemReader; import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper; import org.springframework.batch.item.file.mapping.DefaultLineMapper; import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; @@ -27,9 +25,18 @@ import org.springframework.jdbc.core.JdbcTemplate; @EnableBatchProcessing public class BatchConfiguration { + @Autowired + public JobBuilderFactory jobBuilderFactory; + + @Autowired + public StepBuilderFactory stepBuilderFactory; + + @Autowired + public DataSource dataSource; + // tag::readerwriterprocessor[] @Bean - public ItemReader reader() { + public FlatFileItemReader reader() { FlatFileItemReader reader = new FlatFileItemReader(); reader.setResource(new ClassPathResource("sample-data.csv")); reader.setLineMapper(new DefaultLineMapper() {{ @@ -44,12 +51,12 @@ public class BatchConfiguration { } @Bean - public ItemProcessor processor() { + public PersonItemProcessor processor() { return new PersonItemProcessor(); } @Bean - public ItemWriter writer(DataSource dataSource) { + public JdbcBatchItemWriter writer() { JdbcBatchItemWriter writer = new JdbcBatchItemWriter(); writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider()); writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)"); @@ -58,32 +65,34 @@ public class BatchConfiguration { } // end::readerwriterprocessor[] + // tag::listener[] + + @Bean + public JobExecutionListener listener() { + return new JobCompletionNotificationListener(new JdbcTemplate(dataSource)); + } + + // end::listener[] + // tag::jobstep[] @Bean - public Job importUserJob(JobBuilderFactory jobs, Step s1, JobExecutionListener listener) { - return jobs.get("importUserJob") + public Job importUserJob() { + return jobBuilderFactory.get("importUserJob") .incrementer(new RunIdIncrementer()) - .listener(listener) - .flow(s1) + .listener(listener()) + .flow(step1()) .end() .build(); } @Bean - public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader reader, - ItemWriter writer, ItemProcessor processor) { + public Step step1() { return stepBuilderFactory.get("step1") . chunk(10) - .reader(reader) - .processor(processor) - .writer(writer) + .reader(reader()) + .processor(processor()) + .writer(writer()) .build(); } // end::jobstep[] - - @Bean - public JdbcTemplate jdbcTemplate(DataSource dataSource) { - return new JdbcTemplate(dataSource); - } - } diff --git a/complete/src/main/java/hello/JobCompletionNotificationListener.java b/complete/src/main/java/hello/JobCompletionNotificationListener.java index e66aca5da8..a066e0399c 100644 --- a/complete/src/main/java/hello/JobCompletionNotificationListener.java +++ b/complete/src/main/java/hello/JobCompletionNotificationListener.java @@ -15,7 +15,6 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Component; -@Component public class JobCompletionNotificationListener extends JobExecutionListenerSupport { private static final Logger log = LoggerFactory.getLogger(JobCompletionNotificationListener.class);