From f313bf27a109253fcc5c08edd292b4fa35c06b44 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 3 Sep 2019 12:11:39 +0100 Subject: [PATCH] Depend on Flyway beans by type not name Previously, a custom Flyway bean named anything other than flyway could result in a NoSucBeanDefinitionException as the dependencies set up for JPA and JDBC components used the bean name flyway. This commit updates the configuration of the dependencies to depend on Flyway beans by name rather than type. Fixes gh-18102 --- .../flyway/FlywayAutoConfiguration.java | 14 +++---- .../flyway/FlywayAutoConfigurationTests.java | 39 ++++++++++++++++++- 2 files changed, 45 insertions(+), 8 deletions(-) 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 975318995f3..d08f8219558 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 @@ -315,7 +315,7 @@ public class FlywayAutoConfiguration { /** * Additional configuration to ensure that {@link EntityManagerFactory} beans depend - * on the {@code flyway} bean. + * on any {@link Flyway} beans. */ @Configuration @ConditionalOnClass(LocalContainerEntityManagerFactoryBean.class) @@ -323,14 +323,14 @@ public class FlywayAutoConfiguration { protected static class FlywayJpaDependencyConfiguration extends EntityManagerFactoryDependsOnPostProcessor { public FlywayJpaDependencyConfiguration() { - super("flyway"); + super(Flyway.class); } } /** - * Additional configuration to ensure that {@link JdbcOperations} beans depend on the - * {@code flyway} bean. + * Additional configuration to ensure that {@link JdbcOperations} beans depend on any + * {@link Flyway} beans. */ @Configuration @ConditionalOnClass(JdbcOperations.class) @@ -338,14 +338,14 @@ public class FlywayAutoConfiguration { protected static class FlywayJdbcOperationsDependencyConfiguration extends JdbcOperationsDependsOnPostProcessor { public FlywayJdbcOperationsDependencyConfiguration() { - super("flyway"); + super(Flyway.class); } } /** * Additional configuration to ensure that {@link NamedParameterJdbcOperations} beans - * depend on the {@code flyway} bean. + * depend on any {@link Flyway} beans. */ @Configuration @ConditionalOnClass(NamedParameterJdbcOperations.class) @@ -354,7 +354,7 @@ public class FlywayAutoConfiguration { extends NamedParameterJdbcOperationsDependsOnPostProcessor { public FlywayNamedParameterJdbcOperationsDependencyConfiguration() { - super("flyway"); + super(Flyway.class); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java index abd5e3ad854..e237ed5ac2d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java @@ -46,6 +46,10 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; +import org.springframework.jdbc.core.JdbcOperations; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.stereotype.Component; @@ -269,6 +273,13 @@ public class FlywayAutoConfigurationTests { .run((context) -> assertThat(context).hasNotFailed()); } + @Test + public void customFlywayWithJdbc() { + this.contextRunner + .withUserConfiguration(EmbeddedDataSourceConfiguration.class, CustomFlywayWithJdbcConfiguration.class) + .run((context) -> assertThat(context).hasNotFailed()); + } + @Test public void overrideBaselineVersionString() { this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) @@ -405,7 +416,7 @@ public class FlywayAutoConfigurationTests { } @Bean - public Flyway flyway() { + public Flyway customFlyway() { return new Flyway(); } @@ -420,6 +431,32 @@ public class FlywayAutoConfigurationTests { } + @Configuration + protected static class CustomFlywayWithJdbcConfiguration { + + private final DataSource dataSource; + + protected CustomFlywayWithJdbcConfiguration(DataSource dataSource) { + this.dataSource = dataSource; + } + + @Bean + public Flyway customFlyway() { + return new Flyway(); + } + + @Bean + public JdbcOperations jdbcOperations() { + return new JdbcTemplate(this.dataSource); + } + + @Bean + public NamedParameterJdbcOperations namedParameterJdbcOperations() { + return new NamedParameterJdbcTemplate(this.dataSource); + } + + } + @Component protected static class MockFlywayMigrationStrategy implements FlywayMigrationStrategy {