Skip to content

Commit db7fa32

Browse files
committed
Refactor samples to use a common datasource configuration
Issue #4329
1 parent 746ffab commit db7fa32

File tree

34 files changed

+111
-512
lines changed

34 files changed

+111
-512
lines changed

spring-batch-samples/src/main/java/org/springframework/batch/samples/amqp/AmqpJobConfiguration.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.batch.samples.amqp;
1818

19-
import javax.sql.DataSource;
20-
2119
import org.springframework.amqp.rabbit.core.RabbitTemplate;
2220
import org.springframework.batch.core.Job;
2321
import org.springframework.batch.core.Step;
@@ -29,10 +27,10 @@
2927
import org.springframework.batch.item.ItemWriter;
3028
import org.springframework.batch.item.amqp.builder.AmqpItemReaderBuilder;
3129
import org.springframework.batch.item.amqp.builder.AmqpItemWriterBuilder;
30+
import org.springframework.batch.samples.common.DataSourceConfiguration;
3231
import org.springframework.context.annotation.Bean;
3332
import org.springframework.context.annotation.Configuration;
34-
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
35-
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
33+
import org.springframework.context.annotation.Import;
3634
import org.springframework.jdbc.support.JdbcTransactionManager;
3735

3836
/**
@@ -43,6 +41,7 @@
4341
*/
4442
@Configuration
4543
@EnableBatchProcessing
44+
@Import(DataSourceConfiguration.class)
4645
public class AmqpJobConfiguration {
4746

4847
@Bean
@@ -60,20 +59,6 @@ public Step step(JobRepository jobRepository, JdbcTransactionManager transaction
6059
.build();
6160
}
6261

63-
@Bean
64-
public DataSource dataSource() {
65-
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
66-
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
67-
.addScript("/business-schema-hsqldb.sql")
68-
.generateUniqueName(true)
69-
.build();
70-
}
71-
72-
@Bean
73-
public JdbcTransactionManager transactionManager(DataSource dataSource) {
74-
return new JdbcTransactionManager(dataSource);
75-
}
76-
7762
/**
7863
* Reads from the designated queue.
7964
* @param rabbitInputTemplate the template to be used by the {@link ItemReader}.

spring-batch-samples/src/main/java/org/springframework/batch/samples/chunking/ManagerConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.batch.integration.chunk.RemoteChunkingManagerStepBuilderFactory;
2929
import org.springframework.batch.integration.config.annotation.EnableBatchIntegration;
3030
import org.springframework.batch.item.support.ListItemReader;
31+
import org.springframework.batch.samples.common.DataSourceConfiguration;
3132
import org.springframework.beans.factory.annotation.Autowired;
3233
import org.springframework.beans.factory.annotation.Value;
3334
import org.springframework.context.annotation.Bean;

spring-batch-samples/src/main/java/org/springframework/batch/samples/chunking/WorkerConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.batch.integration.config.annotation.EnableBatchIntegration;
2424
import org.springframework.batch.item.ItemProcessor;
2525
import org.springframework.batch.item.ItemWriter;
26+
import org.springframework.batch.samples.common.DataSourceConfiguration;
2627
import org.springframework.beans.factory.annotation.Autowired;
2728
import org.springframework.beans.factory.annotation.Value;
2829
import org.springframework.context.annotation.Bean;
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2022 the original author or authors.
2+
* Copyright 2021-2023 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.
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.batch.samples.chunking;
17+
package org.springframework.batch.samples.common;
1818

1919
import javax.sql.DataSource;
2020

@@ -33,6 +33,7 @@ public class DataSourceConfiguration {
3333
public DataSource dataSource() {
3434
return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
3535
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
36+
.addScript("/org/springframework/batch/samples/common/business-schema-hsqldb.sql")
3637
.generateUniqueName(true)
3738
.build();
3839
}

spring-batch-samples/src/main/java/org/springframework/batch/samples/file/delimited/DelimitedJobConfiguration.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.springframework.batch.samples.file.delimited;
22

3-
import javax.sql.DataSource;
4-
53
import org.springframework.batch.core.Job;
64
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
75
import org.springframework.batch.core.configuration.annotation.StepScope;
@@ -14,19 +12,20 @@
1412
import org.springframework.batch.item.file.FlatFileItemWriter;
1513
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
1614
import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder;
15+
import org.springframework.batch.samples.common.DataSourceConfiguration;
1716
import org.springframework.batch.samples.domain.trade.CustomerCredit;
1817
import org.springframework.batch.samples.domain.trade.internal.CustomerCreditIncreaseProcessor;
1918
import org.springframework.beans.factory.annotation.Value;
2019
import org.springframework.context.annotation.Bean;
2120
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.context.annotation.Import;
2222
import org.springframework.core.io.Resource;
2323
import org.springframework.core.io.WritableResource;
24-
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
25-
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
2624
import org.springframework.jdbc.support.JdbcTransactionManager;
2725

2826
@Configuration
2927
@EnableBatchProcessing
28+
@Import(DataSourceConfiguration.class)
3029
public class DelimitedJobConfiguration {
3130

3231
@Bean
@@ -63,20 +62,4 @@ public Job job(JobRepository jobRepository, JdbcTransactionManager transactionMa
6362
.build();
6463
}
6564

66-
// Infrastructure beans
67-
68-
@Bean
69-
public DataSource dataSource() {
70-
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
71-
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
72-
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
73-
.generateUniqueName(true)
74-
.build();
75-
}
76-
77-
@Bean
78-
public JdbcTransactionManager transactionManager(DataSource dataSource) {
79-
return new JdbcTransactionManager(dataSource);
80-
}
81-
8265
}

spring-batch-samples/src/main/java/org/springframework/batch/samples/file/fixed/FixedLengthJobConfiguration.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.springframework.batch.samples.file.fixed;
22

3-
import javax.sql.DataSource;
4-
53
import org.springframework.batch.core.Job;
64
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
75
import org.springframework.batch.core.configuration.annotation.StepScope;
@@ -15,19 +13,20 @@
1513
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
1614
import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder;
1715
import org.springframework.batch.item.file.transform.Range;
16+
import org.springframework.batch.samples.common.DataSourceConfiguration;
1817
import org.springframework.batch.samples.domain.trade.CustomerCredit;
1918
import org.springframework.batch.samples.domain.trade.internal.CustomerCreditIncreaseProcessor;
2019
import org.springframework.beans.factory.annotation.Value;
2120
import org.springframework.context.annotation.Bean;
2221
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.context.annotation.Import;
2323
import org.springframework.core.io.Resource;
2424
import org.springframework.core.io.WritableResource;
25-
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
26-
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
2725
import org.springframework.jdbc.support.JdbcTransactionManager;
2826

2927
@Configuration
3028
@EnableBatchProcessing
29+
@Import(DataSourceConfiguration.class)
3130
public class FixedLengthJobConfiguration {
3231

3332
@Bean
@@ -66,20 +65,4 @@ public Job job(JobRepository jobRepository, JdbcTransactionManager transactionMa
6665
.build();
6766
}
6867

69-
// Infrastructure beans
70-
71-
@Bean
72-
public DataSource dataSource() {
73-
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
74-
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
75-
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
76-
.generateUniqueName(true)
77-
.build();
78-
}
79-
80-
@Bean
81-
public JdbcTransactionManager transactionManager(DataSource dataSource) {
82-
return new JdbcTransactionManager(dataSource);
83-
}
84-
8568
}

spring-batch-samples/src/main/java/org/springframework/batch/samples/file/json/JsonJobConfiguration.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.batch.samples.file.json;
1717

18-
import javax.sql.DataSource;
19-
2018
import org.springframework.batch.core.Job;
2119
import org.springframework.batch.core.Step;
2220
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
@@ -30,20 +28,22 @@
3028
import org.springframework.batch.item.json.JsonItemReader;
3129
import org.springframework.batch.item.json.builder.JsonFileItemWriterBuilder;
3230
import org.springframework.batch.item.json.builder.JsonItemReaderBuilder;
31+
import org.springframework.batch.samples.common.DataSourceConfiguration;
3332
import org.springframework.batch.samples.domain.trade.Trade;
3433
import org.springframework.beans.factory.annotation.Value;
3534
import org.springframework.context.annotation.Bean;
3635
import org.springframework.context.annotation.Configuration;
36+
import org.springframework.context.annotation.Import;
3737
import org.springframework.core.io.Resource;
3838
import org.springframework.core.io.WritableResource;
39-
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
4039
import org.springframework.jdbc.support.JdbcTransactionManager;
4140

4241
/**
4342
* @author Mahmoud Ben Hassine
4443
*/
4544
@Configuration
4645
@EnableBatchProcessing
46+
@Import(DataSourceConfiguration.class)
4747
public class JsonJobConfiguration {
4848

4949
@Bean
@@ -80,19 +80,4 @@ public Job job(JobRepository jobRepository, Step step) {
8080
return new JobBuilder("job", jobRepository).start(step).build();
8181
}
8282

83-
// Infrastructure beans
84-
85-
@Bean
86-
public DataSource dataSource() {
87-
return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
88-
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
89-
.generateUniqueName(true)
90-
.build();
91-
}
92-
93-
@Bean
94-
public JdbcTransactionManager transactionManager(DataSource dataSource) {
95-
return new JdbcTransactionManager(dataSource);
96-
}
97-
9883
}

spring-batch-samples/src/main/java/org/springframework/batch/samples/file/multiline/MultiLineJobConfiguration.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.springframework.batch.samples.file.multiline;
22

3-
import javax.sql.DataSource;
4-
53
import org.springframework.batch.core.Job;
64
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
75
import org.springframework.batch.core.configuration.annotation.StepScope;
@@ -16,17 +14,18 @@
1614
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
1715
import org.springframework.batch.item.file.transform.FieldSet;
1816
import org.springframework.batch.item.file.transform.PassThroughLineAggregator;
17+
import org.springframework.batch.samples.common.DataSourceConfiguration;
1918
import org.springframework.beans.factory.annotation.Value;
2019
import org.springframework.context.annotation.Bean;
2120
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.context.annotation.Import;
2222
import org.springframework.core.io.Resource;
2323
import org.springframework.core.io.WritableResource;
24-
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
25-
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
2624
import org.springframework.jdbc.support.JdbcTransactionManager;
2725

2826
@Configuration
2927
@EnableBatchProcessing
28+
@Import(DataSourceConfiguration.class)
3029
public class MultiLineJobConfiguration {
3130

3231
@Bean
@@ -65,20 +64,4 @@ public Job job(JobRepository jobRepository, JdbcTransactionManager transactionMa
6564
.build();
6665
}
6766

68-
// Infrastructure beans
69-
70-
@Bean
71-
public DataSource dataSource() {
72-
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
73-
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
74-
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
75-
.generateUniqueName(true)
76-
.build();
77-
}
78-
79-
@Bean
80-
public JdbcTransactionManager transactionManager(DataSource dataSource) {
81-
return new JdbcTransactionManager(dataSource);
82-
}
83-
8467
}

spring-batch-samples/src/main/java/org/springframework/batch/samples/file/multirecordtype/MultiRecordTypeJobConfiguration.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
import java.util.Map;
1919

20-
import javax.sql.DataSource;
21-
2220
import org.springframework.batch.core.Job;
2321
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
2422
import org.springframework.batch.core.configuration.annotation.StepScope;
@@ -34,24 +32,25 @@
3432
import org.springframework.batch.item.file.transform.FixedLengthTokenizer;
3533
import org.springframework.batch.item.file.transform.FormatterLineAggregator;
3634
import org.springframework.batch.item.file.transform.Range;
35+
import org.springframework.batch.samples.common.DataSourceConfiguration;
3736
import org.springframework.batch.samples.domain.trade.CustomerCredit;
3837
import org.springframework.batch.samples.domain.trade.Trade;
3938
import org.springframework.batch.samples.domain.trade.internal.CustomerCreditFieldSetMapper;
4039
import org.springframework.batch.samples.domain.trade.internal.TradeFieldSetMapper;
4140
import org.springframework.beans.factory.annotation.Value;
4241
import org.springframework.context.annotation.Bean;
4342
import org.springframework.context.annotation.Configuration;
43+
import org.springframework.context.annotation.Import;
4444
import org.springframework.core.io.Resource;
4545
import org.springframework.core.io.WritableResource;
46-
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
47-
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
4846
import org.springframework.jdbc.support.JdbcTransactionManager;
4947

5048
/**
5149
* @author Mahmoud Ben Hassine
5250
*/
5351
@Configuration
5452
@EnableBatchProcessing
53+
@Import(DataSourceConfiguration.class)
5554
public class MultiRecordTypeJobConfiguration {
5655

5756
@Bean
@@ -137,20 +136,4 @@ public Job job(JobRepository jobRepository, JdbcTransactionManager transactionMa
137136
.build();
138137
}
139138

140-
// Infrastructure beans
141-
142-
@Bean
143-
public DataSource dataSource() {
144-
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
145-
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
146-
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
147-
.generateUniqueName(true)
148-
.build();
149-
}
150-
151-
@Bean
152-
public JdbcTransactionManager transactionManager(DataSource dataSource) {
153-
return new JdbcTransactionManager(dataSource);
154-
}
155-
156139
}

0 commit comments

Comments
 (0)