SpringBoot中的SmartInitializingSingleton接⼝及其使⽤
⼀、使⽤场景
实现SmartInitializingSingleton的接⼝后,当所有单例 bean 都初始化完成以后, Spring的IOC容器会回调该接⼝
的 afterSingletonsInstantiated()⽅法。springboot是啥
主要应⽤场合就是在所有单例 bean 创建完成之后,可以在该回调中做⼀些事情,例如:
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.stereotype.Component;
@Component
public class MyRegister implements SmartInitializingSingleton {
private ListableBeanFactory beanFactory;
public MyRegister(ListableBeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
@Override
public void afterSingletonsInstantiated() {
String[] beanNames = BeanNamesForType(IPerson.class);
for (String s : beanNames) {
System.out.println(s);
}
}
}
⼆、原理分析
在Spring容器启动时,通过SpringApplication.run()--&freshContext()--&fresh() --&fresh(),在这个refresh()函数过程中会调⽤ finishBeanFactoryInitialization(beanFactory)来提前初始化单例bean,具体⽅法是调⽤beanFactory.preInstantiateSingletons(),⽽这⾥的beanFactory实例实际为接
⼝ ConfigurableListableBeanFactory 的实现类DefaultListableBeanFactory的实例对象。
查看到DefaultListableBeanFactory.preInstantiateSingletons()的源码如下:
@Override
public void preInstantiateSingletons() throws BeansException {
if (logger.isTraceEnabled()) {
}
/
/ Iterate over a copy to allow for init methods which in turn register new bean definitions.
// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
// Trigger initialization of all non-lazy
for (String beanName : beanNames) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
if (isFactoryBean(beanName)) {
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
if (bean instanceof FactoryBean) {
final FactoryBean<?> factory = (FactoryBean<?>) bean;
boolean isEagerInit;
if (SecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
((SmartFactoryBean<?>) factory)::isEagerInit,
getAccessControlContext());
}
else {
isEagerInit = (factory instanceof SmartFactoryBean &&
((SmartFactoryBean<?>) factory).isEagerInit());
}
if (isEagerInit) {
getBean(beanName);
}
}
}
else {
getBean(beanName);
}
}
}
// Trigger post-initialization callback for all
for (String beanName : beanNames) {
Object singletonInstance = getSingleton(beanName);
if (singletonInstance instanceof SmartInitializingSingleton) {
final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
if (SecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
smartSingleton.afterSingletonsInstantiated();
return null;
}, getAccessControlContext());
}
else {
smartSingleton.afterSingletonsInstantiated();
}
}
}
}
从源码分析中得知,当Spring将所有单例 bean 都实例初始化完成以后,如果存在实现SmartInitializingSingleton接⼝的bean,那么Spring还会调⽤到该bean的afterSingletonsInstantiated()⽅法。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论