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 7db159bc98..dc768f42de 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 @@ -251,7 +251,7 @@ public class FlywayAutoConfiguration { /** * Additional configuration to ensure that {@link EntityManagerFactory} beans - * depend on the {@code flywayInitializer} bean. + * depend on any {@link FlywayMigrationInitializer} beans. */ @Configuration(proxyBeanMethods = false) @ConditionalOnClass(LocalContainerEntityManagerFactoryBean.class) @@ -260,14 +260,14 @@ public class FlywayAutoConfiguration { extends EntityManagerFactoryDependsOnPostProcessor { public FlywayInitializerJpaDependencyConfiguration() { - super("flywayInitializer"); + super(FlywayMigrationInitializer.class); } } /** * Additional configuration to ensure that {@link JdbcOperations} beans depend on - * the {@code flywayInitializer} bean. + * any {@link FlywayMigrationInitializer} beans. */ @Configuration(proxyBeanMethods = false) @ConditionalOnClass(JdbcOperations.class) @@ -276,14 +276,14 @@ public class FlywayAutoConfiguration { extends JdbcOperationsDependsOnPostProcessor { public FlywayInitializerJdbcOperationsDependencyConfiguration() { - super("flywayInitializer"); + super(FlywayMigrationInitializer.class); } } /** * Additional configuration to ensure that {@link NamedParameterJdbcOperations} - * beans depend on the {@code flywayInitializer} bean. + * beans depend on any {@link FlywayMigrationInitializer} beans. */ @Configuration(proxyBeanMethods = false) @ConditionalOnClass(NamedParameterJdbcOperations.class) @@ -292,7 +292,7 @@ public class FlywayAutoConfiguration { extends NamedParameterJdbcOperationsDependsOnPostProcessor { public FlywayInitializerNamedParameterJdbcOperationsDependencyConfiguration() { - super("flywayInitializer"); + super(FlywayMigrationInitializer.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 de2afb08ea..d608045913 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 @@ -304,6 +304,22 @@ class FlywayAutoConfigurationTests { .run((context) -> assertThat(context).hasNotFailed()); } + @Test + void customFlywayMigrationInitializerWithJpa() { + this.contextRunner + .withUserConfiguration(EmbeddedDataSourceConfiguration.class, + CustomFlywayMigrationInitializerWithJpaConfiguration.class) + .run((context) -> assertThat(context).hasNotFailed()); + } + + @Test + void customFlywayMigrationInitializerWithJdbc() { + this.contextRunner + .withUserConfiguration(EmbeddedDataSourceConfiguration.class, + CustomFlywayMigrationInitializerWithJdbcConfiguration.class) + .run((context) -> assertThat(context).hasNotFailed()); + } + @Test void overrideBaselineVersionString() { this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) @@ -531,6 +547,25 @@ class FlywayAutoConfigurationTests { } + @Configuration(proxyBeanMethods = false) + static class CustomFlywayMigrationInitializerWithJpaConfiguration { + + @Bean + FlywayMigrationInitializer customFlywayMigrationInitializer(Flyway flyway) { + return new FlywayMigrationInitializer(flyway); + } + + @Bean + LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(DataSource dataSource) { + Map properties = new HashMap<>(); + properties.put("configured", "manually"); + properties.put("hibernate.transaction.jta.platform", NoJtaPlatform.INSTANCE); + return new EntityManagerFactoryBuilder(new HibernateJpaVendorAdapter(), properties, null) + .dataSource(dataSource).build(); + } + + } + @Configuration(proxyBeanMethods = false) static class CustomFlywayWithJpaConfiguration { @@ -582,6 +617,32 @@ class FlywayAutoConfigurationTests { } + @Configuration + protected static class CustomFlywayMigrationInitializerWithJdbcConfiguration { + + private final DataSource dataSource; + + protected CustomFlywayMigrationInitializerWithJdbcConfiguration(DataSource dataSource) { + this.dataSource = dataSource; + } + + @Bean + public FlywayMigrationInitializer customFlywayMigrationInitializer(Flyway flyway) { + return new FlywayMigrationInitializer(flyway); + } + + @Bean + public JdbcOperations jdbcOperations() { + return new JdbcTemplate(this.dataSource); + } + + @Bean + public NamedParameterJdbcOperations namedParameterJdbcOperations() { + return new NamedParameterJdbcTemplate(this.dataSource); + } + + } + @Component static class MockFlywayMigrationStrategy implements FlywayMigrationStrategy {