From 2fec06ac7ebfe99f545e63c13bc9c294e24e85cf Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 10 Dec 2021 14:38:08 +0000 Subject: [PATCH] Find annotation without initializing factory beans Closes gh-28977 --- ...pendsOnDatabaseInitializationDetector.java | 2 +- ...itializerDependencyConfigurationTests.java | 20 +++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/sql/init/dependency/AnnotationDependsOnDatabaseInitializationDetector.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/sql/init/dependency/AnnotationDependsOnDatabaseInitializationDetector.java index 6d76427aef..ae947bc01f 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/sql/init/dependency/AnnotationDependsOnDatabaseInitializationDetector.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/sql/init/dependency/AnnotationDependsOnDatabaseInitializationDetector.java @@ -33,7 +33,7 @@ class AnnotationDependsOnDatabaseInitializationDetector implements DependsOnData public Set detect(ConfigurableListableBeanFactory beanFactory) { Set dependentBeans = new HashSet<>(); for (String beanName : beanFactory.getBeanDefinitionNames()) { - if (beanFactory.findAnnotationOnBean(beanName, DependsOnDatabaseInitialization.class) != null) { + if (beanFactory.findAnnotationOnBean(beanName, DependsOnDatabaseInitialization.class, false) != null) { dependentBeans.add(beanName); } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/AbstractDataSourceInitializerDependencyConfigurationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/AbstractDataSourceInitializerDependencyConfigurationTests.java index 4a09876af3..6786079a77 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/AbstractDataSourceInitializerDependencyConfigurationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/AbstractDataSourceInitializerDependencyConfigurationTests.java @@ -39,11 +39,13 @@ import static org.assertj.core.api.Assertions.assertThat; class AbstractDataSourceInitializerDependencyConfigurationTests { @Test - void beanThatDependsOnDatabaseInitializationDependsOnAbstractDataSourceInitializerBeans() { + void beansThatDependOnDatabaseInitializationDependOnAbstractDataSourceInitializerBeans() { try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( TestConfiguration.class)) { - assertThat(context.getBeanFactory().getBeanDefinition("dependsOnDataSourceInitialization").getDependsOn()) - .contains("initializer"); + assertThat(context.getBeanFactory().getBeanDefinition("factoryMethodDependsOnDatabaseInitialization") + .getDependsOn()).contains("initializer"); + assertThat(context.getBeanFactory().getBeanDefinition("beanClassDependsOnDatabaseInitialization") + .getDependsOn()).contains("initializer"); } } @@ -58,10 +60,15 @@ class AbstractDataSourceInitializerDependencyConfigurationTests { @Bean @DependsOnDatabaseInitialization - String dependsOnDataSourceInitialization() { + String factoryMethodDependsOnDatabaseInitialization() { return "test"; } + @Bean + DatabaseInitializationDependent beanClassDependsOnDatabaseInitialization() { + return new DatabaseInitializationDependent(); + } + @Bean AbstractDataSourceInitializer initializer(DataSource dataSource, ResourceLoader resourceLoader) { return new AbstractDataSourceInitializer(dataSource, resourceLoader) { @@ -81,4 +88,9 @@ class AbstractDataSourceInitializerDependencyConfigurationTests { } + @DependsOnDatabaseInitialization + static class DatabaseInitializationDependent { + + } + }