Skip to content

Commit f313bf2

Browse files
committed
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
1 parent ae86343 commit f313bf2

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,37 +315,37 @@ public FlywayInitializerNamedParameterJdbcOperationsDependencyConfiguration() {
315315

316316
/**
317317
* Additional configuration to ensure that {@link EntityManagerFactory} beans depend
318-
* on the {@code flyway} bean.
318+
* on any {@link Flyway} beans.
319319
*/
320320
@Configuration
321321
@ConditionalOnClass(LocalContainerEntityManagerFactoryBean.class)
322322
@ConditionalOnBean(AbstractEntityManagerFactoryBean.class)
323323
protected static class FlywayJpaDependencyConfiguration extends EntityManagerFactoryDependsOnPostProcessor {
324324

325325
public FlywayJpaDependencyConfiguration() {
326-
super("flyway");
326+
super(Flyway.class);
327327
}
328328

329329
}
330330

331331
/**
332-
* Additional configuration to ensure that {@link JdbcOperations} beans depend on the
333-
* {@code flyway} bean.
332+
* Additional configuration to ensure that {@link JdbcOperations} beans depend on any
333+
* {@link Flyway} beans.
334334
*/
335335
@Configuration
336336
@ConditionalOnClass(JdbcOperations.class)
337337
@ConditionalOnBean(JdbcOperations.class)
338338
protected static class FlywayJdbcOperationsDependencyConfiguration extends JdbcOperationsDependsOnPostProcessor {
339339

340340
public FlywayJdbcOperationsDependencyConfiguration() {
341-
super("flyway");
341+
super(Flyway.class);
342342
}
343343

344344
}
345345

346346
/**
347347
* Additional configuration to ensure that {@link NamedParameterJdbcOperations} beans
348-
* depend on the {@code flyway} bean.
348+
* depend on any {@link Flyway} beans.
349349
*/
350350
@Configuration
351351
@ConditionalOnClass(NamedParameterJdbcOperations.class)
@@ -354,7 +354,7 @@ protected static class FlywayNamedParameterJdbcOperationsDependencyConfiguration
354354
extends NamedParameterJdbcOperationsDependsOnPostProcessor {
355355

356356
public FlywayNamedParameterJdbcOperationsDependencyConfiguration() {
357-
super("flyway");
357+
super(Flyway.class);
358358
}
359359

360360
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
import org.springframework.context.annotation.Primary;
4747
import org.springframework.core.Ordered;
4848
import org.springframework.core.annotation.Order;
49+
import org.springframework.jdbc.core.JdbcOperations;
50+
import org.springframework.jdbc.core.JdbcTemplate;
51+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
52+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
4953
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
5054
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
5155
import org.springframework.stereotype.Component;
@@ -269,6 +273,13 @@ public void customFlywayWithJpa() {
269273
.run((context) -> assertThat(context).hasNotFailed());
270274
}
271275

276+
@Test
277+
public void customFlywayWithJdbc() {
278+
this.contextRunner
279+
.withUserConfiguration(EmbeddedDataSourceConfiguration.class, CustomFlywayWithJdbcConfiguration.class)
280+
.run((context) -> assertThat(context).hasNotFailed());
281+
}
282+
272283
@Test
273284
public void overrideBaselineVersionString() {
274285
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
@@ -405,7 +416,7 @@ protected CustomFlywayWithJpaConfiguration(DataSource dataSource) {
405416
}
406417

407418
@Bean
408-
public Flyway flyway() {
419+
public Flyway customFlyway() {
409420
return new Flyway();
410421
}
411422

@@ -420,6 +431,32 @@ public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
420431

421432
}
422433

434+
@Configuration
435+
protected static class CustomFlywayWithJdbcConfiguration {
436+
437+
private final DataSource dataSource;
438+
439+
protected CustomFlywayWithJdbcConfiguration(DataSource dataSource) {
440+
this.dataSource = dataSource;
441+
}
442+
443+
@Bean
444+
public Flyway customFlyway() {
445+
return new Flyway();
446+
}
447+
448+
@Bean
449+
public JdbcOperations jdbcOperations() {
450+
return new JdbcTemplate(this.dataSource);
451+
}
452+
453+
@Bean
454+
public NamedParameterJdbcOperations namedParameterJdbcOperations() {
455+
return new NamedParameterJdbcTemplate(this.dataSource);
456+
}
457+
458+
}
459+
423460
@Component
424461
protected static class MockFlywayMigrationStrategy implements FlywayMigrationStrategy {
425462

0 commit comments

Comments
 (0)