Skip to content

Commit 9bc4f8e

Browse files
committed
Polish "Group jdbc-related batch properties beneath spring.batch.jdbc"
See gh-25316
1 parent d093807 commit 9bc4f8e

File tree

9 files changed

+104
-93
lines changed

9 files changed

+104
-93
lines changed

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

+1-2
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.
@@ -38,7 +38,6 @@
3838
* @author Andy Wilkinson
3939
* @author Kazuki Shimizu
4040
* @author Stephane Nicoll
41-
* @author Mukul Kumar Chaundhyan
4241
* @since 1.0.0
4342
*/
4443
public class BasicBatchConfigurer implements BatchConfigurer, InitializingBean {

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

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

109108
@Bean
110109
@ConditionalOnMissingBean
111-
@ConditionalOnProperty(prefix = "spring.batch.jdbc", name = "enabled", havingValue = "true",
112-
matchIfMissing = true)
113110
BatchDataSourceInitializer batchDataSourceInitializer(DataSource dataSource,
114111
@BatchDataSource ObjectProvider<DataSource> batchDataSource, ResourceLoader resourceLoader,
115112
BatchProperties properties) {
116113
return new BatchDataSourceInitializer(batchDataSource.getIfAvailable(() -> dataSource), resourceLoader,
117-
properties.getJdbc());
114+
properties);
118115
}
119116

120117
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@
2929
*
3030
* @author Dave Syer
3131
* @author Vedran Pavic
32-
* @author Mukul Kumar Chaundhyan
3332
* @since 1.0.0
3433
*/
3534
public class BatchDataSourceInitializer extends AbstractDataSourceInitializer {
3635

3736
private final Jdbc jdbcProperties;
3837

39-
public BatchDataSourceInitializer(DataSource dataSource, ResourceLoader resourceLoader, Jdbc jdbcProperties) {
38+
public BatchDataSourceInitializer(DataSource dataSource, ResourceLoader resourceLoader,
39+
BatchProperties properties) {
4040
super(dataSource, resourceLoader);
41-
Assert.notNull(jdbcProperties, "Jdbc Batch Properties must not be null");
42-
this.jdbcProperties = jdbcProperties;
41+
Assert.notNull(properties, "BatchProperties must not be null");
42+
this.jdbcProperties = properties.getJdbc();
4343
}
4444

4545
@Override

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

+25-10
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.
@@ -36,29 +36,50 @@ public class BatchProperties {
3636

3737
private final Jdbc jdbc = new Jdbc();
3838

39-
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc")
39+
/**
40+
* Return the datasource schema.
41+
* @return the schema
42+
* @deprecated as of 2.5.0 in favor of {@link Jdbc#getSchema()}
43+
*/
44+
@Deprecated
45+
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc.schema")
4046
public String getSchema() {
4147
return this.jdbc.getSchema();
4248
}
4349

50+
@Deprecated
4451
public void setSchema(String schema) {
4552
this.jdbc.setSchema(schema);
4653
}
4754

48-
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc")
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")
4962
public String getTablePrefix() {
5063
return this.jdbc.getTablePrefix();
5164
}
5265

66+
@Deprecated
5367
public void setTablePrefix(String tablePrefix) {
5468
this.jdbc.setTablePrefix(tablePrefix);
5569
}
5670

57-
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc")
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")
5878
public DataSourceInitializationMode getInitializeSchema() {
5979
return this.jdbc.getInitializeSchema();
6080
}
6181

82+
@Deprecated
6283
public void setInitializeSchema(DataSourceInitializationMode initializeSchema) {
6384
this.jdbc.setInitializeSchema(initializeSchema);
6485
}
@@ -89,12 +110,6 @@ public void setNames(String names) {
89110

90111
}
91112

92-
/**
93-
* JDBC configuration properties for Spring Batch.
94-
*
95-
* @author Mukul Kumar Chaundhyan
96-
* @since 2.5.0
97-
*/
98113
public static class Jdbc {
99114

100115
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"

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

+5-1
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

+43-48
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();
@@ -176,30 +178,28 @@ void testDisableLaunchesJob() {
176178
void testDisableSchemaLoader() {
177179
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
178180
.withPropertyValues("spring.datasource.generate-unique-name=true",
179-
"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-
});
181+
"spring.batch.jdbc.initialize-schema:never")
182+
.run(assertDatasourceIsNotInitialized());
188183
}
189184

190185
@Test
191-
void testDisableSchemaLoaderWithNewJdbcProperties() {
186+
@Deprecated
187+
void testDisableSchemaLoaderWithDeprecatedProperty() {
192188
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
193189
.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-
});
190+
"spring.batch.initialize-schema:never")
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+
};
203203
}
204204

205205
@Test
@@ -224,40 +224,35 @@ void testRenamePrefix() {
224224
.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class,
225225
HibernateJpaAutoConfiguration.class)
226226
.withPropertyValues("spring.datasource.generate-unique-name=true",
227-
"spring.batch.schema:classpath:batch/custom-schema-hsql.sql",
228-
"spring.batch.tablePrefix:PREFIX_")
229-
.run((context) -> {
230-
assertThat(context).hasSingleBean(JobLauncher.class);
231-
assertThat(context.getBean(BatchProperties.class).getInitializeSchema())
232-
.isEqualTo(DataSourceInitializationMode.EMBEDDED);
233-
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
234-
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
235-
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
236-
assertThat(jobExplorer.findRunningJobExecutions("test")).isEmpty();
237-
JobRepository jobRepository = context.getBean(JobRepository.class);
238-
assertThat(jobRepository.getLastJobExecution("test", new JobParameters())).isNull();
239-
});
227+
"spring.batch.jdbc.schema:classpath:batch/custom-schema-hsql.sql",
228+
"spring.batch.jdbc.tablePrefix:PREFIX_")
229+
.run(assertCustomTablePrefix());
240230
}
241231

242232
@Test
243-
void testRenamePrefixWithNewJdbcProperties() {
233+
@Deprecated
234+
void testRenamePrefixWithDeprecatedProperty() {
244235
this.contextRunner
245236
.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class,
246237
HibernateJpaAutoConfiguration.class)
247238
.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-
});
239+
"spring.batch.schema:classpath:batch/custom-schema-hsql.sql",
240+
"spring.batch.tablePrefix:PREFIX_")
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+
};
261256
}
262257

263258
@Test

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

+21-21
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.
@@ -31,7 +31,9 @@
3131
import org.springframework.boot.autoconfigure.orm.jpa.test.City;
3232
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
3333
import org.springframework.boot.jdbc.DataSourceInitializationMode;
34+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
3435
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
36+
import org.springframework.boot.test.context.runner.ContextConsumer;
3537
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
3638
import org.springframework.jdbc.core.JdbcTemplate;
3739
import org.springframework.transaction.PlatformTransactionManager;
@@ -59,7 +61,7 @@ void jdbcWithDefaultSettings() {
5961
assertThat(context).hasSingleBean(PlatformTransactionManager.class);
6062
assertThat(context.getBean(PlatformTransactionManager.class).toString())
6163
.contains("DataSourceTransactionManager");
62-
assertThat(context.getBean(BatchProperties.class).getInitializeSchema())
64+
assertThat(context.getBean(BatchProperties.class).getJdbc().getInitializeSchema())
6365
.isEqualTo(DataSourceInitializationMode.EMBEDDED);
6466
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
6567
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
@@ -73,30 +75,28 @@ void jdbcWithDefaultSettings() {
7375
void jdbcWithCustomPrefix() {
7476
this.contextRunner.withUserConfiguration(DefaultConfiguration.class, EmbeddedDataSourceConfiguration.class)
7577
.withPropertyValues("spring.datasource.generate-unique-name=true",
76-
"spring.batch.schema:classpath:batch/custom-schema-hsql.sql",
77-
"spring.batch.tablePrefix:PREFIX_")
78-
.run((context) -> {
79-
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
80-
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
81-
assertThat(context.getBean(JobExplorer.class).findRunningJobExecutions("test")).isEmpty();
82-
assertThat(context.getBean(JobRepository.class).getLastJobExecution("test", new JobParameters()))
83-
.isNull();
84-
});
78+
"spring.batch.jdbc.schema:classpath:batch/custom-schema-hsql.sql",
79+
"spring.batch.jdbc.tablePrefix:PREFIX_")
80+
.run(assertCustomPrefix());
8581
}
8682

8783
@Test
88-
void jdbcWithCustomPrefixWithNewJdbcProperties() {
84+
@Deprecated
85+
void jdbcWithCustomPrefixWithDeprecatedProperties() {
8986
this.contextRunner.withUserConfiguration(DefaultConfiguration.class, EmbeddedDataSourceConfiguration.class)
9087
.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-
});
88+
"spring.batch.schema:classpath:batch/custom-schema-hsql.sql",
89+
"spring.batch.tablePrefix:PREFIX_")
90+
.run(assertCustomPrefix());
91+
}
92+
93+
private ContextConsumer<AssertableApplicationContext> assertCustomPrefix() {
94+
return (context) -> {
95+
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
96+
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
97+
assertThat(context.getBean(JobExplorer.class).findRunningJobExecutions("test")).isEmpty();
98+
assertThat(context.getBean(JobRepository.class).getLastJobExecution("test", new JobParameters())).isNull();
99+
};
100100
}
101101

102102
@EnableBatchProcessing

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().getJdbc());
231+
return new BatchDataSourceInitializer(this.dataSource, resourceLoader, new BatchProperties());
232232
}
233233

234234
}

spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc

+3-2
Original file line numberDiff line numberDiff line change
@@ -2087,10 +2087,11 @@ You can also enable it for any database type, as shown in the following example:
20872087
----
20882088
spring:
20892089
batch:
2090-
initialize-schema: "always"
2090+
jdbc:
2091+
initialize-schema: "always"
20912092
----
20922093

2093-
You can also switch off the initialization explicitly by setting `spring.batch.initialize-schema` to `never`.
2094+
You can also switch off the initialization explicitly by setting `spring.batch.jdbc.initialize-schema` to `never`.
20942095

20952096

20962097

0 commit comments

Comments
 (0)