Skip to content

Commit 332e693

Browse files
committed
Resolves Circular Dependency issue
Makes sure that dependencies are passed in via parameters in bean declaraction vs. Autowiring refer to spring-projects/spring-batch#3991 resolves spring-cloud#797 Updated based on code review
1 parent 8b43520 commit 332e693

File tree

5 files changed

+30
-79
lines changed

5 files changed

+30
-79
lines changed

spring-cloud-starter-single-step-batch-job/src/main/java/org/springframework/cloud/task/batch/autoconfigure/flatfile/FlatFileItemReaderAutoConfiguration.java

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2020 the original author or authors.
2+
* Copyright 2019-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@
4141
* Autconfiguration for a {@code FlatFileItemReader}.
4242
*
4343
* @author Michael Minella
44+
* @author Glenn Renfro
4445
* @since 2.3
4546
*/
4647
@Configuration
@@ -50,29 +51,19 @@ public class FlatFileItemReaderAutoConfiguration {
5051

5152
private final FlatFileItemReaderProperties properties;
5253

53-
@Autowired(required = false)
54-
private LineTokenizer lineTokenizer;
55-
56-
@Autowired(required = false)
57-
private FieldSetMapper<Map<String, Object>> fieldSetMapper;
58-
59-
@Autowired(required = false)
60-
private LineMapper<Map<String, Object>> lineMapper;
61-
62-
@Autowired(required = false)
63-
private LineCallbackHandler skippedLinesCallback;
64-
65-
@Autowired(required = false)
66-
private RecordSeparatorPolicy recordSeparatorPolicy;
67-
6854
public FlatFileItemReaderAutoConfiguration(FlatFileItemReaderProperties properties) {
6955
this.properties = properties;
7056
}
7157

7258
@Bean
7359
@ConditionalOnMissingBean
7460
@ConditionalOnProperty(prefix = "spring.batch.job.flatfileitemreader", name = "name")
75-
public FlatFileItemReader<Map<String, Object>> itemReader() {
61+
public FlatFileItemReader<Map<String, Object>> itemReader(
62+
@Autowired(required = false) LineTokenizer lineTokenizer,
63+
@Autowired(required = false) FieldSetMapper<Map<String, Object>> fieldSetMapper,
64+
@Autowired(required = false) LineMapper<Map<String, Object>> lineMapper,
65+
@Autowired(required = false) LineCallbackHandler skippedLinesCallback,
66+
@Autowired(required = false) RecordSeparatorPolicy recordSeparatorPolicy) {
7667
FlatFileItemReaderBuilder<Map<String, Object>> mapFlatFileItemReaderBuilder = new FlatFileItemReaderBuilder<Map<String, Object>>()
7768
.name(this.properties.getName()).resource(this.properties.getResource())
7869
.saveState(this.properties.isSaveState())
@@ -84,26 +75,14 @@ public FlatFileItemReader<Map<String, Object>> itemReader() {
8475
.comments(this.properties.getComments()
8576
.toArray(new String[this.properties.getComments().size()]));
8677

87-
if (this.lineTokenizer != null) {
88-
mapFlatFileItemReaderBuilder.lineTokenizer(this.lineTokenizer);
89-
}
90-
91-
if (this.recordSeparatorPolicy != null) {
78+
mapFlatFileItemReaderBuilder.lineTokenizer(lineTokenizer);
79+
if (recordSeparatorPolicy != null) {
9280
mapFlatFileItemReaderBuilder
93-
.recordSeparatorPolicy(this.recordSeparatorPolicy);
94-
}
95-
96-
if (this.fieldSetMapper != null) {
97-
mapFlatFileItemReaderBuilder.fieldSetMapper(this.fieldSetMapper);
98-
}
99-
100-
if (this.lineMapper != null) {
101-
mapFlatFileItemReaderBuilder.lineMapper(this.lineMapper);
102-
}
103-
104-
if (this.skippedLinesCallback != null) {
105-
mapFlatFileItemReaderBuilder.skippedLinesCallback(skippedLinesCallback);
81+
.recordSeparatorPolicy(recordSeparatorPolicy);
10682
}
83+
mapFlatFileItemReaderBuilder.fieldSetMapper(fieldSetMapper);
84+
mapFlatFileItemReaderBuilder.lineMapper(lineMapper);
85+
mapFlatFileItemReaderBuilder.skippedLinesCallback(skippedLinesCallback);
10786

10887
if (this.properties.isDelimited()) {
10988
mapFlatFileItemReaderBuilder.delimited()

spring-cloud-starter-single-step-batch-job/src/main/java/org/springframework/cloud/task/batch/autoconfigure/jdbc/JdbcCursorItemReaderAutoConfiguration.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2020 the original author or authors.
2+
* Copyright 2020-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@
3838

3939
/**
4040
* @author Michael Minella
41+
* @author Glenn Renfro
4142
* @since 2.3
4243
*/
4344
@Configuration
@@ -50,12 +51,6 @@ public class JdbcCursorItemReaderAutoConfiguration {
5051

5152
private final DataSource dataSource;
5253

53-
@Autowired(required = false)
54-
private PreparedStatementSetter preparedStatementSetter;
55-
56-
@Autowired(required = false)
57-
private RowMapper<Map<String, Object>> rowMapper;
58-
5954
public JdbcCursorItemReaderAutoConfiguration(
6055
JdbcCursorItemReaderProperties properties, DataSource dataSource) {
6156
this.properties = properties;
@@ -64,7 +59,8 @@ public JdbcCursorItemReaderAutoConfiguration(
6459

6560
@Bean
6661
@ConditionalOnMissingBean
67-
public JdbcCursorItemReader<Map<String, Object>> itemReader() {
62+
public JdbcCursorItemReader<Map<String, Object>> itemReader(@Autowired(required = false) RowMapper<Map<String, Object>> rowMapper,
63+
@Autowired(required = false) PreparedStatementSetter preparedStatementSetter) {
6864
return new JdbcCursorItemReaderBuilder<Map<String, Object>>()
6965
.name(this.properties.getName())
7066
.currentItemCount(this.properties.getCurrentItemCount())
@@ -76,8 +72,8 @@ public JdbcCursorItemReader<Map<String, Object>> itemReader() {
7672
.maxRows(this.properties.getMaxRows())
7773
.queryTimeout(this.properties.getQueryTimeout())
7874
.saveState(this.properties.isSaveState()).sql(this.properties.getSql())
79-
.rowMapper(this.rowMapper)
80-
.preparedStatementSetter(this.preparedStatementSetter)
75+
.rowMapper(rowMapper)
76+
.preparedStatementSetter(preparedStatementSetter)
8177
.verifyCursorPosition(this.properties.isVerifyCursorPosition())
8278
.useSharedExtendedConnection(
8379
this.properties.isUseSharedExtendedConnection())

spring-cloud-starter-single-step-batch-job/src/test/java/org/springframework/cloud/task/batch/autoconfigure/flatfile/FlatFileItemReaderAutoConfigurationTests.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2020 the original author or authors.
2+
* Copyright 2020-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,19 +27,15 @@
2727
import org.springframework.batch.core.JobExecution;
2828
import org.springframework.batch.core.JobParameters;
2929
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
30-
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
31-
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
3230
import org.springframework.batch.core.explore.JobExplorer;
3331
import org.springframework.batch.core.launch.JobLauncher;
34-
import org.springframework.batch.item.file.FlatFileItemReader;
3532
import org.springframework.batch.item.file.LineCallbackHandler;
3633
import org.springframework.batch.item.file.LineMapper;
3734
import org.springframework.batch.item.file.mapping.FieldSetMapper;
3835
import org.springframework.batch.item.file.separator.RecordSeparatorPolicy;
3936
import org.springframework.batch.item.file.transform.DefaultFieldSet;
4037
import org.springframework.batch.item.file.transform.LineTokenizer;
4138
import org.springframework.batch.item.support.ListItemWriter;
42-
import org.springframework.beans.factory.annotation.Autowired;
4339
import org.springframework.boot.autoconfigure.AutoConfigurations;
4440
import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration;
4541
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
@@ -53,6 +49,7 @@
5349

5450
/**
5551
* @author Michael Minella
52+
* @author Glenn Renfro
5653
*/
5754
public class FlatFileItemReaderAutoConfigurationTests {
5855

@@ -319,15 +316,6 @@ public void testCustomMapping() {
319316
@Configuration
320317
public static class CustomMappingConfiguration {
321318

322-
@Autowired
323-
private JobBuilderFactory jobBuilderFactory;
324-
325-
@Autowired
326-
private StepBuilderFactory stepBuilderFactory;
327-
328-
@Autowired
329-
private FlatFileItemReader itemReader;
330-
331319
@Bean
332320
public ListItemWriter<Map<String, Object>> itemWriter() {
333321
return new ListItemWriter<>();

spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/listener/PrefixTests.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2019 the original author or authors.
2+
* Copyright 2018-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,6 @@
2828
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
2929
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
3030
import org.springframework.batch.repeat.RepeatStatus;
31-
import org.springframework.beans.factory.annotation.Autowired;
3231
import org.springframework.boot.SpringApplication;
3332
import org.springframework.cloud.task.batch.configuration.TaskBatchTest;
3433
import org.springframework.cloud.task.configuration.EnableTask;
@@ -39,7 +38,7 @@
3938
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
4039
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
4140

42-
import static org.assertj.core.api.Assertions.assertThat;
41+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
4342

4443
/**
4544
* @author Glenn Renfro
@@ -73,15 +72,9 @@ public void testPrefix() {
7372
@EnableTask
7473
public static class JobConfiguration {
7574

76-
@Autowired
77-
private JobBuilderFactory jobBuilderFactory;
78-
79-
@Autowired
80-
private StepBuilderFactory stepBuilderFactory;
81-
8275
@Bean
83-
public Job job() {
84-
return this.jobBuilderFactory.get("job").start(this.stepBuilderFactory
76+
public Job job(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
77+
return jobBuilderFactory.get("job").start(stepBuilderFactory
8578
.get("step1").tasklet((contribution, chunkContext) -> {
8679
System.out.println("Executed");
8780
return RepeatStatus.FINISHED;

spring-cloud-task-batch/src/test/java/org/springframework/cloud/task/batch/listener/TaskBatchExecutionListenerTests.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -361,16 +361,11 @@ public boolean isSingleton() {
361361
@Import(EmbeddedDataSourceConfiguration.class)
362362
public static class JobConfigurationMultipleDataSources {
363363

364-
@Autowired
365-
private JobBuilderFactory jobBuilderFactory;
366-
367-
@Autowired
368-
private StepBuilderFactory stepBuilderFactory;
369364

370365
@Bean
371-
public Job job() {
372-
return this.jobBuilderFactory.get("job")
373-
.start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
366+
public Job job(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
367+
return jobBuilderFactory.get("job")
368+
.start(stepBuilderFactory.get("step1").tasklet(new Tasklet() {
374369
@Override
375370
public RepeatStatus execute(StepContribution contribution,
376371
ChunkContext chunkContext) throws Exception {

0 commit comments

Comments
 (0)