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
This commit is contained in:
Andy Wilkinson 2019-09-03 12:11:39 +01:00
parent ae863434b6
commit f313bf27a1
2 changed files with 45 additions and 8 deletions

View File

@ -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);
}
}

View File

@ -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 {