Skip to content

Commit 2a89b11

Browse files
committed
Exclude SmartInitializingSingletones from lazy initialization
Fixes gh-26470
1 parent 5edb788 commit 2a89b11

File tree

2 files changed

+106
-3
lines changed

2 files changed

+106
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/LazyInitializationBeanFactoryPostProcessor.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package org.springframework.boot;
1818

19+
import java.util.ArrayList;
1920
import java.util.Collection;
2021

2122
import org.springframework.beans.BeansException;
2223
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
24+
import org.springframework.beans.factory.SmartInitializingSingleton;
2325
import org.springframework.beans.factory.config.BeanDefinition;
2426
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
2527
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -30,6 +32,11 @@
3032
* {@link BeanFactoryPostProcessor} to set lazy-init on bean definitions that are not
3133
* {@link LazyInitializationExcludeFilter excluded} and have not already had a value
3234
* explicitly set.
35+
* <p>
36+
* Note that {@link SmartInitializingSingleton SmartInitializingSingletons} are
37+
* automatically excluded from lazy initialization to ensure that their
38+
* {@link SmartInitializingSingleton#afterSingletonsInstantiated() callback method} is
39+
* invoked.
3340
*
3441
* @author Andy Wilkinson
3542
* @author Madhura Bhave
@@ -42,9 +49,7 @@ public final class LazyInitializationBeanFactoryPostProcessor implements BeanFac
4249

4350
@Override
4451
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
45-
// Take care not to force the eager init of factory beans when getting filters
46-
Collection<LazyInitializationExcludeFilter> filters = beanFactory
47-
.getBeansOfType(LazyInitializationExcludeFilter.class, false, false).values();
52+
Collection<LazyInitializationExcludeFilter> filters = getFilters(beanFactory);
4853
for (String beanName : beanFactory.getBeanDefinitionNames()) {
4954
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
5055
if (beanDefinition instanceof AbstractBeanDefinition) {
@@ -53,6 +58,14 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
5358
}
5459
}
5560

61+
private Collection<LazyInitializationExcludeFilter> getFilters(ConfigurableListableBeanFactory beanFactory) {
62+
// Take care not to force the eager init of factory beans when getting filters
63+
ArrayList<LazyInitializationExcludeFilter> filters = new ArrayList<>(
64+
beanFactory.getBeansOfType(LazyInitializationExcludeFilter.class, false, false).values());
65+
filters.add(LazyInitializationExcludeFilter.forBeanTypes(SmartInitializingSingleton.class));
66+
return filters;
67+
}
68+
5669
private void postProcess(ConfigurableListableBeanFactory beanFactory,
5770
Collection<LazyInitializationExcludeFilter> filters, String beanName,
5871
AbstractBeanDefinition beanDefinition) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.beans.factory.SmartInitializingSingleton;
25+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for {@link LazyInitializationBeanFactoryPostProcessor}.
31+
*
32+
* @author Andy Wilkinson
33+
*/
34+
class LazyInitializationBeanFactoryPostProcessorTests {
35+
36+
@Test
37+
void whenLazyInitializationIsEnabledThenNormalBeansAreNotInitializedUntilRequired() {
38+
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
39+
context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
40+
context.register(BeanState.class, ExampleBean.class);
41+
context.refresh();
42+
BeanState beanState = context.getBean(BeanState.class);
43+
assertThat(beanState.initializedBeans).isEmpty();
44+
context.getBean(ExampleBean.class);
45+
assertThat(beanState.initializedBeans).containsExactly(ExampleBean.class);
46+
}
47+
}
48+
49+
@Test
50+
void whenLazyInitializationIsEnabledThenSmartInitializingSingletonsAreInitializedDuringRefresh() {
51+
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
52+
context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
53+
context.register(BeanState.class, ExampleSmartInitializingSingleton.class);
54+
context.refresh();
55+
BeanState beanState = context.getBean(BeanState.class);
56+
assertThat(beanState.initializedBeans).containsExactly(ExampleSmartInitializingSingleton.class);
57+
assertThat(context.getBean(ExampleSmartInitializingSingleton.class).callbackInvoked).isTrue();
58+
}
59+
}
60+
61+
static class ExampleBean {
62+
63+
ExampleBean(BeanState beanState) {
64+
beanState.initializedBeans.add(getClass());
65+
}
66+
67+
}
68+
69+
static class ExampleSmartInitializingSingleton implements SmartInitializingSingleton {
70+
71+
private boolean callbackInvoked;
72+
73+
ExampleSmartInitializingSingleton(BeanState beanState) {
74+
beanState.initializedBeans.add(getClass());
75+
}
76+
77+
@Override
78+
public void afterSingletonsInstantiated() {
79+
this.callbackInvoked = true;
80+
}
81+
82+
}
83+
84+
static class BeanState {
85+
86+
private final List<Class<?>> initializedBeans = new ArrayList<>();
87+
88+
}
89+
90+
}

0 commit comments

Comments
 (0)