Commit f313bf27 authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent ae863434
...@@ -315,7 +315,7 @@ public class FlywayAutoConfiguration { ...@@ -315,7 +315,7 @@ public class FlywayAutoConfiguration {
/** /**
* Additional configuration to ensure that {@link EntityManagerFactory} beans depend * Additional configuration to ensure that {@link EntityManagerFactory} beans depend
* on the {@code flyway} bean. * on any {@link Flyway} beans.
*/ */
@Configuration @Configuration
@ConditionalOnClass(LocalContainerEntityManagerFactoryBean.class) @ConditionalOnClass(LocalContainerEntityManagerFactoryBean.class)
...@@ -323,14 +323,14 @@ public class FlywayAutoConfiguration { ...@@ -323,14 +323,14 @@ public class FlywayAutoConfiguration {
protected static class FlywayJpaDependencyConfiguration extends EntityManagerFactoryDependsOnPostProcessor { protected static class FlywayJpaDependencyConfiguration extends EntityManagerFactoryDependsOnPostProcessor {
public FlywayJpaDependencyConfiguration() { public FlywayJpaDependencyConfiguration() {
super("flyway"); super(Flyway.class);
} }
} }
/** /**
* Additional configuration to ensure that {@link JdbcOperations} beans depend on the * Additional configuration to ensure that {@link JdbcOperations} beans depend on any
* {@code flyway} bean. * {@link Flyway} beans.
*/ */
@Configuration @Configuration
@ConditionalOnClass(JdbcOperations.class) @ConditionalOnClass(JdbcOperations.class)
...@@ -338,14 +338,14 @@ public class FlywayAutoConfiguration { ...@@ -338,14 +338,14 @@ public class FlywayAutoConfiguration {
protected static class FlywayJdbcOperationsDependencyConfiguration extends JdbcOperationsDependsOnPostProcessor { protected static class FlywayJdbcOperationsDependencyConfiguration extends JdbcOperationsDependsOnPostProcessor {
public FlywayJdbcOperationsDependencyConfiguration() { public FlywayJdbcOperationsDependencyConfiguration() {
super("flyway"); super(Flyway.class);
} }
} }
/** /**
* Additional configuration to ensure that {@link NamedParameterJdbcOperations} beans * Additional configuration to ensure that {@link NamedParameterJdbcOperations} beans
* depend on the {@code flyway} bean. * depend on any {@link Flyway} beans.
*/ */
@Configuration @Configuration
@ConditionalOnClass(NamedParameterJdbcOperations.class) @ConditionalOnClass(NamedParameterJdbcOperations.class)
...@@ -354,7 +354,7 @@ public class FlywayAutoConfiguration { ...@@ -354,7 +354,7 @@ public class FlywayAutoConfiguration {
extends NamedParameterJdbcOperationsDependsOnPostProcessor { extends NamedParameterJdbcOperationsDependsOnPostProcessor {
public FlywayNamedParameterJdbcOperationsDependencyConfiguration() { public FlywayNamedParameterJdbcOperationsDependencyConfiguration() {
super("flyway"); super(Flyway.class);
} }
} }
......
...@@ -46,6 +46,10 @@ import org.springframework.context.annotation.Configuration; ...@@ -46,6 +46,10 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; 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.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -269,6 +273,13 @@ public class FlywayAutoConfigurationTests { ...@@ -269,6 +273,13 @@ public class FlywayAutoConfigurationTests {
.run((context) -> assertThat(context).hasNotFailed()); .run((context) -> assertThat(context).hasNotFailed());
} }
@Test
public void customFlywayWithJdbc() {
this.contextRunner
.withUserConfiguration(EmbeddedDataSourceConfiguration.class, CustomFlywayWithJdbcConfiguration.class)
.run((context) -> assertThat(context).hasNotFailed());
}
@Test @Test
public void overrideBaselineVersionString() { public void overrideBaselineVersionString() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
...@@ -405,7 +416,7 @@ public class FlywayAutoConfigurationTests { ...@@ -405,7 +416,7 @@ public class FlywayAutoConfigurationTests {
} }
@Bean @Bean
public Flyway flyway() { public Flyway customFlyway() {
return new Flyway(); return new Flyway();
} }
...@@ -420,6 +431,32 @@ public class FlywayAutoConfigurationTests { ...@@ -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 @Component
protected static class MockFlywayMigrationStrategy implements FlywayMigrationStrategy { protected static class MockFlywayMigrationStrategy implements FlywayMigrationStrategy {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment