Skip to content

Commit

Permalink
Add BeanFactoryInitializer callback before preInstantiateSingletons
Browse files Browse the repository at this point in the history
Closes gh-32836
  • Loading branch information
jhoeller committed Jun 4, 2024
1 parent f10caf6 commit 28eb9ae
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.beans.factory;

/**
* Callback interface for initializing a Spring {@link ListableBeanFactory}
* prior to entering the singleton pre-instantiation phase. Can be used to
* trigger early initialization of specific beans before regular singletons.
*
* <p>Can be programmatically applied to a {@code ListableBeanFactory} instance.
* In an {@code ApplicationContext}, beans of type {@code BeanFactoryInitializer}
* will be autodetected and automatically applied to the underlying bean factory.
*
* @author Juergen Hoeller
* @since 6.2
* @param <F> the bean factory type
* @see org.springframework.beans.factory.config.ConfigurableListableBeanFactory#preInstantiateSingletons()
*/
public interface BeanFactoryInitializer<F extends ListableBeanFactory> {

/**
* Initialize the given bean factory.
* @param beanFactory the bean factory to bootstrap
*/
void initialize(F beanFactory);

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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;
Expand Down Expand Up @@ -932,6 +933,7 @@ protected void registerListeners() {
* 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) &&
Expand All @@ -954,6 +956,12 @@ protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory b
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,55 +19,30 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.BeanFactoryInitializer;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.context.weaving.LoadTimeWeaverAware;
import org.springframework.instrument.classloading.LoadTimeWeaver;
import org.springframework.lang.Nullable;
import org.springframework.test.context.DynamicPropertySource;

/**
* Internal component which eagerly initializes beans created by {@code @Bean}
* factory methods annotated with {@link DynamicPropertySource @DynamicPropertySource}.
*
* <p>This class implements {@link LoadTimeWeaverAware} since doing so is
* currently the only way to have a component eagerly initialized before the
* {@code ConfigurableListableBeanFactory.preInstantiateSingletons()} phase.
*
* @author Sam Brannen
* @since 6.2
*/
class DynamicPropertySourceBeanInitializer implements BeanFactoryAware, InitializingBean, LoadTimeWeaverAware {
class DynamicPropertySourceBeanInitializer implements BeanFactoryInitializer<ListableBeanFactory> {

private static final Log logger = LogFactory.getLog(DynamicPropertySourceBeanInitializer.class);

@Nullable
private BeanFactory beanFactory;


@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}

@Override
public void afterPropertiesSet() {
if (!(this.beanFactory instanceof ListableBeanFactory lbf)) {
throw new IllegalStateException("BeanFactory must be set and must be a ListableBeanFactory");
}
for (String name : lbf.getBeanNamesForAnnotation(DynamicPropertySource.class)) {
public void initialize(ListableBeanFactory beanFactory) {
for (String name : beanFactory.getBeanNamesForAnnotation(DynamicPropertySource.class)) {
if (logger.isDebugEnabled()) {
logger.debug("Eagerly initializing @DynamicPropertySource bean '%s'".formatted(name));
}
this.beanFactory.getBean(name);
beanFactory.getBean(name);
}
}

@Override
public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver) {
// no-op
}

}

0 comments on commit 28eb9ae

Please sign in to comment.