Skip to content

Commit d093807

Browse files
chaundhyansnicoll
authored andcommitted
Group jdbc-related batch properties beneath spring.batch.jdbc
See gh-25316
1 parent 9a3889b commit d093807

File tree

7 files changed

+136
-35
lines changed

7 files changed

+136
-35
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @author Andy Wilkinson
3939
* @author Kazuki Shimizu
4040
* @author Stephane Nicoll
41+
* @author Mukul Kumar Chaundhyan
4142
* @since 1.0.0
4243
*/
4344
public class BasicBatchConfigurer implements BatchConfigurer, InitializingBean {
@@ -111,7 +112,7 @@ protected JobExplorer createJobExplorer() throws Exception {
111112
PropertyMapper map = PropertyMapper.get();
112113
JobExplorerFactoryBean factory = new JobExplorerFactoryBean();
113114
factory.setDataSource(this.dataSource);
114-
map.from(this.properties::getTablePrefix).whenHasText().to(factory::setTablePrefix);
115+
map.from(this.properties.getJdbc()::getTablePrefix).whenHasText().to(factory::setTablePrefix);
115116
factory.afterPropertiesSet();
116117
return factory.getObject();
117118
}
@@ -128,7 +129,7 @@ protected JobRepository createJobRepository() throws Exception {
128129
PropertyMapper map = PropertyMapper.get();
129130
map.from(this.dataSource).to(factory::setDataSource);
130131
map.from(this::determineIsolationLevel).whenNonNull().to(factory::setIsolationLevelForCreate);
131-
map.from(this.properties::getTablePrefix).whenHasText().to(factory::setTablePrefix);
132+
map.from(this.properties.getJdbc()::getTablePrefix).whenHasText().to(factory::setTablePrefix);
132133
map.from(this::getTransactionManager).to(factory::setTransactionManager);
133134
factory.afterPropertiesSet();
134135
return factory.getObject();

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* @author Eddú Meléndez
5858
* @author Kazuki Shimizu
5959
* @author Mahmoud Ben Hassine
60+
* @author Mukul Kumar Chaundhyan
6061
* @since 1.0.0
6162
*/
6263
@Configuration(proxyBeanMethods = false)
@@ -107,11 +108,13 @@ static class DataSourceInitializerConfiguration {
107108

108109
@Bean
109110
@ConditionalOnMissingBean
111+
@ConditionalOnProperty(prefix = "spring.batch.jdbc", name = "enabled", havingValue = "true",
112+
matchIfMissing = true)
110113
BatchDataSourceInitializer batchDataSourceInitializer(DataSource dataSource,
111114
@BatchDataSource ObjectProvider<DataSource> batchDataSource, ResourceLoader resourceLoader,
112115
BatchProperties properties) {
113116
return new BatchDataSourceInitializer(batchDataSource.getIfAvailable(() -> dataSource), resourceLoader,
114-
properties);
117+
properties.getJdbc());
115118
}
116119

117120
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializer.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import javax.sql.DataSource;
2020

21+
import org.springframework.boot.autoconfigure.batch.BatchProperties.Jdbc;
2122
import org.springframework.boot.jdbc.AbstractDataSourceInitializer;
2223
import org.springframework.boot.jdbc.DataSourceInitializationMode;
2324
import org.springframework.core.io.ResourceLoader;
@@ -28,27 +29,27 @@
2829
*
2930
* @author Dave Syer
3031
* @author Vedran Pavic
32+
* @author Mukul Kumar Chaundhyan
3133
* @since 1.0.0
3234
*/
3335
public class BatchDataSourceInitializer extends AbstractDataSourceInitializer {
3436

35-
private final BatchProperties properties;
37+
private final Jdbc jdbcProperties;
3638

37-
public BatchDataSourceInitializer(DataSource dataSource, ResourceLoader resourceLoader,
38-
BatchProperties properties) {
39+
public BatchDataSourceInitializer(DataSource dataSource, ResourceLoader resourceLoader, Jdbc jdbcProperties) {
3940
super(dataSource, resourceLoader);
40-
Assert.notNull(properties, "BatchProperties must not be null");
41-
this.properties = properties;
41+
Assert.notNull(jdbcProperties, "Jdbc Batch Properties must not be null");
42+
this.jdbcProperties = jdbcProperties;
4243
}
4344

4445
@Override
4546
protected DataSourceInitializationMode getMode() {
46-
return this.properties.getInitializeSchema();
47+
return this.jdbcProperties.getInitializeSchema();
4748
}
4849

4950
@Override
5051
protected String getSchemaLocation() {
51-
return this.properties.getSchema();
52+
return this.jdbcProperties.getSchema();
5253
}
5354

5455
@Override

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java

+69-24
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.autoconfigure.batch;
1818

1919
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
2021
import org.springframework.boot.jdbc.DataSourceInitializationMode;
2122

2223
/**
@@ -25,59 +26,51 @@
2526
* @author Stephane Nicoll
2627
* @author Eddú Meléndez
2728
* @author Vedran Pavic
29+
* @author Mukul Kumar Chaundhyan
2830
* @since 1.2.0
2931
*/
3032
@ConfigurationProperties(prefix = "spring.batch")
3133
public class BatchProperties {
3234

33-
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
34-
+ "batch/core/schema-@@platform@@.sql";
35-
36-
/**
37-
* Path to the SQL file to use to initialize the database schema.
38-
*/
39-
private String schema = DEFAULT_SCHEMA_LOCATION;
40-
41-
/**
42-
* Table prefix for all the batch meta-data tables.
43-
*/
44-
private String tablePrefix;
45-
46-
/**
47-
* Database schema initialization mode.
48-
*/
49-
private DataSourceInitializationMode initializeSchema = DataSourceInitializationMode.EMBEDDED;
50-
5135
private final Job job = new Job();
5236

37+
private final Jdbc jdbc = new Jdbc();
38+
39+
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc")
5340
public String getSchema() {
54-
return this.schema;
41+
return this.jdbc.getSchema();
5542
}
5643

5744
public void setSchema(String schema) {
58-
this.schema = schema;
45+
this.jdbc.setSchema(schema);
5946
}
6047

48+
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc")
6149
public String getTablePrefix() {
62-
return this.tablePrefix;
50+
return this.jdbc.getTablePrefix();
6351
}
6452

6553
public void setTablePrefix(String tablePrefix) {
66-
this.tablePrefix = tablePrefix;
54+
this.jdbc.setTablePrefix(tablePrefix);
6755
}
6856

57+
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc")
6958
public DataSourceInitializationMode getInitializeSchema() {
70-
return this.initializeSchema;
59+
return this.jdbc.getInitializeSchema();
7160
}
7261

7362
public void setInitializeSchema(DataSourceInitializationMode initializeSchema) {
74-
this.initializeSchema = initializeSchema;
63+
this.jdbc.setInitializeSchema(initializeSchema);
7564
}
7665

7766
public Job getJob() {
7867
return this.job;
7968
}
8069

70+
public Jdbc getJdbc() {
71+
return this.jdbc;
72+
}
73+
8174
public static class Job {
8275

8376
/**
@@ -96,4 +89,56 @@ public void setNames(String names) {
9689

9790
}
9891

92+
/**
93+
* JDBC configuration properties for Spring Batch.
94+
*
95+
* @author Mukul Kumar Chaundhyan
96+
* @since 2.5.0
97+
*/
98+
public static class Jdbc {
99+
100+
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
101+
+ "batch/core/schema-@@platform@@.sql";
102+
103+
/**
104+
* Path to the SQL file to use to initialize the database schema.
105+
*/
106+
private String schema = DEFAULT_SCHEMA_LOCATION;
107+
108+
/**
109+
* Table prefix for all the batch meta-data tables.
110+
*/
111+
private String tablePrefix;
112+
113+
/**
114+
* Database schema initialization mode.
115+
*/
116+
private DataSourceInitializationMode initializeSchema = DataSourceInitializationMode.EMBEDDED;
117+
118+
public String getSchema() {
119+
return this.schema;
120+
}
121+
122+
public void setSchema(String schema) {
123+
this.schema = schema;
124+
}
125+
126+
public String getTablePrefix() {
127+
return this.tablePrefix;
128+
}
129+
130+
public void setTablePrefix(String tablePrefix) {
131+
this.tablePrefix = tablePrefix;
132+
}
133+
134+
public DataSourceInitializationMode getInitializeSchema() {
135+
return this.initializeSchema;
136+
}
137+
138+
public void setInitializeSchema(DataSourceInitializationMode initializeSchema) {
139+
this.initializeSchema = initializeSchema;
140+
}
141+
142+
}
143+
99144
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java

+36
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ void testDisableSchemaLoader() {
187187
});
188188
}
189189

190+
@Test
191+
void testDisableSchemaLoaderWithNewJdbcProperties() {
192+
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
193+
.withPropertyValues("spring.datasource.generate-unique-name=true",
194+
"spring.batch.jdbc.initialize-schema:never")
195+
.run((context) -> {
196+
assertThat(context).hasSingleBean(JobLauncher.class);
197+
assertThat(context.getBean(BatchProperties.class).getInitializeSchema())
198+
.isEqualTo(DataSourceInitializationMode.NEVER);
199+
assertThatExceptionOfType(BadSqlGrammarException.class)
200+
.isThrownBy(() -> new JdbcTemplate(context.getBean(DataSource.class))
201+
.queryForList("select * from BATCH_JOB_EXECUTION"));
202+
});
203+
}
204+
190205
@Test
191206
void testUsingJpa() {
192207
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class,
@@ -224,6 +239,27 @@ void testRenamePrefix() {
224239
});
225240
}
226241

242+
@Test
243+
void testRenamePrefixWithNewJdbcProperties() {
244+
this.contextRunner
245+
.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class,
246+
HibernateJpaAutoConfiguration.class)
247+
.withPropertyValues("spring.datasource.generate-unique-name=true",
248+
"spring.batch.jdbc.schema:classpath:batch/custom-schema-hsql.sql",
249+
"spring.batch.jdbc.tablePrefix:PREFIX_")
250+
.run((context) -> {
251+
assertThat(context).hasSingleBean(JobLauncher.class);
252+
assertThat(context.getBean(BatchProperties.class).getInitializeSchema())
253+
.isEqualTo(DataSourceInitializationMode.EMBEDDED);
254+
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
255+
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
256+
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
257+
assertThat(jobExplorer.findRunningJobExecutions("test")).isEmpty();
258+
JobRepository jobRepository = context.getBean(JobRepository.class);
259+
assertThat(jobRepository.getLastJobExecution("test", new JobParameters())).isNull();
260+
});
261+
}
262+
227263
@Test
228264
void testCustomizeJpaTransactionManagerUsingProperties() {
229265
this.contextRunner

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationWithoutJpaTests.java

+15
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ void jdbcWithCustomPrefix() {
8484
});
8585
}
8686

87+
@Test
88+
void jdbcWithCustomPrefixWithNewJdbcProperties() {
89+
this.contextRunner.withUserConfiguration(DefaultConfiguration.class, EmbeddedDataSourceConfiguration.class)
90+
.withPropertyValues("spring.datasource.generate-unique-name=true",
91+
"spring.batch.jdbc.schema:classpath:batch/custom-schema-hsql.sql",
92+
"spring.batch.jdbc.tablePrefix:PREFIX_")
93+
.run((context) -> {
94+
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
95+
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
96+
assertThat(context.getBean(JobExplorer.class).findRunningJobExecutions("test")).isEmpty();
97+
assertThat(context.getBean(JobRepository.class).getLastJobExecution("test", new JobParameters()))
98+
.isNull();
99+
});
100+
}
101+
87102
@EnableBatchProcessing
88103
@TestAutoConfigurationPackage(City.class)
89104
static class DefaultConfiguration {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunnerTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ protected BatchConfiguration(DataSource dataSource) {
228228

229229
@Bean
230230
BatchDataSourceInitializer batchDataSourceInitializer(ResourceLoader resourceLoader) {
231-
return new BatchDataSourceInitializer(this.dataSource, resourceLoader, new BatchProperties());
231+
return new BatchDataSourceInitializer(this.dataSource, resourceLoader, new BatchProperties().getJdbc());
232232
}
233233

234234
}

0 commit comments

Comments
 (0)