mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Prevent repeat configuration of DB init bean dependencies
Fixes gh-33374
This commit is contained in:
parent
e1efdadfe3
commit
276b288891
@ -26,6 +26,7 @@ import java.util.List;
|
||||
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;
|
||||
@ -95,6 +96,9 @@ public class DatabaseInitializationDependencyConfigurer implements ImportBeanDef
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
|
||||
if (AotDetector.useGeneratedArtifacts()) {
|
||||
return;
|
||||
}
|
||||
InitializerBeanNames initializerBeanNames = detectInitializerBeanNames(beanFactory);
|
||||
if (initializerBeanNames.isEmpty()) {
|
||||
return;
|
||||
|
@ -169,6 +169,35 @@ class DatabaseInitializationDependencyConfigurerTests {
|
||||
});
|
||||
}
|
||||
|
||||
@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);
|
||||
|
Loading…
Reference in New Issue
Block a user