diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java index 0a6ba394e7d..33973150786 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 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. @@ -46,6 +46,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.autoconfigure.jdbc.JdbcOperationsDependsOnPostProcessor; import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.NamedParameterJdbcOperationsDependsOnPostProcessor; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationPropertiesBinding; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -57,6 +58,7 @@ import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.GenericConverter; import org.springframework.core.io.ResourceLoader; import org.springframework.jdbc.core.JdbcOperations; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; import org.springframework.jdbc.support.JdbcUtils; import org.springframework.jdbc.support.MetaDataAccessException; import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean; @@ -76,6 +78,7 @@ import org.springframework.util.StringUtils; * @author Jacques-Etienne Beaudet * @author Eddú Meléndez * @author Dominic Gunn + * @author Dan Zheng * @since 1.1.0 */ @SuppressWarnings("deprecation") @@ -321,6 +324,23 @@ public class FlywayAutoConfiguration { public FlywayInitializerJdbcOperationsDependencyConfiguration() { super("flywayInitializer"); + + } + + } + + /** + * Additional configuration to ensure that {@link NamedParameterJdbcOperations} + * beans depend on the {@code flywayInitializer} bean. + */ + @Configuration + @ConditionalOnClass(NamedParameterJdbcOperations.class) + @ConditionalOnBean(NamedParameterJdbcOperations.class) + protected static class FlywayInitializerNamedParameterJdbcOperationsDependencyConfiguration + extends NamedParameterJdbcOperationsDependsOnPostProcessor { + + public FlywayInitializerNamedParameterJdbcOperationsDependencyConfiguration() { + super("flywayInitializer"); } } @@ -359,6 +379,22 @@ public class FlywayAutoConfiguration { } + /** + * Additional configuration to ensure that {@link NamedParameterJdbcOperations} beans + * depend on the {@code flyway} bean. + */ + @Configuration + @ConditionalOnClass(NamedParameterJdbcOperations.class) + @ConditionalOnBean(NamedParameterJdbcOperations.class) + protected static class FlywayNamedParameterJdbcOperationsDependencyConfiguration + extends NamedParameterJdbcOperationsDependsOnPostProcessor { + + public FlywayNamedParameterJdbcOperationsDependencyConfiguration() { + super("flyway"); + } + + } + private static class LocationResolver { private static final String VENDOR_PLACEHOLDER = "{vendor}"; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java new file mode 100644 index 00000000000..d4b12cc12a0 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java @@ -0,0 +1,40 @@ +/* + * Copyright 2012-2019 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.jdbc; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; +import org.springframework.boot.autoconfigure.AbstractDependsOnBeanFactoryPostProcessor; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; + +/** + * {@link BeanFactoryPostProcessor} that can be used to dynamically declare that all + * {@link NamedParameterJdbcOperations} beans should "depend on" one or more specific + * beans. + * + * @author Dan Zheng + * @since 2.1.4 + * @see BeanDefinition#setDependsOn(String[]) + */ +public class NamedParameterJdbcOperationsDependsOnPostProcessor + extends AbstractDependsOnBeanFactoryPostProcessor { + + public NamedParameterJdbcOperationsDependsOnPostProcessor(String... dependsOn) { + super(NamedParameterJdbcOperations.class, dependsOn); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java index cd5fdc03f0e..7c220f51b91 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 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,6 +36,7 @@ import org.springframework.boot.autoconfigure.data.jpa.EntityManagerFactoryDepen import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.autoconfigure.jdbc.JdbcOperationsDependsOnPostProcessor; +import org.springframework.boot.autoconfigure.jdbc.NamedParameterJdbcOperationsDependsOnPostProcessor; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; @@ -45,6 +46,7 @@ import org.springframework.context.annotation.Import; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.jdbc.core.JdbcOperations; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.util.Assert; @@ -58,6 +60,7 @@ import org.springframework.util.Assert; * @author Eddú Meléndez * @author Andy Wilkinson * @author Dominic Gunn + * @author Dan Zheng * @since 1.1.0 */ @Configuration @@ -208,4 +211,20 @@ public class LiquibaseAutoConfiguration { } + /** + * Additional configuration to ensure that {@link NamedParameterJdbcOperations} beans + * depend on the liquibase bean. + */ + @Configuration + @ConditionalOnClass(NamedParameterJdbcOperations.class) + @ConditionalOnBean(NamedParameterJdbcOperations.class) + protected static class LiquibaseNamedParameterJdbcOperationsDependencyConfiguration + extends NamedParameterJdbcOperationsDependsOnPostProcessor { + + public LiquibaseNamedParameterJdbcOperationsDependencyConfiguration() { + super("liquibase"); + } + + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java index d918b4658f3..ad083edbbb3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 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. @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure.jdbc; +import java.util.Collections; + import javax.sql.DataSource; import org.junit.Test; @@ -41,6 +43,7 @@ import static org.mockito.Mockito.mock; * @author Dave Syer * @author Stephane Nicoll * @author Kazuki Shimizu + * @author Dan Zheng */ public class JdbcTemplateAutoConfigurationTests { @@ -185,6 +188,21 @@ public class JdbcTemplateAutoConfigurationTests { }); } + @Test + public void testDependencyToFlywayWithJdbcTemplateMixed() { + this.contextRunner + .withUserConfiguration(NamedParameterDataSourceMigrationValidator.class) + .withPropertyValues("spring.flyway.locations:classpath:db/city") + .withConfiguration(AutoConfigurations.of(FlywayAutoConfiguration.class)) + .run((context) -> { + assertThat(context).hasNotFailed(); + assertThat(context.getBean(JdbcTemplate.class)).isNotNull(); + assertThat(context.getBean( + NamedParameterDataSourceMigrationValidator.class).count) + .isEqualTo(0); + }); + } + @Test public void testDependencyToLiquibase() { this.contextRunner.withUserConfiguration(DataSourceMigrationValidator.class) @@ -199,6 +217,23 @@ public class JdbcTemplateAutoConfigurationTests { }); } + @Test + public void testDependencyToLiquibaseWithJdbcTemplateMixed() { + this.contextRunner + .withUserConfiguration(NamedParameterDataSourceMigrationValidator.class) + .withPropertyValues( + "spring.liquibase.changeLog:classpath:db/changelog/db.changelog-city.yaml") + .withConfiguration( + AutoConfigurations.of(LiquibaseAutoConfiguration.class)) + .run((context) -> { + assertThat(context).hasNotFailed(); + assertThat(context.getBean(JdbcTemplate.class)).isNotNull(); + assertThat(context.getBean( + NamedParameterDataSourceMigrationValidator.class).count) + .isEqualTo(0); + }); + } + @Configuration static class CustomConfiguration { @@ -278,4 +313,16 @@ public class JdbcTemplateAutoConfigurationTests { } + static class NamedParameterDataSourceMigrationValidator { + + private final Integer count; + + NamedParameterDataSourceMigrationValidator( + NamedParameterJdbcTemplate namedParameterJdbcTemplate) { + this.count = namedParameterJdbcTemplate.queryForObject( + "SELECT COUNT(*) from CITY", Collections.emptyMap(), Integer.class); + } + + } + }