Merge pull request #11 from mminella/ISSUE-10
Refactored DI to be more explicit
This commit is contained in:
commit
3633545a77
@ -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.JobBuilderFactory;
|
||||||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
||||||
import org.springframework.batch.core.launch.support.RunIdIncrementer;
|
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.BeanPropertyItemSqlParameterSourceProvider;
|
||||||
import org.springframework.batch.item.database.JdbcBatchItemWriter;
|
import org.springframework.batch.item.database.JdbcBatchItemWriter;
|
||||||
import org.springframework.batch.item.file.FlatFileItemReader;
|
import org.springframework.batch.item.file.FlatFileItemReader;
|
||||||
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
|
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
|
||||||
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
|
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
|
||||||
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
|
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.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
@ -27,9 +25,18 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
|||||||
@EnableBatchProcessing
|
@EnableBatchProcessing
|
||||||
public class BatchConfiguration {
|
public class BatchConfiguration {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public JobBuilderFactory jobBuilderFactory;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public StepBuilderFactory stepBuilderFactory;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public DataSource dataSource;
|
||||||
|
|
||||||
// tag::readerwriterprocessor[]
|
// tag::readerwriterprocessor[]
|
||||||
@Bean
|
@Bean
|
||||||
public ItemReader<Person> reader() {
|
public FlatFileItemReader<Person> reader() {
|
||||||
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
|
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
|
||||||
reader.setResource(new ClassPathResource("sample-data.csv"));
|
reader.setResource(new ClassPathResource("sample-data.csv"));
|
||||||
reader.setLineMapper(new DefaultLineMapper<Person>() {{
|
reader.setLineMapper(new DefaultLineMapper<Person>() {{
|
||||||
@ -44,12 +51,12 @@ public class BatchConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ItemProcessor<Person, Person> processor() {
|
public PersonItemProcessor processor() {
|
||||||
return new PersonItemProcessor();
|
return new PersonItemProcessor();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ItemWriter<Person> writer(DataSource dataSource) {
|
public JdbcBatchItemWriter<Person> writer() {
|
||||||
JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
|
JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
|
||||||
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
|
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
|
||||||
writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
|
writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
|
||||||
@ -58,32 +65,34 @@ public class BatchConfiguration {
|
|||||||
}
|
}
|
||||||
// end::readerwriterprocessor[]
|
// end::readerwriterprocessor[]
|
||||||
|
|
||||||
|
// tag::listener[]
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public JobExecutionListener listener() {
|
||||||
|
return new JobCompletionNotificationListener(new JdbcTemplate(dataSource));
|
||||||
|
}
|
||||||
|
|
||||||
|
// end::listener[]
|
||||||
|
|
||||||
// tag::jobstep[]
|
// tag::jobstep[]
|
||||||
@Bean
|
@Bean
|
||||||
public Job importUserJob(JobBuilderFactory jobs, Step s1, JobExecutionListener listener) {
|
public Job importUserJob() {
|
||||||
return jobs.get("importUserJob")
|
return jobBuilderFactory.get("importUserJob")
|
||||||
.incrementer(new RunIdIncrementer())
|
.incrementer(new RunIdIncrementer())
|
||||||
.listener(listener)
|
.listener(listener())
|
||||||
.flow(s1)
|
.flow(step1())
|
||||||
.end()
|
.end()
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Person> reader,
|
public Step step1() {
|
||||||
ItemWriter<Person> writer, ItemProcessor<Person, Person> processor) {
|
|
||||||
return stepBuilderFactory.get("step1")
|
return stepBuilderFactory.get("step1")
|
||||||
.<Person, Person> chunk(10)
|
.<Person, Person> chunk(10)
|
||||||
.reader(reader)
|
.reader(reader())
|
||||||
.processor(processor)
|
.processor(processor())
|
||||||
.writer(writer)
|
.writer(writer())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
// end::jobstep[]
|
// end::jobstep[]
|
||||||
|
|
||||||
@Bean
|
|
||||||
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
|
|
||||||
return new JdbcTemplate(dataSource);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
|||||||
import org.springframework.jdbc.core.RowMapper;
|
import org.springframework.jdbc.core.RowMapper;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
|
||||||
public class JobCompletionNotificationListener extends JobExecutionListenerSupport {
|
public class JobCompletionNotificationListener extends JobExecutionListenerSupport {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(JobCompletionNotificationListener.class);
|
private static final Logger log = LoggerFactory.getLogger(JobCompletionNotificationListener.class);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user