Polish "Group jdbc-related batch properties beneath spring.batch.jdbc"

See gh-25316
This commit is contained in:
Stephane Nicoll
2021-02-17 11:21:53 +01:00
parent d093807f95
commit 9bc4f8ede1
9 changed files with 104 additions and 93 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@@ -38,7 +38,6 @@ import org.springframework.transaction.PlatformTransactionManager;
* @author Andy Wilkinson
* @author Kazuki Shimizu
* @author Stephane Nicoll
* @author Mukul Kumar Chaundhyan
* @since 1.0.0
*/
public class BasicBatchConfigurer implements BatchConfigurer, InitializingBean {

View File

@@ -57,7 +57,6 @@ import org.springframework.util.StringUtils;
* @author Eddú Meléndez
* @author Kazuki Shimizu
* @author Mahmoud Ben Hassine
* @author Mukul Kumar Chaundhyan
* @since 1.0.0
*/
@Configuration(proxyBeanMethods = false)
@@ -108,13 +107,11 @@ public class BatchAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.batch.jdbc", name = "enabled", havingValue = "true",
matchIfMissing = true)
BatchDataSourceInitializer batchDataSourceInitializer(DataSource dataSource,
@BatchDataSource ObjectProvider<DataSource> batchDataSource, ResourceLoader resourceLoader,
BatchProperties properties) {
return new BatchDataSourceInitializer(batchDataSource.getIfAvailable(() -> dataSource), resourceLoader,
properties.getJdbc());
properties);
}
}

View File

@@ -29,17 +29,17 @@ import org.springframework.util.Assert;
*
* @author Dave Syer
* @author Vedran Pavic
* @author Mukul Kumar Chaundhyan
* @since 1.0.0
*/
public class BatchDataSourceInitializer extends AbstractDataSourceInitializer {
private final Jdbc jdbcProperties;
public BatchDataSourceInitializer(DataSource dataSource, ResourceLoader resourceLoader, Jdbc jdbcProperties) {
public BatchDataSourceInitializer(DataSource dataSource, ResourceLoader resourceLoader,
BatchProperties properties) {
super(dataSource, resourceLoader);
Assert.notNull(jdbcProperties, "Jdbc Batch Properties must not be null");
this.jdbcProperties = jdbcProperties;
Assert.notNull(properties, "BatchProperties must not be null");
this.jdbcProperties = properties.getJdbc();
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -36,29 +36,50 @@ public class BatchProperties {
private final Jdbc jdbc = new Jdbc();
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc")
/**
* Return the datasource schema.
* @return the schema
* @deprecated as of 2.5.0 in favor of {@link Jdbc#getSchema()}
*/
@Deprecated
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc.schema")
public String getSchema() {
return this.jdbc.getSchema();
}
@Deprecated
public void setSchema(String schema) {
this.jdbc.setSchema(schema);
}
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc")
/**
* Return the table prefix.
* @return the table prefix
* @deprecated as of 2.5.0 in favor of {@link Jdbc#getTablePrefix()}
*/
@Deprecated
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc.table-prefix")
public String getTablePrefix() {
return this.jdbc.getTablePrefix();
}
@Deprecated
public void setTablePrefix(String tablePrefix) {
this.jdbc.setTablePrefix(tablePrefix);
}
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc")
/**
* Return whether the schema should be initialized.
* @return the initialization mode
* @deprecated as of 2.5.0 in favor of {@link Jdbc#getInitializeSchema()}
*/
@Deprecated
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc.initialize-schema")
public DataSourceInitializationMode getInitializeSchema() {
return this.jdbc.getInitializeSchema();
}
@Deprecated
public void setInitializeSchema(DataSourceInitializationMode initializeSchema) {
this.jdbc.setInitializeSchema(initializeSchema);
}
@@ -89,12 +110,6 @@ public class BatchProperties {
}
/**
* JDBC configuration properties for Spring Batch.
*
* @author Mukul Kumar Chaundhyan
* @since 2.5.0
*/
public static class Jdbc {
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"

View File

@@ -376,10 +376,14 @@
"type": "java.lang.Boolean",
"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.",
"deprecation": {
"replacement": "spring.batch.initialize-schema",
"replacement": "spring.batch.jdbc.initialize-schema",
"level": "error"
}
},
{
"name": "spring.batch.jdbc.initialize-schema",
"defaultValue": "embedded"
},
{
"name": "spring.batch.job.enabled",
"type": "java.lang.Boolean",

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@@ -49,7 +49,9 @@ import org.springframework.boot.autoconfigure.orm.jpa.test.City;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.jdbc.DataSourceInitializationMode;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@@ -82,7 +84,7 @@ class BatchAutoConfigurationTests {
.run((context) -> {
assertThat(context).hasSingleBean(JobLauncher.class);
assertThat(context).hasSingleBean(JobExplorer.class);
assertThat(context.getBean(BatchProperties.class).getInitializeSchema())
assertThat(context.getBean(BatchProperties.class).getJdbc().getInitializeSchema())
.isEqualTo(DataSourceInitializationMode.EMBEDDED);
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
@@ -176,30 +178,28 @@ class BatchAutoConfigurationTests {
void testDisableSchemaLoader() {
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.datasource.generate-unique-name=true",
"spring.batch.initialize-schema:never")
.run((context) -> {
assertThat(context).hasSingleBean(JobLauncher.class);
assertThat(context.getBean(BatchProperties.class).getInitializeSchema())
.isEqualTo(DataSourceInitializationMode.NEVER);
assertThatExceptionOfType(BadSqlGrammarException.class)
.isThrownBy(() -> new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION"));
});
"spring.batch.jdbc.initialize-schema:never")
.run(assertDatasourceIsNotInitialized());
}
@Test
void testDisableSchemaLoaderWithNewJdbcProperties() {
@Deprecated
void testDisableSchemaLoaderWithDeprecatedProperty() {
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.datasource.generate-unique-name=true",
"spring.batch.jdbc.initialize-schema:never")
.run((context) -> {
assertThat(context).hasSingleBean(JobLauncher.class);
assertThat(context.getBean(BatchProperties.class).getInitializeSchema())
.isEqualTo(DataSourceInitializationMode.NEVER);
assertThatExceptionOfType(BadSqlGrammarException.class)
.isThrownBy(() -> new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION"));
});
"spring.batch.initialize-schema:never")
.run(assertDatasourceIsNotInitialized());
}
private ContextConsumer<AssertableApplicationContext> assertDatasourceIsNotInitialized() {
return (context) -> {
assertThat(context).hasSingleBean(JobLauncher.class);
assertThat(context.getBean(BatchProperties.class).getJdbc().getInitializeSchema())
.isEqualTo(DataSourceInitializationMode.NEVER);
assertThatExceptionOfType(BadSqlGrammarException.class)
.isThrownBy(() -> new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION"));
};
}
@Test
@@ -224,40 +224,35 @@ class BatchAutoConfigurationTests {
.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class,
HibernateJpaAutoConfiguration.class)
.withPropertyValues("spring.datasource.generate-unique-name=true",
"spring.batch.schema:classpath:batch/custom-schema-hsql.sql",
"spring.batch.tablePrefix:PREFIX_")
.run((context) -> {
assertThat(context).hasSingleBean(JobLauncher.class);
assertThat(context.getBean(BatchProperties.class).getInitializeSchema())
.isEqualTo(DataSourceInitializationMode.EMBEDDED);
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
assertThat(jobExplorer.findRunningJobExecutions("test")).isEmpty();
JobRepository jobRepository = context.getBean(JobRepository.class);
assertThat(jobRepository.getLastJobExecution("test", new JobParameters())).isNull();
});
"spring.batch.jdbc.schema:classpath:batch/custom-schema-hsql.sql",
"spring.batch.jdbc.tablePrefix:PREFIX_")
.run(assertCustomTablePrefix());
}
@Test
void testRenamePrefixWithNewJdbcProperties() {
@Deprecated
void testRenamePrefixWithDeprecatedProperty() {
this.contextRunner
.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class,
HibernateJpaAutoConfiguration.class)
.withPropertyValues("spring.datasource.generate-unique-name=true",
"spring.batch.jdbc.schema:classpath:batch/custom-schema-hsql.sql",
"spring.batch.jdbc.tablePrefix:PREFIX_")
.run((context) -> {
assertThat(context).hasSingleBean(JobLauncher.class);
assertThat(context.getBean(BatchProperties.class).getInitializeSchema())
.isEqualTo(DataSourceInitializationMode.EMBEDDED);
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
assertThat(jobExplorer.findRunningJobExecutions("test")).isEmpty();
JobRepository jobRepository = context.getBean(JobRepository.class);
assertThat(jobRepository.getLastJobExecution("test", new JobParameters())).isNull();
});
"spring.batch.schema:classpath:batch/custom-schema-hsql.sql",
"spring.batch.tablePrefix:PREFIX_")
.run(assertCustomTablePrefix());
}
private ContextConsumer<AssertableApplicationContext> assertCustomTablePrefix() {
return (context) -> {
assertThat(context).hasSingleBean(JobLauncher.class);
assertThat(context.getBean(BatchProperties.class).getJdbc().getInitializeSchema())
.isEqualTo(DataSourceInitializationMode.EMBEDDED);
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
assertThat(jobExplorer.findRunningJobExecutions("test")).isEmpty();
JobRepository jobRepository = context.getBean(JobRepository.class);
assertThat(jobRepository.getLastJobExecution("test", new JobParameters())).isNull();
};
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -31,7 +31,9 @@ import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfigurati
import org.springframework.boot.autoconfigure.orm.jpa.test.City;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
import org.springframework.boot.jdbc.DataSourceInitializationMode;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
@@ -59,7 +61,7 @@ class BatchAutoConfigurationWithoutJpaTests {
assertThat(context).hasSingleBean(PlatformTransactionManager.class);
assertThat(context.getBean(PlatformTransactionManager.class).toString())
.contains("DataSourceTransactionManager");
assertThat(context.getBean(BatchProperties.class).getInitializeSchema())
assertThat(context.getBean(BatchProperties.class).getJdbc().getInitializeSchema())
.isEqualTo(DataSourceInitializationMode.EMBEDDED);
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
@@ -73,30 +75,28 @@ class BatchAutoConfigurationWithoutJpaTests {
void jdbcWithCustomPrefix() {
this.contextRunner.withUserConfiguration(DefaultConfiguration.class, EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.datasource.generate-unique-name=true",
"spring.batch.schema:classpath:batch/custom-schema-hsql.sql",
"spring.batch.tablePrefix:PREFIX_")
.run((context) -> {
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
assertThat(context.getBean(JobExplorer.class).findRunningJobExecutions("test")).isEmpty();
assertThat(context.getBean(JobRepository.class).getLastJobExecution("test", new JobParameters()))
.isNull();
});
"spring.batch.jdbc.schema:classpath:batch/custom-schema-hsql.sql",
"spring.batch.jdbc.tablePrefix:PREFIX_")
.run(assertCustomPrefix());
}
@Test
void jdbcWithCustomPrefixWithNewJdbcProperties() {
@Deprecated
void jdbcWithCustomPrefixWithDeprecatedProperties() {
this.contextRunner.withUserConfiguration(DefaultConfiguration.class, EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.datasource.generate-unique-name=true",
"spring.batch.jdbc.schema:classpath:batch/custom-schema-hsql.sql",
"spring.batch.jdbc.tablePrefix:PREFIX_")
.run((context) -> {
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
assertThat(context.getBean(JobExplorer.class).findRunningJobExecutions("test")).isEmpty();
assertThat(context.getBean(JobRepository.class).getLastJobExecution("test", new JobParameters()))
.isNull();
});
"spring.batch.schema:classpath:batch/custom-schema-hsql.sql",
"spring.batch.tablePrefix:PREFIX_")
.run(assertCustomPrefix());
}
private ContextConsumer<AssertableApplicationContext> assertCustomPrefix() {
return (context) -> {
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
assertThat(context.getBean(JobExplorer.class).findRunningJobExecutions("test")).isEmpty();
assertThat(context.getBean(JobRepository.class).getLastJobExecution("test", new JobParameters())).isNull();
};
}
@EnableBatchProcessing

View File

@@ -228,7 +228,7 @@ class JobLauncherApplicationRunnerTests {
@Bean
BatchDataSourceInitializer batchDataSourceInitializer(ResourceLoader resourceLoader) {
return new BatchDataSourceInitializer(this.dataSource, resourceLoader, new BatchProperties().getJdbc());
return new BatchDataSourceInitializer(this.dataSource, resourceLoader, new BatchProperties());
}
}

View File

@@ -2087,10 +2087,11 @@ You can also enable it for any database type, as shown in the following example:
----
spring:
batch:
initialize-schema: "always"
jdbc:
initialize-schema: "always"
----
You can also switch off the initialization explicitly by setting `spring.batch.initialize-schema` to `never`.
You can also switch off the initialization explicitly by setting `spring.batch.jdbc.initialize-schema` to `never`.