Add BeanFactoryInitializer callback before preInstantiateSingletons

Closes gh-32836
This commit is contained in:
Juergen Hoeller
2024-06-04 22:50:42 +02:00
parent f10caf6aa6
commit 28eb9aebcf
4 changed files with 56 additions and 32 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -44,7 +44,7 @@ public interface ApplicationContextInitializer<C extends ConfigurableApplication
/**
* Initialize the given application context.
* @param applicationContext the application to configure
* @param applicationContext the application context to bootstrap
*/
void initialize(C applicationContext);

View File

@@ -37,6 +37,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.CachedIntrospectionResults;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryInitializer;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
@@ -932,6 +933,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
* Finish the initialization of this context's bean factory,
* initializing all remaining singleton beans.
*/
@SuppressWarnings("unchecked")
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
// Initialize bootstrap executor for this context.
if (beanFactory.containsBean(BOOTSTRAP_EXECUTOR_BEAN_NAME) &&
@@ -954,6 +956,12 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
// Call BeanFactoryInitializer beans early to allow for initializing specific other beans early.
String[] initializerNames = beanFactory.getBeanNamesForType(BeanFactoryInitializer.class, false, false);
for (String initializerName : initializerNames) {
beanFactory.getBean(initializerName, BeanFactoryInitializer.class).initialize(beanFactory);
}
// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {