Depend on FlywayMigrationInitializer beans by type not name
Previously, a custom FlywayMigrationInitializer bean named anything other than flywayInitializer could result in a NoSucBeanDefinitionException as the dependencies set up for JPA and JDBC components used the bean name flywayInitializer. This commit updates the configuration of the dependencies to depend on FlywayMigrationInitializer beans by type rather than name. Fixes gh-18105
This commit is contained in:
		
							parent
							
								
									f313bf27a1
								
							
						
					
					
						commit
						52311ffe3c
					
				|  | @ -265,7 +265,7 @@ public class FlywayAutoConfiguration { | ||||||
| 
 | 
 | ||||||
| 		/** | 		/** | ||||||
| 		 * Additional configuration to ensure that {@link EntityManagerFactory} beans | 		 * Additional configuration to ensure that {@link EntityManagerFactory} beans | ||||||
| 		 * depend on the {@code flywayInitializer} bean. | 		 * depend on any {@link FlywayMigrationInitializer} beans. | ||||||
| 		 */ | 		 */ | ||||||
| 		@Configuration | 		@Configuration | ||||||
| 		@ConditionalOnClass(LocalContainerEntityManagerFactoryBean.class) | 		@ConditionalOnClass(LocalContainerEntityManagerFactoryBean.class) | ||||||
|  | @ -274,14 +274,14 @@ public class FlywayAutoConfiguration { | ||||||
| 				extends EntityManagerFactoryDependsOnPostProcessor { | 				extends EntityManagerFactoryDependsOnPostProcessor { | ||||||
| 
 | 
 | ||||||
| 			public FlywayInitializerJpaDependencyConfiguration() { | 			public FlywayInitializerJpaDependencyConfiguration() { | ||||||
| 				super("flywayInitializer"); | 				super(FlywayMigrationInitializer.class); | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		/** | 		/** | ||||||
| 		 * Additional configuration to ensure that {@link JdbcOperations} beans depend on | 		 * Additional configuration to ensure that {@link JdbcOperations} beans depend on | ||||||
| 		 * the {@code flywayInitializer} bean. | 		 * any {@link FlywayMigrationInitializer} beans. | ||||||
| 		 */ | 		 */ | ||||||
| 		@Configuration | 		@Configuration | ||||||
| 		@ConditionalOnClass(JdbcOperations.class) | 		@ConditionalOnClass(JdbcOperations.class) | ||||||
|  | @ -290,14 +290,14 @@ public class FlywayAutoConfiguration { | ||||||
| 				extends JdbcOperationsDependsOnPostProcessor { | 				extends JdbcOperationsDependsOnPostProcessor { | ||||||
| 
 | 
 | ||||||
| 			public FlywayInitializerJdbcOperationsDependencyConfiguration() { | 			public FlywayInitializerJdbcOperationsDependencyConfiguration() { | ||||||
| 				super("flywayInitializer"); | 				super(FlywayMigrationInitializer.class); | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		/** | 		/** | ||||||
| 		 * Additional configuration to ensure that {@link NamedParameterJdbcOperations} | 		 * Additional configuration to ensure that {@link NamedParameterJdbcOperations} | ||||||
| 		 * beans depend on the {@code flywayInitializer} bean. | 		 * beans depend on any {@link FlywayMigrationInitializer} beans. | ||||||
| 		 */ | 		 */ | ||||||
| 		@Configuration | 		@Configuration | ||||||
| 		@ConditionalOnClass(NamedParameterJdbcOperations.class) | 		@ConditionalOnClass(NamedParameterJdbcOperations.class) | ||||||
|  | @ -306,7 +306,7 @@ public class FlywayAutoConfiguration { | ||||||
| 				extends NamedParameterJdbcOperationsDependsOnPostProcessor { | 				extends NamedParameterJdbcOperationsDependsOnPostProcessor { | ||||||
| 
 | 
 | ||||||
| 			public FlywayInitializerNamedParameterJdbcOperationsDependencyConfiguration() { | 			public FlywayInitializerNamedParameterJdbcOperationsDependencyConfiguration() { | ||||||
| 				super("flywayInitializer"); | 				super(FlywayMigrationInitializer.class); | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | @ -266,6 +266,22 @@ public class FlywayAutoConfigurationTests { | ||||||
| 				}); | 				}); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	@Test | ||||||
|  | 	public void customFlywayMigrationInitializerWithJpa() { | ||||||
|  | 		this.contextRunner | ||||||
|  | 				.withUserConfiguration(EmbeddedDataSourceConfiguration.class, | ||||||
|  | 						CustomFlywayMigrationInitializerWithJpaConfiguration.class) | ||||||
|  | 				.run((context) -> assertThat(context).hasNotFailed()); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	@Test | ||||||
|  | 	public void customFlywayMigrationInitializerWithJdbc() { | ||||||
|  | 		this.contextRunner | ||||||
|  | 				.withUserConfiguration(EmbeddedDataSourceConfiguration.class, | ||||||
|  | 						CustomFlywayMigrationInitializerWithJdbcConfiguration.class) | ||||||
|  | 				.run((context) -> assertThat(context).hasNotFailed()); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void customFlywayWithJpa() { | 	public void customFlywayWithJpa() { | ||||||
| 		this.contextRunner | 		this.contextRunner | ||||||
|  | @ -406,6 +422,25 @@ public class FlywayAutoConfigurationTests { | ||||||
| 
 | 
 | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	@Configuration | ||||||
|  | 	protected static class CustomFlywayMigrationInitializerWithJpaConfiguration { | ||||||
|  | 
 | ||||||
|  | 		@Bean | ||||||
|  | 		public FlywayMigrationInitializer customFlywayMigrationInitializer(Flyway flyway) { | ||||||
|  | 			return new FlywayMigrationInitializer(flyway); | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		@Bean | ||||||
|  | 		public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(DataSource dataSource) { | ||||||
|  | 			Map<String, Object> 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 | 	@Configuration | ||||||
| 	protected static class CustomFlywayWithJpaConfiguration { | 	protected static class CustomFlywayWithJpaConfiguration { | ||||||
| 
 | 
 | ||||||
|  | @ -457,6 +492,32 @@ public 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 | 	@Component | ||||||
| 	protected static class MockFlywayMigrationStrategy implements FlywayMigrationStrategy { | 	protected static class MockFlywayMigrationStrategy implements FlywayMigrationStrategy { | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue