From d093807f95d6362e88a2655f738acd6310ee4ec6 Mon Sep 17 00:00:00 2001 From: Mukul Chaundhyan Date: Tue, 9 Feb 2021 10:30:51 +0530 Subject: [PATCH 1/2] Group jdbc-related batch properties beneath spring.batch.jdbc See gh-25316 --- .../batch/BasicBatchConfigurer.java | 5 +- .../batch/BatchAutoConfiguration.java | 5 +- .../batch/BatchDataSourceInitializer.java | 15 +-- .../autoconfigure/batch/BatchProperties.java | 93 ++++++++++++++----- .../batch/BatchAutoConfigurationTests.java | 36 +++++++ ...BatchAutoConfigurationWithoutJpaTests.java | 15 +++ .../JobLauncherApplicationRunnerTests.java | 2 +- 7 files changed, 136 insertions(+), 35 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java index c86f638939..4dd4c2b434 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java @@ -38,6 +38,7 @@ 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 { @@ -111,7 +112,7 @@ public class BasicBatchConfigurer implements BatchConfigurer, InitializingBean { PropertyMapper map = PropertyMapper.get(); JobExplorerFactoryBean factory = new JobExplorerFactoryBean(); factory.setDataSource(this.dataSource); - map.from(this.properties::getTablePrefix).whenHasText().to(factory::setTablePrefix); + map.from(this.properties.getJdbc()::getTablePrefix).whenHasText().to(factory::setTablePrefix); factory.afterPropertiesSet(); return factory.getObject(); } @@ -128,7 +129,7 @@ public class BasicBatchConfigurer implements BatchConfigurer, InitializingBean { PropertyMapper map = PropertyMapper.get(); map.from(this.dataSource).to(factory::setDataSource); map.from(this::determineIsolationLevel).whenNonNull().to(factory::setIsolationLevelForCreate); - map.from(this.properties::getTablePrefix).whenHasText().to(factory::setTablePrefix); + map.from(this.properties.getJdbc()::getTablePrefix).whenHasText().to(factory::setTablePrefix); map.from(this::getTransactionManager).to(factory::setTransactionManager); factory.afterPropertiesSet(); return factory.getObject(); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java index e0ca77d818..3953b5424b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java @@ -57,6 +57,7 @@ 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) @@ -107,11 +108,13 @@ public class BatchAutoConfiguration { @Bean @ConditionalOnMissingBean + @ConditionalOnProperty(prefix = "spring.batch.jdbc", name = "enabled", havingValue = "true", + matchIfMissing = true) BatchDataSourceInitializer batchDataSourceInitializer(DataSource dataSource, @BatchDataSource ObjectProvider batchDataSource, ResourceLoader resourceLoader, BatchProperties properties) { return new BatchDataSourceInitializer(batchDataSource.getIfAvailable(() -> dataSource), resourceLoader, - properties); + properties.getJdbc()); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializer.java index 4abf9de4a0..6cd9c9162b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializer.java @@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.batch; import javax.sql.DataSource; +import org.springframework.boot.autoconfigure.batch.BatchProperties.Jdbc; import org.springframework.boot.jdbc.AbstractDataSourceInitializer; import org.springframework.boot.jdbc.DataSourceInitializationMode; import org.springframework.core.io.ResourceLoader; @@ -28,27 +29,27 @@ 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 BatchProperties properties; + private final Jdbc jdbcProperties; - public BatchDataSourceInitializer(DataSource dataSource, ResourceLoader resourceLoader, - BatchProperties properties) { + public BatchDataSourceInitializer(DataSource dataSource, ResourceLoader resourceLoader, Jdbc jdbcProperties) { super(dataSource, resourceLoader); - Assert.notNull(properties, "BatchProperties must not be null"); - this.properties = properties; + Assert.notNull(jdbcProperties, "Jdbc Batch Properties must not be null"); + this.jdbcProperties = jdbcProperties; } @Override protected DataSourceInitializationMode getMode() { - return this.properties.getInitializeSchema(); + return this.jdbcProperties.getInitializeSchema(); } @Override protected String getSchemaLocation() { - return this.properties.getSchema(); + return this.jdbcProperties.getSchema(); } @Override diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java index d1bdbc0cba..cf78a26cb1 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java @@ -17,6 +17,7 @@ package org.springframework.boot.autoconfigure.batch; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.DeprecatedConfigurationProperty; import org.springframework.boot.jdbc.DataSourceInitializationMode; /** @@ -25,59 +26,51 @@ import org.springframework.boot.jdbc.DataSourceInitializationMode; * @author Stephane Nicoll * @author Eddú Meléndez * @author Vedran Pavic + * @author Mukul Kumar Chaundhyan * @since 1.2.0 */ @ConfigurationProperties(prefix = "spring.batch") public class BatchProperties { - private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/" - + "batch/core/schema-@@platform@@.sql"; - - /** - * Path to the SQL file to use to initialize the database schema. - */ - private String schema = DEFAULT_SCHEMA_LOCATION; - - /** - * Table prefix for all the batch meta-data tables. - */ - private String tablePrefix; - - /** - * Database schema initialization mode. - */ - private DataSourceInitializationMode initializeSchema = DataSourceInitializationMode.EMBEDDED; - private final Job job = new Job(); + private final Jdbc jdbc = new Jdbc(); + + @DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc") public String getSchema() { - return this.schema; + return this.jdbc.getSchema(); } public void setSchema(String schema) { - this.schema = schema; + this.jdbc.setSchema(schema); } + @DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc") public String getTablePrefix() { - return this.tablePrefix; + return this.jdbc.getTablePrefix(); } public void setTablePrefix(String tablePrefix) { - this.tablePrefix = tablePrefix; + this.jdbc.setTablePrefix(tablePrefix); } + @DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc") public DataSourceInitializationMode getInitializeSchema() { - return this.initializeSchema; + return this.jdbc.getInitializeSchema(); } public void setInitializeSchema(DataSourceInitializationMode initializeSchema) { - this.initializeSchema = initializeSchema; + this.jdbc.setInitializeSchema(initializeSchema); } public Job getJob() { return this.job; } + public Jdbc getJdbc() { + return this.jdbc; + } + public static class Job { /** @@ -96,4 +89,56 @@ 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/" + + "batch/core/schema-@@platform@@.sql"; + + /** + * Path to the SQL file to use to initialize the database schema. + */ + private String schema = DEFAULT_SCHEMA_LOCATION; + + /** + * Table prefix for all the batch meta-data tables. + */ + private String tablePrefix; + + /** + * Database schema initialization mode. + */ + private DataSourceInitializationMode initializeSchema = DataSourceInitializationMode.EMBEDDED; + + public String getSchema() { + return this.schema; + } + + public void setSchema(String schema) { + this.schema = schema; + } + + public String getTablePrefix() { + return this.tablePrefix; + } + + public void setTablePrefix(String tablePrefix) { + this.tablePrefix = tablePrefix; + } + + public DataSourceInitializationMode getInitializeSchema() { + return this.initializeSchema; + } + + public void setInitializeSchema(DataSourceInitializationMode initializeSchema) { + this.initializeSchema = initializeSchema; + } + + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java index a00dfca3d0..4898a3b04e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java @@ -187,6 +187,21 @@ class BatchAutoConfigurationTests { }); } + @Test + void testDisableSchemaLoaderWithNewJdbcProperties() { + 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")); + }); + } + @Test void testUsingJpa() { this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class, @@ -224,6 +239,27 @@ class BatchAutoConfigurationTests { }); } + @Test + void testRenamePrefixWithNewJdbcProperties() { + 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(); + }); + } + @Test void testCustomizeJpaTransactionManagerUsingProperties() { this.contextRunner diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationWithoutJpaTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationWithoutJpaTests.java index 218660dfc4..6b7e515cd8 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationWithoutJpaTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationWithoutJpaTests.java @@ -84,6 +84,21 @@ class BatchAutoConfigurationWithoutJpaTests { }); } + @Test + void jdbcWithCustomPrefixWithNewJdbcProperties() { + 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(); + }); + } + @EnableBatchProcessing @TestAutoConfigurationPackage(City.class) static class DefaultConfiguration { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunnerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunnerTests.java index bd93b9a05b..2d3488e3b2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunnerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunnerTests.java @@ -228,7 +228,7 @@ class JobLauncherApplicationRunnerTests { @Bean BatchDataSourceInitializer batchDataSourceInitializer(ResourceLoader resourceLoader) { - return new BatchDataSourceInitializer(this.dataSource, resourceLoader, new BatchProperties()); + return new BatchDataSourceInitializer(this.dataSource, resourceLoader, new BatchProperties().getJdbc()); } } From 9bc4f8ede115b890a2f38a11a81e7bce3c7eec9a Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 17 Feb 2021 11:21:53 +0100 Subject: [PATCH 2/2] Polish "Group jdbc-related batch properties beneath spring.batch.jdbc" See gh-25316 --- .../batch/BasicBatchConfigurer.java | 3 +- .../batch/BatchAutoConfiguration.java | 5 +- .../batch/BatchDataSourceInitializer.java | 8 +- .../autoconfigure/batch/BatchProperties.java | 35 +++++-- ...itional-spring-configuration-metadata.json | 6 +- .../batch/BatchAutoConfigurationTests.java | 91 +++++++++---------- ...BatchAutoConfigurationWithoutJpaTests.java | 42 ++++----- .../JobLauncherApplicationRunnerTests.java | 2 +- .../src/docs/asciidoc/howto.adoc | 5 +- 9 files changed, 104 insertions(+), 93 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java index 4dd4c2b434..15f8ca3e36 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java @@ -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 { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java index 3953b5424b..e0ca77d818 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java @@ -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 batchDataSource, ResourceLoader resourceLoader, BatchProperties properties) { return new BatchDataSourceInitializer(batchDataSource.getIfAvailable(() -> dataSource), resourceLoader, - properties.getJdbc()); + properties); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializer.java index 6cd9c9162b..b642fe2f44 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializer.java @@ -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 diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java index cf78a26cb1..89760536b3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java @@ -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/" diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 153254e79a..cb6fa6771e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -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", diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java index 4898a3b04e..c71d2f0bd1 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java @@ -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 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 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 diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationWithoutJpaTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationWithoutJpaTests.java index 6b7e515cd8..a7bc4c83d4 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationWithoutJpaTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationWithoutJpaTests.java @@ -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 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 diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunnerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunnerTests.java index 2d3488e3b2..bd93b9a05b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunnerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunnerTests.java @@ -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()); } } diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc index 481c0762e2..7fff03eec2 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc @@ -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`.