Skip to content

Commit

Permalink
Prevent repeat configuration of DB init bean dependencies
Browse files Browse the repository at this point in the history
Fixes gh-33374
  • Loading branch information
wilkinsona committed Nov 30, 2022
1 parent e1efdad commit 276b288
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Expand Up @@ -26,6 +26,7 @@
import java.util.Map;
import java.util.Set;

import org.springframework.aot.AotDetector;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
Expand Down Expand Up @@ -95,6 +96,9 @@ public int getOrder() {

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
if (AotDetector.useGeneratedArtifacts()) {
return;
}
InitializerBeanNames initializerBeanNames = detectInitializerBeanNames(beanFactory);
if (initializerBeanNames.isEmpty()) {
return;
Expand Down
Expand Up @@ -169,6 +169,35 @@ void whenDependenciesAreConfiguredDetectedDatabaseInitializersAreInitializedInCo
});
}

@Test
void whenInAnAotProcessedContextDependsOnDatabaseInitializationPostProcessorDoesNothing() {
withAotEnabled(() -> {
BeanDefinition alpha = BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition();
BeanDefinition bravo = BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition();
performDetection(Arrays.asList(MockDatabaseInitializerDetector.class,
MockedDependsOnDatabaseInitializationDetector.class), (context) -> {
context.registerBeanDefinition("alpha", alpha);
context.registerBeanDefinition("bravo", bravo);
context.register(DependencyConfigurerConfiguration.class);
context.refresh();
assertThat(alpha.getAttribute(DatabaseInitializerDetector.class.getName())).isNull();
assertThat(bravo.getAttribute(DatabaseInitializerDetector.class.getName())).isNull();
then(MockDatabaseInitializerDetector.instance).shouldHaveNoInteractions();
assertThat(bravo.getDependsOn()).isNull();
});
});
}

private void withAotEnabled(Runnable action) {
System.setProperty("spring.aot.enabled", "true");
try {
action.run();
}
finally {
System.clearProperty("spring.aot.enabled");
}
}

private void performDetection(Collection<Class<?>> detectors,
Consumer<AnnotationConfigApplicationContext> contextCallback) {
DetectorSpringFactoriesClassLoader detectorSpringFactories = new DetectorSpringFactoriesClassLoader(this.temp);
Expand Down

0 comments on commit 276b288

Please sign in to comment.