spring复习:(50)@Configuration注解配置的singleton的bean是什么时候被创建出来并缓存到容器的?

一、主类:
spring复习:(50)@Configuration注解配置的singleton的bean是什么时候被创建出来并缓存到容器的?_第1张图片
二、配置类:
spring复习:(50)@Configuration注解配置的singleton的bean是什么时候被创建出来并缓存到容器的?_第2张图片
三、singleton bean的创建流程
运行到context.refresh();
spring复习:(50)@Configuration注解配置的singleton的bean是什么时候被创建出来并缓存到容器的?_第3张图片
进入refresh方法:
spring复习:(50)@Configuration注解配置的singleton的bean是什么时候被创建出来并缓存到容器的?_第4张图片
向下运行到红线位置时:
spring复习:(50)@Configuration注解配置的singleton的bean是什么时候被创建出来并缓存到容器的?_第5张图片
会实例化所有的singleton bean.进入finisheBeanFactoryInitialization方法:
spring复习:(50)@Configuration注解配置的singleton的bean是什么时候被创建出来并缓存到容器的?_第6张图片
向下拖动代码,可以看到beanFactory.preInstantiateSingletons();
spring复习:(50)@Configuration注解配置的singleton的bean是什么时候被创建出来并缓存到容器的?_第7张图片
进入preInstantiateSingletons方法:

	public 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.
		List beanNames = new ArrayList<>(this.beanDefinitionNames);

		// Trigger initialization of all non-lazy singleton beans...
		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 (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
							isEagerInit = AccessController.doPrivileged((PrivilegedAction)
											((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 applicable beans...
		for (String beanName : beanNames) {
			Object singletonInstance = getSingleton(beanName);
			if (singletonInstance instanceof SmartInitializingSingleton) {
				final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
				if (System.getSecurityManager() != null) {
					AccessController.doPrivileged((PrivilegedAction) () -> {
						smartSingleton.afterSingletonsInstantiated();
						return null;
					}, getAccessControlContext());
				}
				else {
					smartSingleton.afterSingletonsInstantiated();
				}
			}
		}
	}
 
  

可以看到实例化singleton bean的代码:
spring复习:(50)@Configuration注解配置的singleton的bean是什么时候被创建出来并缓存到容器的?_第8张图片
可以看到对非abstract、singleton的、非懒加载的bean,则会调用getBean方法创建bean并缓存(getBean方法的流程不再详述)
spring复习:(50)@Configuration注解配置的singleton的bean是什么时候被创建出来并缓存到容器的?_第9张图片

你可能感兴趣的:(Spring,spring,单例模式,缓存)