Merge pull request #6651 from vpavic:improve-batch-autoconfig
* pr/6651: Polish contribution Validate Spring Batch database initializer configuration
This commit is contained in:
commit
5b0d916910
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -23,6 +23,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
|||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Eddú Meléndez
|
||||
* @author Vedran Pavic
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@ConfigurationProperties("spring.batch")
|
||||
|
|
@ -69,16 +70,24 @@ public class BatchProperties {
|
|||
return this.tablePrefix;
|
||||
}
|
||||
|
||||
public static class Initializer {
|
||||
public class Initializer {
|
||||
|
||||
/**
|
||||
* Create the required batch tables on startup if necessary.
|
||||
* 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.
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
private Boolean enabled;
|
||||
|
||||
public boolean isEnabled() {
|
||||
if (this.enabled != null) {
|
||||
return this.enabled;
|
||||
}
|
||||
boolean defaultTablePrefix = BatchProperties.this.getTablePrefix() == null;
|
||||
boolean customSchema = !DEFAULT_SCHEMA_LOCATION.equals(
|
||||
BatchProperties.this.getSchema());
|
||||
return (defaultTablePrefix || customSchema);
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
|
|
|
|||
|
|
@ -67,6 +67,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
*
|
||||
* @author Dave Syer
|
||||
* @author Stephane Nicoll
|
||||
* @author Vedran Pavic
|
||||
*/
|
||||
public class BatchAutoConfigurationTests {
|
||||
|
||||
|
|
@ -91,6 +92,8 @@ public class BatchAutoConfigurationTests {
|
|||
this.context.refresh();
|
||||
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
|
||||
assertThat(this.context.getBean(JobExplorer.class)).isNotNull();
|
||||
assertThat(this.context.getBean(BatchProperties.class)
|
||||
.getInitializer().isEnabled()).isTrue();
|
||||
assertThat(new JdbcTemplate(this.context.getBean(DataSource.class))
|
||||
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
|
||||
}
|
||||
|
|
@ -190,6 +193,8 @@ public class BatchAutoConfigurationTests {
|
|||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
|
||||
assertThat(this.context.getBean(BatchProperties.class)
|
||||
.getInitializer().isEnabled()).isFalse();
|
||||
this.expected.expect(BadSqlGrammarException.class);
|
||||
new JdbcTemplate(this.context.getBean(DataSource.class))
|
||||
.queryForList("select * from BATCH_JOB_EXECUTION");
|
||||
|
|
@ -228,6 +233,8 @@ public class BatchAutoConfigurationTests {
|
|||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
|
||||
assertThat(this.context.getBean(BatchProperties.class)
|
||||
.getInitializer().isEnabled()).isTrue();
|
||||
assertThat(new JdbcTemplate(this.context.getBean(DataSource.class))
|
||||
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
|
||||
JobExplorer jobExplorer = this.context.getBean(JobExplorer.class);
|
||||
|
|
@ -237,6 +244,25 @@ public class BatchAutoConfigurationTests {
|
|||
.isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomTablePrefixWithDefaultSchemaDisablesInitializer() throws Exception {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.datasource.name:batchtest",
|
||||
"spring.batch.tablePrefix:PREFIX_");
|
||||
this.context.register(TestConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class,
|
||||
HibernateJpaAutoConfiguration.class, BatchAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
|
||||
assertThat(this.context.getBean(BatchProperties.class)
|
||||
.getInitializer().isEnabled()).isFalse();
|
||||
this.expected.expect(BadSqlGrammarException.class);
|
||||
new JdbcTemplate(this.context.getBean(DataSource.class))
|
||||
.queryForList("select * from BATCH_JOB_EXECUTION");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class EmptyConfiguration {
|
||||
}
|
||||
|
|
|
|||
|
|
@ -847,7 +847,7 @@ content into your application; rather pick only the properties that you need.
|
|||
spring.artemis.user= # Login user of the broker.
|
||||
|
||||
# SPRING BATCH ({sc-spring-boot-autoconfigure}/batch/BatchProperties.{sc-ext}[BatchProperties])
|
||||
spring.batch.initializer.enabled=true # Create the required batch tables on startup if necessary.
|
||||
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.
|
||||
spring.batch.job.enabled=true # Execute all Spring Batch jobs in the context on startup.
|
||||
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.
|
||||
spring.batch.schema=classpath:org/springframework/batch/core/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
|
||||
|
|
|
|||
Loading…
Reference in New Issue