Skip to content

Commit b3d73a1

Browse files
committed
Merge pull request #25316 from chaundhyan
* pr/25316: Polish "Group jdbc-related batch properties beneath spring.batch.jdbc" Group jdbc-related batch properties beneath spring.batch.jdbc Closes gh-25316
2 parents 9a3889b + 9bc4f8e commit b3d73a1

File tree

7 files changed

+175
-63
lines changed

7 files changed

+175
-63
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-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.
@@ -111,7 +111,7 @@ protected JobExplorer createJobExplorer() throws Exception {
111111
PropertyMapper map = PropertyMapper.get();
112112
JobExplorerFactoryBean factory = new JobExplorerFactoryBean();
113113
factory.setDataSource(this.dataSource);
114-
map.from(this.properties::getTablePrefix).whenHasText().to(factory::setTablePrefix);
114+
map.from(this.properties.getJdbc()::getTablePrefix).whenHasText().to(factory::setTablePrefix);
115115
factory.afterPropertiesSet();
116116
return factory.getObject();
117117
}
@@ -128,7 +128,7 @@ protected JobRepository createJobRepository() throws Exception {
128128
PropertyMapper map = PropertyMapper.get();
129129
map.from(this.dataSource).to(factory::setDataSource);
130130
map.from(this::determineIsolationLevel).whenNonNull().to(factory::setIsolationLevelForCreate);
131-
map.from(this.properties::getTablePrefix).whenHasText().to(factory::setTablePrefix);
131+
map.from(this.properties.getJdbc()::getTablePrefix).whenHasText().to(factory::setTablePrefix);
132132
map.from(this::getTransactionManager).to(factory::setTransactionManager);
133133
factory.afterPropertiesSet();
134134
return factory.getObject();

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

Lines changed: 5 additions & 4 deletions
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;
@@ -32,23 +33,23 @@
3233
*/
3334
public class BatchDataSourceInitializer extends AbstractDataSourceInitializer {
3435

35-
private final BatchProperties properties;
36+
private final Jdbc jdbcProperties;
3637

3738
public BatchDataSourceInitializer(DataSource dataSource, ResourceLoader resourceLoader,
3839
BatchProperties properties) {
3940
super(dataSource, resourceLoader);
4041
Assert.notNull(properties, "BatchProperties must not be null");
41-
this.properties = properties;
42+
this.jdbcProperties = properties.getJdbc();
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
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-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.
@@ -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,72 @@
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;
35+
private final Job job = new Job();
4036

41-
/**
42-
* Table prefix for all the batch meta-data tables.
43-
*/
44-
private String tablePrefix;
37+
private final Jdbc jdbc = new Jdbc();
4538

4639
/**
47-
* Database schema initialization mode.
40+
* Return the datasource schema.
41+
* @return the schema
42+
* @deprecated as of 2.5.0 in favor of {@link Jdbc#getSchema()}
4843
*/
49-
private DataSourceInitializationMode initializeSchema = DataSourceInitializationMode.EMBEDDED;
50-
51-
private final Job job = new Job();
52-
44+
@Deprecated
45+
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc.schema")
5346
public String getSchema() {
54-
return this.schema;
47+
return this.jdbc.getSchema();
5548
}
5649

50+
@Deprecated
5751
public void setSchema(String schema) {
58-
this.schema = schema;
52+
this.jdbc.setSchema(schema);
5953
}
6054

55+
/**
56+
* Return the table prefix.
57+
* @return the table prefix
58+
* @deprecated as of 2.5.0 in favor of {@link Jdbc#getTablePrefix()}
59+
*/
60+
@Deprecated
61+
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc.table-prefix")
6162
public String getTablePrefix() {
62-
return this.tablePrefix;
63+
return this.jdbc.getTablePrefix();
6364
}
6465

66+
@Deprecated
6567
public void setTablePrefix(String tablePrefix) {
66-
this.tablePrefix = tablePrefix;
68+
this.jdbc.setTablePrefix(tablePrefix);
6769
}
6870

71+
/**
72+
* Return whether the schema should be initialized.
73+
* @return the initialization mode
74+
* @deprecated as of 2.5.0 in favor of {@link Jdbc#getInitializeSchema()}
75+
*/
76+
@Deprecated
77+
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc.initialize-schema")
6978
public DataSourceInitializationMode getInitializeSchema() {
70-
return this.initializeSchema;
79+
return this.jdbc.getInitializeSchema();
7180
}
7281

82+
@Deprecated
7383
public void setInitializeSchema(DataSourceInitializationMode initializeSchema) {
74-
this.initializeSchema = initializeSchema;
84+
this.jdbc.setInitializeSchema(initializeSchema);
7585
}
7686

7787
public Job getJob() {
7888
return this.job;
7989
}
8090

91+
public Jdbc getJdbc() {
92+
return this.jdbc;
93+
}
94+
8195
public static class Job {
8296

8397
/**
@@ -96,4 +110,50 @@ public void setNames(String names) {
96110

97111
}
98112

113+
public static class Jdbc {
114+
115+
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
116+
+ "batch/core/schema-@@platform@@.sql";
117+
118+
/**
119+
* Path to the SQL file to use to initialize the database schema.
120+
*/
121+
private String schema = DEFAULT_SCHEMA_LOCATION;
122+
123+
/**
124+
* Table prefix for all the batch meta-data tables.
125+
*/
126+
private String tablePrefix;
127+
128+
/**
129+
* Database schema initialization mode.
130+
*/
131+
private DataSourceInitializationMode initializeSchema = DataSourceInitializationMode.EMBEDDED;
132+
133+
public String getSchema() {
134+
return this.schema;
135+
}
136+
137+
public void setSchema(String schema) {
138+
this.schema = schema;
139+
}
140+
141+
public String getTablePrefix() {
142+
return this.tablePrefix;
143+
}
144+
145+
public void setTablePrefix(String tablePrefix) {
146+
this.tablePrefix = tablePrefix;
147+
}
148+
149+
public DataSourceInitializationMode getInitializeSchema() {
150+
return this.initializeSchema;
151+
}
152+
153+
public void setInitializeSchema(DataSourceInitializationMode initializeSchema) {
154+
this.initializeSchema = initializeSchema;
155+
}
156+
157+
}
158+
99159
}

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,14 @@
376376
"type": "java.lang.Boolean",
377377
"description": "Create the required batch tables on startup if necessary. Enabled automatically\n if no custom table prefix is set or if a custom schema is configured.",
378378
"deprecation": {
379-
"replacement": "spring.batch.initialize-schema",
379+
"replacement": "spring.batch.jdbc.initialize-schema",
380380
"level": "error"
381381
}
382382
},
383+
{
384+
"name": "spring.batch.jdbc.initialize-schema",
385+
"defaultValue": "embedded"
386+
},
383387
{
384388
"name": "spring.batch.job.enabled",
385389
"type": "java.lang.Boolean",

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

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-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.
@@ -49,7 +49,9 @@
4949
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
5050
import org.springframework.boot.jdbc.DataSourceBuilder;
5151
import org.springframework.boot.jdbc.DataSourceInitializationMode;
52+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
5253
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
54+
import org.springframework.boot.test.context.runner.ContextConsumer;
5355
import org.springframework.context.annotation.Bean;
5456
import org.springframework.context.annotation.Configuration;
5557
import org.springframework.context.annotation.Primary;
@@ -82,7 +84,7 @@ void testDefaultContext() {
8284
.run((context) -> {
8385
assertThat(context).hasSingleBean(JobLauncher.class);
8486
assertThat(context).hasSingleBean(JobExplorer.class);
85-
assertThat(context.getBean(BatchProperties.class).getInitializeSchema())
87+
assertThat(context.getBean(BatchProperties.class).getJdbc().getInitializeSchema())
8688
.isEqualTo(DataSourceInitializationMode.EMBEDDED);
8789
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
8890
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
@@ -174,17 +176,30 @@ void testDisableLaunchesJob() {
174176

175177
@Test
176178
void testDisableSchemaLoader() {
179+
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
180+
.withPropertyValues("spring.datasource.generate-unique-name=true",
181+
"spring.batch.jdbc.initialize-schema:never")
182+
.run(assertDatasourceIsNotInitialized());
183+
}
184+
185+
@Test
186+
@Deprecated
187+
void testDisableSchemaLoaderWithDeprecatedProperty() {
177188
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
178189
.withPropertyValues("spring.datasource.generate-unique-name=true",
179190
"spring.batch.initialize-schema:never")
180-
.run((context) -> {
181-
assertThat(context).hasSingleBean(JobLauncher.class);
182-
assertThat(context.getBean(BatchProperties.class).getInitializeSchema())
183-
.isEqualTo(DataSourceInitializationMode.NEVER);
184-
assertThatExceptionOfType(BadSqlGrammarException.class)
185-
.isThrownBy(() -> new JdbcTemplate(context.getBean(DataSource.class))
186-
.queryForList("select * from BATCH_JOB_EXECUTION"));
187-
});
191+
.run(assertDatasourceIsNotInitialized());
192+
}
193+
194+
private ContextConsumer<AssertableApplicationContext> assertDatasourceIsNotInitialized() {
195+
return (context) -> {
196+
assertThat(context).hasSingleBean(JobLauncher.class);
197+
assertThat(context.getBean(BatchProperties.class).getJdbc().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+
};
188203
}
189204

190205
@Test
@@ -205,23 +220,39 @@ void testUsingJpa() {
205220

206221
@Test
207222
void testRenamePrefix() {
223+
this.contextRunner
224+
.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class,
225+
HibernateJpaAutoConfiguration.class)
226+
.withPropertyValues("spring.datasource.generate-unique-name=true",
227+
"spring.batch.jdbc.schema:classpath:batch/custom-schema-hsql.sql",
228+
"spring.batch.jdbc.tablePrefix:PREFIX_")
229+
.run(assertCustomTablePrefix());
230+
}
231+
232+
@Test
233+
@Deprecated
234+
void testRenamePrefixWithDeprecatedProperty() {
208235
this.contextRunner
209236
.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class,
210237
HibernateJpaAutoConfiguration.class)
211238
.withPropertyValues("spring.datasource.generate-unique-name=true",
212239
"spring.batch.schema:classpath:batch/custom-schema-hsql.sql",
213240
"spring.batch.tablePrefix:PREFIX_")
214-
.run((context) -> {
215-
assertThat(context).hasSingleBean(JobLauncher.class);
216-
assertThat(context.getBean(BatchProperties.class).getInitializeSchema())
217-
.isEqualTo(DataSourceInitializationMode.EMBEDDED);
218-
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
219-
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
220-
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
221-
assertThat(jobExplorer.findRunningJobExecutions("test")).isEmpty();
222-
JobRepository jobRepository = context.getBean(JobRepository.class);
223-
assertThat(jobRepository.getLastJobExecution("test", new JobParameters())).isNull();
224-
});
241+
.run(assertCustomTablePrefix());
242+
}
243+
244+
private ContextConsumer<AssertableApplicationContext> assertCustomTablePrefix() {
245+
return (context) -> {
246+
assertThat(context).hasSingleBean(JobLauncher.class);
247+
assertThat(context.getBean(BatchProperties.class).getJdbc().getInitializeSchema())
248+
.isEqualTo(DataSourceInitializationMode.EMBEDDED);
249+
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
250+
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
251+
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
252+
assertThat(jobExplorer.findRunningJobExecutions("test")).isEmpty();
253+
JobRepository jobRepository = context.getBean(JobRepository.class);
254+
assertThat(jobRepository.getLastJobExecution("test", new JobParameters())).isNull();
255+
};
225256
}
226257

227258
@Test

0 commit comments

Comments
 (0)