Skip to content

Commit 5b0d916

Browse files
committed
Merge pull request #6651 from vpavic:improve-batch-autoconfig
* pr/6651: Polish contribution Validate Spring Batch database initializer configuration
2 parents def40bf + ee668e6 commit 5b0d916

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 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.
@@ -23,6 +23,7 @@
2323
*
2424
* @author Stephane Nicoll
2525
* @author Eddú Meléndez
26+
* @author Vedran Pavic
2627
* @since 1.2.0
2728
*/
2829
@ConfigurationProperties("spring.batch")
@@ -69,15 +70,23 @@ public String getTablePrefix() {
6970
return this.tablePrefix;
7071
}
7172

72-
public static class Initializer {
73+
public class Initializer {
7374

7475
/**
75-
* Create the required batch tables on startup if necessary.
76+
* Create the required batch tables on startup if necessary. Enabled
77+
* automatically if no custom table prefix is set or if a custom schema is
78+
* configured.
7679
*/
77-
private boolean enabled = true;
80+
private Boolean enabled;
7881

7982
public boolean isEnabled() {
80-
return this.enabled;
83+
if (this.enabled != null) {
84+
return this.enabled;
85+
}
86+
boolean defaultTablePrefix = BatchProperties.this.getTablePrefix() == null;
87+
boolean customSchema = !DEFAULT_SCHEMA_LOCATION.equals(
88+
BatchProperties.this.getSchema());
89+
return (defaultTablePrefix || customSchema);
8190
}
8291

8392
public void setEnabled(boolean enabled) {

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
*
6868
* @author Dave Syer
6969
* @author Stephane Nicoll
70+
* @author Vedran Pavic
7071
*/
7172
public class BatchAutoConfigurationTests {
7273

@@ -91,6 +92,8 @@ public void testDefaultContext() throws Exception {
9192
this.context.refresh();
9293
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
9394
assertThat(this.context.getBean(JobExplorer.class)).isNotNull();
95+
assertThat(this.context.getBean(BatchProperties.class)
96+
.getInitializer().isEnabled()).isTrue();
9497
assertThat(new JdbcTemplate(this.context.getBean(DataSource.class))
9598
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
9699
}
@@ -190,6 +193,8 @@ public void testDisableSchemaLoader() throws Exception {
190193
PropertyPlaceholderAutoConfiguration.class);
191194
this.context.refresh();
192195
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
196+
assertThat(this.context.getBean(BatchProperties.class)
197+
.getInitializer().isEnabled()).isFalse();
193198
this.expected.expect(BadSqlGrammarException.class);
194199
new JdbcTemplate(this.context.getBean(DataSource.class))
195200
.queryForList("select * from BATCH_JOB_EXECUTION");
@@ -228,6 +233,8 @@ public void testRenamePrefix() throws Exception {
228233
PropertyPlaceholderAutoConfiguration.class);
229234
this.context.refresh();
230235
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
236+
assertThat(this.context.getBean(BatchProperties.class)
237+
.getInitializer().isEnabled()).isTrue();
231238
assertThat(new JdbcTemplate(this.context.getBean(DataSource.class))
232239
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
233240
JobExplorer jobExplorer = this.context.getBean(JobExplorer.class);
@@ -237,6 +244,25 @@ public void testRenamePrefix() throws Exception {
237244
.isNull();
238245
}
239246

247+
@Test
248+
public void testCustomTablePrefixWithDefaultSchemaDisablesInitializer() throws Exception {
249+
this.context = new AnnotationConfigApplicationContext();
250+
EnvironmentTestUtils.addEnvironment(this.context,
251+
"spring.datasource.name:batchtest",
252+
"spring.batch.tablePrefix:PREFIX_");
253+
this.context.register(TestConfiguration.class,
254+
EmbeddedDataSourceConfiguration.class,
255+
HibernateJpaAutoConfiguration.class, BatchAutoConfiguration.class,
256+
PropertyPlaceholderAutoConfiguration.class);
257+
this.context.refresh();
258+
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
259+
assertThat(this.context.getBean(BatchProperties.class)
260+
.getInitializer().isEnabled()).isFalse();
261+
this.expected.expect(BadSqlGrammarException.class);
262+
new JdbcTemplate(this.context.getBean(DataSource.class))
263+
.queryForList("select * from BATCH_JOB_EXECUTION");
264+
}
265+
240266
@Configuration
241267
protected static class EmptyConfiguration {
242268
}

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ content into your application; rather pick only the properties that you need.
847847
spring.artemis.user= # Login user of the broker.
848848
849849
# SPRING BATCH ({sc-spring-boot-autoconfigure}/batch/BatchProperties.{sc-ext}[BatchProperties])
850-
spring.batch.initializer.enabled=true # Create the required batch tables on startup if necessary.
850+
spring.batch.initializer.enabled= # Create the required batch tables on startup if necessary. Enabled automatically if no custom table prefix is set or if a custom schema is configured.
851851
spring.batch.job.enabled=true # Execute all Spring Batch jobs in the context on startup.
852852
spring.batch.job.names= # Comma-separated list of job names to execute on startup (For instance `job1,job2`). By default, all Jobs found in the context are executed.
853853
spring.batch.schema=classpath:org/springframework/batch/core/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema.

0 commit comments

Comments
 (0)