Spring解析之finishBeanFactoryInitialization即初始化单例bean

本文深入解析Spring核心方法finishBeanFactoryInitialization,该方法负责初始化所有单例bean。文中详细介绍了从finishBeanFactoryInitialization到getBean(),再到createBean()的调用过程,探讨了Spring如何解决循环依赖问题,以及单例bean初始化的三个核心流程:createBeanInstance、populateBean和initializeBean。文章适合对Spring感兴趣的开发者阅读。
摘要由CSDN通过智能技术生成

今天解读Spring核心方法refresh()中最最重要的一个方法finishBeanFactoryInitialization()方法,该方法负责初始化所有的单例bean。

finishBeanFactoryInitialization()方法位于refresh()中下标为8的位置。

到目前为止,应该说是是 BeanFactory 已经创建完成,并且所有的实现了 BeanFactoryPostProcessor 接口的 Bean 都已经初始化并且其中的 postProcessBeanFactory(factory) 方法已经得到回调执行了。而且 Spring 已经“手动”注册了一些特殊的 Bean,如 environment、systemProperties 等。

剩下的就是初始化 singleton beans 了,大多数我们的业务中都是单例bean,就像我们写的@Controller、@Service的类(没有设置懒加载的)都是在这个地方初始化,以供我们使用,如果没有设置懒加载,那么 Spring 会在接下来初始化所有的 singleton beans。

我们先看一下refresh()的源码,大概看下finishBeanFactoryInitialization(beanFactory)所处的位置。

@Overridepublic void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// Prepare this context for refreshing.//1、刷新前的准备prepareRefresh();// Tell the subclass to refresh the internal bean factory.//2、将会初始化 BeanFactory、加载 Bean、注册 BeanConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();// Prepare the bean factory for use in this context.//3、设置 BeanFactory 的类加载器,添加几个 BeanPostProcessor,手动注册几个特殊的 beanprepareBeanFactory(beanFactory);try {//4、模板方法// Allows post-processing of the bean factory in context subclasses.postProcessBeanFactory(beanFactory);// Invoke factory processors registered as beans in the context.//执行BeanFactory后置处理器invokeBeanFactoryPostProcessors(beanFactory);// 5、Register bean processors that intercept bean creation.//注册bean后置处理器registerBeanPostProcessors(beanFactory);// Initialize message source for this context.//国际化initMessageSource();// Initialize event multicaster for this context.initApplicationEventMulticaster();// Initialize other special beans in specific context subclasses.//6、模板方法--springboot实现了这个方法onRefresh();// Check for listener beans and register them.//7、注册监听器registerListeners();// Instantiate all remaining (non-lazy-init) singletons.//8、完成bean工厂的初始化**方法重要**********************************************finishBeanFactoryInitialization(beanFactory);//9、 Last step: publish corresponding event.finishRefresh();}1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.

我们深入finishBeanFactoryInitialization(beanFactory)中,里面的调用线路错综复杂,还望读者可以做好心理准备。

/** * 负责单例bean的初始化 * Finish the initialization of this context's bean factory, * initializing all remaining singleton beans. */protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {// Initialize conversion service for this context.//最先初始化名字为 conversionService的类,conversionService类 它用来将前端传过来的参数和后端的 controller 方法上的参数进行绑定的时候用//尤其是用于非基础类型的转换if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {beanFactory.setConversionService(//初始化在getBean()方法中实现beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));}// Register a default embedded value resolver if no bean post-processor// (such as a PropertyPlaceholderConfigurer bean) registered any before:// at this point, primarily for resolution in annotation attribute values.if (!beanFactory.hasEmbeddedValueResolver()) {beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));}// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.// 先初始化 LoadTimeWeaverAware 类型的 Bean aop相关注:大概有个印象,以后解析aop会和它串起来。String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);for (String weaverAwareName : weaverAwareNames) {getBean(weaverAwareName);}// Stop using the temporary ClassLoader for type matching.beanFactory.setTempClassLoader(null);// Allow for caching all bean definition metadata, not expecting further changes.//freeze的单词意思是冻结,这个时候已经开始预初始化, bean 定义解析、加载、注册先停止beanFactory.freezeConfiguration();// Instantiate all remaining (non-lazy-init) singletons.//开始初始化beanFactory.preInstantiateSingletons();}1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.

该方法是判断bean的一系列是不是属于某个类型的bean,如果是就调用getBean()方法,如果不是,就调用beanFactory.preInstantiateSingletons()进行初始化,我们先把getBean()放一放,重点看一看beanFactory.preInstantiateSingletons()方法。

@Overridepublic void preInstantiateSingletons() throws BeansException {if (logger.isTraceEnabled()) {logger.trace("Pre-instantiating singletons in " + this);}// 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.// this.beanDefinitionNames 保存了所有的 beanNamesList<String> beanNames = new ArrayList<>(this.beanDefinitionNames);// Trigger initialization of all non-lazy singleton beans... 下面这个循环,触发所有的非懒加载的 singleton beans 的初始化操作for (String beanName : beanNames) {RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);// 非抽象、非懒加载的 singletons。如果配置了 'abstract = true',那是不需要初始化的if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {// 处理 FactoryBean (负责初始化工厂的bean)if (isFactoryBean(beanName)) {// FactoryBean 的话,在 beanName 前面加上 ‘&’ 符号//此处调用getBean()方法Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);if (bean instanceof FactoryBean) {FactoryBean<?> factory = (FactoryBean<?>) bean;// 判断当前 FactoryBean 是否是 SmartFactoryBean 的实现boolean isEagerInit;if (System.getSecurityManager() != 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 {// 对于普通的 Bean,只要调用 getBean(beanName) 这个方法就可以进行初始化了getBean(beanName);}}}// Trigger post-initialization callback for all applicable beans...// 到这里说明所有的非懒加载的 singleton beans 已经完成了初始化// 如果我们定义的 bean 是实现了 SmartInitializingSingleton 接口的,那么在这里得到回调//如果你想在单例bean初始化后做一些事 那就实现该接口for (String beanName : beanNames) {Object singletonInstance = getSingleton(beanName);if (singletonInstance instanceof SmartInitializingSingleton) {SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;if (System.getSecurityManager() != null) {AccessController.doPrivileged((PrivilegedAction<Object>) () -> {smartSingleton.afterSingletonsInstantiated();return null;}, getAccessControlContext());}else {smartSingleton.afterSingletonsInstantiated();}}}}1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.

preInstantiateSingletons()方法的主要任务是进行初始化的,在初始化前同样是一系列判断,如,是否是懒加载的,是否是一个factorybean(一个特别的bean,负责工厂创建的bean),最后调用getBean()方法。

其中有个插曲是否实现了SmartInitializingSingleton接口,将接口让你可以在bean初始化后做一些事,我们写一个

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值