Skip to content

Commit cb1ee20

Browse files
committed
Exclude SharedMetadataReaderFactoryContextInitializer from AOT contexts
SharedMetadataReaderFactoryContextInitializer exposes an additional bean post processor that is only relevant when parsing the bean factory, auto-configurations in particular. Given that this does not happen in an AOT-optimized context, this commit excludes the bean and makes sure the initializer does not do anything at runtime. Closes gh-33216
1 parent 55ba5a5 commit cb1ee20

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SharedMetadataReaderFactoryContextInitializer.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
import java.util.function.Supplier;
2020

21+
import org.springframework.aot.AotDetector;
2122
import org.springframework.beans.BeansException;
2223
import org.springframework.beans.MutablePropertyValues;
2324
import org.springframework.beans.factory.BeanClassLoaderAware;
2425
import org.springframework.beans.factory.FactoryBean;
2526
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
27+
import org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter;
2628
import org.springframework.beans.factory.config.BeanDefinition;
2729
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
2830
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -31,6 +33,7 @@
3133
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
3234
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3335
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
36+
import org.springframework.beans.factory.support.RegisteredBean;
3437
import org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory;
3538
import org.springframework.context.ApplicationContextInitializer;
3639
import org.springframework.context.ApplicationListener;
@@ -51,14 +54,17 @@
5154
* @author Phillip Webb
5255
* @author Dave Syer
5356
*/
54-
class SharedMetadataReaderFactoryContextInitializer
55-
implements ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {
57+
class SharedMetadataReaderFactoryContextInitializer implements
58+
ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered, BeanRegistrationExcludeFilter {
5659

5760
public static final String BEAN_NAME = "org.springframework.boot.autoconfigure."
5861
+ "internalCachingMetadataReaderFactory";
5962

6063
@Override
6164
public void initialize(ConfigurableApplicationContext applicationContext) {
65+
if (AotDetector.useGeneratedArtifacts()) {
66+
return;
67+
}
6268
BeanFactoryPostProcessor postProcessor = new CachingMetadataReaderFactoryPostProcessor(applicationContext);
6369
applicationContext.addBeanFactoryPostProcessor(postProcessor);
6470
}
@@ -68,6 +74,11 @@ public int getOrder() {
6874
return 0;
6975
}
7076

77+
@Override
78+
public boolean isExcludedFromAotProcessing(RegisteredBean registeredBean) {
79+
return BEAN_NAME.equals(registeredBean.getBeanName());
80+
}
81+
7182
/**
7283
* {@link BeanDefinitionRegistryPostProcessor} to register the
7384
* {@link CachingMetadataReaderFactory} and configure the

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring/aot.factories

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingP
77

88
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\
99
org.springframework.boot.autoconfigure.flyway.ResourceProviderCustomizerBeanRegistrationAotProcessor
10+
11+
org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter=\
12+
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer

0 commit comments

Comments
 (0)