Find annotation without initializing factory beans

Closes gh-28977
pull/29161/head
Andy Wilkinson 3 years ago committed by Stephane Nicoll
parent 5d0206320a
commit 2fec06ac7e

@ -33,7 +33,7 @@ class AnnotationDependsOnDatabaseInitializationDetector implements DependsOnData
public Set<String> detect(ConfigurableListableBeanFactory beanFactory) {
Set<String> 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);
}
}

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

Loading…
Cancel
Save