Spring中Bean的生命周期以及三级缓存介绍

Bean的生命周期以及三级缓存介绍

  • 简述
    • 测试代码编写
    • 创建IOC容器(Bean创建)
      • 1.refresh()方法
      • 2.finishBeanFactoryInitialization(beanFactory)方法
      • 3. beanFactory.preInstantiateSingletons()
      • 4.getBean(beanName)
      • 5.doGetBean(name, null, null, false)
      • 6.getSingleton(String beanName)
      • 7.getSingleton(beanName,...)
      • 8.createBean(beanName, mbd, args)
      • 9.doCreateBean(beanName, mbdToUse, args)
      • 10.createBeanInstance(beanName, mbd, args)
      • 11.addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean))
      • 12.populateBean(beanName, mbd, instanceWrapper)
      • 13.继续执行getSingleton(beanName,....)的方法
      • 创建总结
    • Bean的使用
    • Bean的销毁
    • 总结

简述

spring中的bean的生命周期我们都知道有创建、使用、销毁这三个步骤,那么在源码中是怎么做的呢,以及spring中三级缓存是如何使用的呢(三级缓存也是解决循环依赖问题的关键)

测试代码编写

由于我搭建了源码的阅读环境,所以我就直接让项目之间关联就行了,
Spring中Bean的生命周期以及三级缓存介绍_第1张图片
此处不做详解
InstantC.java

import org.springframework.stereotype.Component;

@Component
public class InstantC {
	public InstantC(){
		System.out.println("ic的构造器被调用了");
	}
	public void print() {
		System.out.println("ic的bean使用");
	}
}

MainConfigure.java

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource(locations="classpath:spring.xml")
@ComponentScan(basePackages= {"com.zk.test"})
public class MainConfigure {

}

Main .java

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//上下文 创建IOC容器
		AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(MainConfigure.class);
		//获取bean  去容器的缓存中直接拿
		InstantC instantC=(InstantC) ctx.getBean("ic");
		ctx.close();
	}

}

spring.xml



	

以上就是测试的代码,下面来分析一下Main.java代码,需要结合源码来看

创建IOC容器(Bean创建)

//上下文 创建IOC容器
AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(MainConfigure.class);

要想使用bean,首先要创建bean,并将bean保存到相应的容器之中,然后获取的时候直接从容器之中获取就行,我们可以断点调试一下,然后就会发现走了一下几个关键性的方法
Spring中Bean的生命周期以及三级缓存介绍_第2张图片
流程图画的不美观,从上面可以看出bean创建的整个流程,
简单叙述一下流程

  1. 创建上下文实例(AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(MainConfigure.class);)的时候,会调用refresh方法刷新上下文
  2. 调用finishBeanFactoryInitialization(beanFactory);方法实例化剩余的bean;(为什么是剩余的一会可以看下代码)
  3. 执行beanFactory.preInstantiateSingletons();bean的创建就在这个方法里面
  4. 执行getBean(beanName)去获取bean
  5. 然后执行doGetBean(name, null, null, false);方法,这一块在源码有个注释是beanfactory接口的实现
  6. 执行getSingleton(beanName)方法,尝试从缓存中获取bean,由于是创建所以是不存在bean的
  7. 中间处理省略,标记玩bean正在被创建,然后执行 getSingleton(beanName,...)方法,这个在源码中是
sharedInstance = getSingleton(beanName, () -> {
						try {
							//正式开始创建bean实例
							return createBean(beanName, mbd, args);
						}
						catch (BeansException ex) {
							// Explicitly remove instance from singleton cache: It might have been put there
							// eagerly by the creation process, to allow for circular reference resolution.
							// Also remove any beans that received a temporary reference to the bean.
							destroySingleton(beanName);
							throw ex;
						}
					});

在此方法中,是先执行lambel函数的方法,然后继续往下执行,下面详细说

  1. 执行createBean(beanName, mbd, args);去创建bean
  2. 判断处理完毕之后 ,执行doCreateBean(beanName, mbdToUse, args);继续创建bean
  3. 执行createBeanInstance(beanName, mbd, args); 方法,去调用bean的构造器方法,创建一个早期对象,这个对象的属性还未被赋值
  4. 执行 addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));将早期对象加入三级缓存,这是为了解决循环依赖
  5. 执行populateBean(beanName, mbd, instanceWrapper);方法,用属性值填充bean定义的实例,如果是在这一步存在依赖,就会去创建依赖的bean
  6. 执行initializeBean(beanName, exposedObject, mbd);方法初始化bean
  7. lambel函数执行结束,继续执行第七步的方法,将二三级缓存的数据清除
  8. 结束

1.refresh()方法

了解的话可以查看中文注释

@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.
			//准备刷新此上下文。
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			//告诉子类刷新内部bean工厂。
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			//准备bean工厂以供在此上下文中使用。
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				//允许在上下文子类中对bean工厂进行后处理。
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				//调用上下文中注册为bean的工厂处理器。
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				//注册拦截bean创建的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.
				//初始化特定上下文子类中的其他特殊bean。
				onRefresh();

				// Check for listener beans and register them.
				//检查侦听器bean并注册它们。
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				//实例化所有剩余的(非延迟初始化)单例 明显创建我们自定义的bean是在这一步
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				//最后一步:发布对应的事件
				finishRefresh();
			}

			catch (BeansException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}

				// Destroy already created singletons to avoid dangling resources.
				//销毁已经创建的单例以避免资源悬空。
				destroyBeans();

				// Reset 'active' flag.
				//重置“活动”标志
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}

			finally {
				// Reset common introspection caches in Spring's core, since we
				// might not ever need metadata for singleton beans anymore...
				//在spring的核心中重置常见的内省缓存,因为我们可能不再需要singleton bean的元数据了……
				resetCommonCaches();
			}
		}
	}

这个方法在注释里面各个部分都说的很清楚,我们只需要看finishBeanFactoryInitialization(beanFactory);这个方法就行了

2.finishBeanFactoryInitialization(beanFactory)方法

源码如下

/**
	 * 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.
		//初始化此上下文的转换服务。
		if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
				beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
			beanFactory.setConversionService(
					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.
		//如果在此之前没有任何bean后处理器(如propertyplaceholderconfigurerbean)注册,
		//则注册默认的嵌入式值解析器:
		//此时,主要是为了在注释属性值中进行解析
		if (!beanFactory.hasEmbeddedValueResolver()) {
			beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
		}

		// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
		//尽早初始化loadTimeWeaveraware bean,以便尽早注册其转换器。
		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.
		//允许缓存所有bean定义元数据,不需要进一步更改。
		beanFactory.freezeConfiguration();

		// Instantiate all remaining (non-lazy-init) singletons.
		//实例化所有剩余的(非延迟初始化)单例 很明显这一步是创建的入口
		beanFactory.preInstantiateSingletons();
	}

3. beanFactory.preInstantiateSingletons()

源码如下

	@Override
	public void preInstantiateSingletons() throws BeansException {
		if (logger.isDebugEnabled()) {
			logger.debug("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<String> beanNames = new ArrayList<>(this.beanDefinitionNames);

		// Trigger initialization of all non-lazy singleton beans...
		//所有非惰性单例bean的触发器初始化…
		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<Boolean>)
											((SmartFactoryBean<?>) factory)::isEagerInit,
									getAccessControlContext());
						}
						else {
							isEagerInit = (factory instanceof SmartFactoryBean &&
									((SmartFactoryBean<?>) factory).isEagerInit());
						}
						if (isEagerInit) {
							getBean(beanName);
						}
					}
				}
				else {
					//获取bean
					getBean(beanName);
				}
			}
		}

		// Trigger post-initialization callback for all applicable beans...
		//为所有适用的bean触发初始化后回调…
		for (String beanName : beanNames) {
			Object singletonInstance = getSingleton(beanName);
			if (singletonInstance instanceof SmartInitializingSingleton) {
				final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
				if (System.getSecurityManager() != null) {
					AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
						smartSingleton.afterSingletonsInstantiated();
						return null;
					}, getAccessControlContext());
				}
				else {
					smartSingleton.afterSingletonsInstantiated();
				}
			}
		}
	}

这一部分,我们只需要跟着断点,然后就会走到getBean(beanName);这个方法,这个地方就是获取beanNames然后遍历去执行getBean(beanName);,这个地方我们需要用到条件断点,条件断点设置方式如下
1.先设置一个断点
Spring中Bean的生命周期以及三级缓存介绍_第3张图片
2.在这个断点上右键,选择最后一个breakpoint properties,然后开始设置条件
Spring中Bean的生命周期以及三级缓存介绍_第4张图片
这样条件断点就设置好了,满足条件才会进入这个断点。

4.getBean(beanName)

方法源码如下:

	//---------------------------------------------------------------------
	// Implementation of BeanFactory interface
	//---------------------------------------------------------------------
	//beanfactory接口的实现
	@Override
	public Object getBean(String name) throws BeansException {
		return doGetBean(name, null, null, false);
	}

这一步就是为了进入doGetBean(name, null, null, false)方法

5.doGetBean(name, null, null, false)

源码:

	/**
	 * Return an instance, which may be shared or independent, of the specified bean.
	 * @param name the name of the bean to retrieve
	 * @param requiredType the required type of the bean to retrieve
	 * @param args arguments to use when creating a bean instance using explicit arguments
	 * (only applied when creating a new instance as opposed to retrieving an existing one)
	 * @param typeCheckOnly whether the instance is obtained for a type check,
	 * not for actual use
	 * @return an instance of the bean
	 * @throws BeansException if the bean could not be created
	 */
	@SuppressWarnings("unchecked")
	protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
			@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
		//解析bean的别名i
		final String beanName = transformedBeanName(name);
		Object bean;

		// Eagerly check singleton cache for manually registered singletons.
		//首先尝试去缓存池中去获取对象,是第一次创建bean缓存池中没有对象
		Object sharedInstance = getSingleton(beanName);
		if (sharedInstance != null && args == null) {
			if (logger.isDebugEnabled()) {
				if (isSingletonCurrentlyInCreation(beanName)) {
					logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
							"' that is not fully initialized yet - a consequence of a circular reference");
				}
				else {
					logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
				}
			}
			bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
		}

		else {
			// Fail if we're already creating this bean instance:
			// We're assumably within a circular reference.
			//若出现循环依赖,判断bean是不是单利的不是就抛出异常
			if (isPrototypeCurrentlyInCreation(beanName)) {
				throw new BeanCurrentlyInCreationException(beanName);
			}

			// Check if bean definition exists in this factory.
			//当前容器是否有父工厂,有的话,就由父工厂加载
			BeanFactory parentBeanFactory = getParentBeanFactory();
			if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
				// Not found -> check parent.
				String nameToLookup = originalBeanName(name);
				if (parentBeanFactory instanceof AbstractBeanFactory) {
					return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
							nameToLookup, requiredType, args, typeCheckOnly);
				}
				else if (args != null) {
					// Delegation to parent with explicit args.
					return (T) parentBeanFactory.getBean(nameToLookup, args);
				}
				else {
					// No args -> delegate to standard getBean method.
					return parentBeanFactory.getBean(nameToLookup, requiredType);
				}
			}
			//标记为该bean正在创建
			if (!typeCheckOnly) {
				markBeanAsCreated(beanName);
			}

			try {
				//合并bean并定义 合并父bean里面的属性
				final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
				//检查bean定义是不是抽象的
				checkMergedBeanDefinition(mbd, beanName, args);

				// Guarantee initialization of beans that the current bean depends on.
				//bean加载顺序,必须dependsOn中的bean全部实例化之后 做依赖检查
				String[] dependsOn = mbd.getDependsOn();
				if (dependsOn != null) {
					for (String dep : dependsOn) {
						if (isDependent(beanName, dep)) {
							throw new BeanCreationException(mbd.getResourceDescription(), beanName,
									"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
						}
						registerDependentBean(dep, beanName);
						try {
							getBean(dep);
						}
						catch (NoSuchBeanDefinitionException ex) {
							throw new BeanCreationException(mbd.getResourceDescription(), beanName,
									"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
						}
					}
				}

				// Create bean instance.
				//创建bean实例
				if (mbd.isSingleton()) {
					//再次判断缓存池中是否有bean ,没有就在回调函数里面创建bean
					sharedInstance = getSingleton(beanName, () -> {
						try {
							//正式开始创建bean实例
							return createBean(beanName, mbd, args);
						}
						catch (BeansException ex) {
							// Explicitly remove instance from singleton cache: It might have been put there
							// eagerly by the creation process, to allow for circular reference resolution.
							// Also remove any beans that received a temporary reference to the bean.
							destroySingleton(beanName);
							throw ex;
						}
					});
					bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
				}

				else if (mbd.isPrototype()) {
					// It's a prototype -> create a new instance.
					Object prototypeInstance = null;
					try {
						beforePrototypeCreation(beanName);
						prototypeInstance = createBean(beanName, mbd, args);
					}
					finally {
						afterPrototypeCreation(beanName);
					}
					bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
				}

				else {
					String scopeName = mbd.getScope();
					final Scope scope = this.scopes.get(scopeName);
					if (scope == null) {
						throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
					}
					try {
						Object scopedInstance = scope.get(beanName, () -> {
							beforePrototypeCreation(beanName);
							try {
								return createBean(beanName, mbd, args);
							}
							finally {
								afterPrototypeCreation(beanName);
							}
						});
						bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
					}
					catch (IllegalStateException ex) {
						throw new BeanCreationException(beanName,
								"Scope '" + scopeName + "' is not active for the current thread; consider " +
								"defining a scoped proxy for this bean if you intend to refer to it from a singleton",
								ex);
					}
				}
			}
			catch (BeansException ex) {
				cleanupAfterBeanCreationFailure(beanName);
				throw ex;
			}
		}

		// Check if required type matches the type of the actual bean instance.
		//检查所需类型是否与实际bean实例的类型匹配。
		if (requiredType != null && !requiredType.isInstance(bean)) {
			try {
				T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
				if (convertedBean == null) {
					throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
				}
				return convertedBean;
			}
			catch (TypeMismatchException ex) {
				if (logger.isDebugEnabled()) {
					logger.debug("Failed to convert bean '" + name + "' to required type '" +
							ClassUtils.getQualifiedName(requiredType) + "'", ex);
				}
				throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
			}
		}
		return (T) bean;
	}

这个里面主要的方法有
1.首先尝试去缓存池中去获取对象Object sharedInstance = getSingleton(beanName);
2.markBeanAsCreated(beanName);将该bean标记为正在创建
3.getSingleton(beanName,...);这个是再次判断缓存中是否存在bean,如果不存在该bean就会去执行创建bean的方法,以及将创建结束的bean从二级缓存移入一级缓存中
4.createBean(beanName, mbd, args);创建bean的方法
这里面要介绍一下dependsOn,dependsOn指的是要先进行实例化的bean,也就是有beanA和beanB,如果用注解在beanB上加上@DependsOn(beanA),那么在创建beanB之前一点更要先创建beanA,和依赖不同,依赖是指在创建beanB的过程中去创建beanA。

6.getSingleton(String beanName)

源码;

@Override
	@Nullable
	public Object getSingleton(String beanName) {
		return getSingleton(beanName, true);
	}
	/**
	 * Return the (raw) singleton object registered under the given name.
	 * 

Checks already instantiated singletons and also allows for an early * reference to a currently created singleton (resolving a circular reference). * @param beanName the name of the bean to look for * @param allowEarlyReference whether early references should be created or not * @return the registered singleton object, or {@code null} if none found */ @Nullable protected Object getSingleton(String beanName, boolean allowEarlyReference) { //从一级缓存获取 Object singletonObject = this.singletonObjects.get(beanName); //如果一级缓存中没有,并且已标记这个bean正在被定义 if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) { synchronized (this.singletonObjects) { //从二级缓存获取bean singletonObject = this.earlySingletonObjects.get(beanName); //如果二级缓存也拿不到 去三级缓存拿 if (singletonObject == null && allowEarlyReference) { //从三级缓存取值 ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName); if (singletonFactory != null) { singletonObject = singletonFactory.getObject(); //如果三级缓存存在值,将三级缓存中的缓存移除,加入二级缓存 this.earlySingletonObjects.put(beanName, singletonObject); this.singletonFactories.remove(beanName); } } } } return singletonObject; }

这一部分的解释都已经加在了注释上面,没有其他需要过多解释的地方

7.getSingleton(beanName,…)

使用的地方如下

//创建bean实例
if (mbd.isSingleton()) {
	//再次判断缓存池中是否有bean ,没有就在回调函数里面创建bean
	sharedInstance = getSingleton(beanName, () -> {
		try {
			//正式开始创建bean实例
			return createBean(beanName, mbd, args);
		}
		catch (BeansException ex) {
			// Explicitly remove instance from singleton cache: It might have been put there
			// eagerly by the creation process, to allow for circular reference resolution.
			// Also remove any beans that received a temporary reference to the bean.
			destroySingleton(beanName);
			throw ex;
		}
	});
	bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}

方法源码如下:

	public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
		Assert.notNull(beanName, "Bean name must not be null");
		synchronized (this.singletonObjects) {
			Object singletonObject = this.singletonObjects.get(beanName);
			if (singletonObject == null) {
				if (this.singletonsCurrentlyInDestruction) {
					throw new BeanCreationNotAllowedException(beanName,
							"Singleton bean creation not allowed while singletons of this factory are in destruction " +
							"(Do not request a bean from a BeanFactory in a destroy method implementation!)");
				}
				if (logger.isDebugEnabled()) {
					logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
				}
				//标识bean要被创建
				beforeSingletonCreation(beanName);
				boolean newSingleton = false;
				boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
				if (recordSuppressedExceptions) {
					this.suppressedExceptions = new LinkedHashSet<>();
				}
				try {
					//执行创建bean
					singletonObject = singletonFactory.getObject();
					newSingleton = true;
				}
				catch (IllegalStateException ex) {
					// Has the singleton object implicitly appeared in the meantime ->
					// if yes, proceed with it since the exception indicates that state.
					singletonObject = this.singletonObjects.get(beanName);
					if (singletonObject == null) {
						throw ex;
					}
				}
				catch (BeanCreationException ex) {
					if (recordSuppressedExceptions) {
						for (Exception suppressedException : this.suppressedExceptions) {
							ex.addRelatedCause(suppressedException);
						}
					}
					throw ex;
				}
				finally {
					if (recordSuppressedExceptions) {
						this.suppressedExceptions = null;
					}
					afterSingletonCreation(beanName);
				}
				if (newSingleton) {
					addSingleton(beanName, singletonObject);
				}
			}
			return singletonObject;
		}
	}

要关注的地方
1.有singletonFactory调用的地方,因为这是lambel函数被调用的地方;
2.addSingleton(beanName, singletonObject);这个地方是把二级缓存的内容加入一级缓存之中。

	/**
	 * Add the given singleton object to the singleton cache of this factory.
	 * 

To be called for eager registration of singletons. * @param beanName the name of the bean * @param singletonObject the singleton object */ protected void addSingleton(String beanName, Object singletonObject) { synchronized (this.singletonObjects) { this.singletonObjects.put(beanName, singletonObject); this.singletonFactories.remove(beanName); this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } }

先把流程写完,在下面再写各级缓存如果传值的。

8.createBean(beanName, mbd, args)

对于这个方法,我们不需要过多看,只需要关注下面的doCreateBean(beanName, mbdToUse, args);方法

try {
	//真正开始创建的地方
		Object beanInstance = doCreateBean(beanName, mbdToUse, args);
		if (logger.isDebugEnabled()) {
			logger.debug("Finished creating instance of bean '" + beanName + "'");
		}
		return beanInstance;
	}

9.doCreateBean(beanName, mbdToUse, args)

源码如下

	/**
	 * Actually create the specified bean. Pre-creation processing has already happened
	 * at this point, e.g. checking {@code postProcessBeforeInstantiation} callbacks.
	 * 

Differentiates between default bean instantiation, use of a * factory method, and autowiring a constructor. * @param beanName the name of the bean * @param mbd the merged bean definition for the bean * @param args explicit arguments to use for constructor or factory method invocation * @return a new instance of the bean * @throws BeanCreationException if the bean could not be created * @see #instantiateBean * @see #instantiateUsingFactoryMethod * @see #autowireConstructor */ protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args) throws BeanCreationException { // Instantiate the bean. //实例化bean BeanWrapper instanceWrapper = null; if (mbd.isSingleton()) { instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) { //调用bean的构造器方法进行实例化 属性还没有被赋值 早期对象 instanceWrapper = createBeanInstance(beanName, mbd, args); } final Object bean = instanceWrapper.getWrappedInstance(); Class<?> beanType = instanceWrapper.getWrappedClass(); if (beanType != NullBean.class) { mbd.resolvedTargetType = beanType; } // Allow post-processors to modify the merged bean definition. synchronized (mbd.postProcessingLock) { if (!mbd.postProcessed) { try { applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); } catch (Throwable ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Post-processing of merged bean definition failed", ex); } mbd.postProcessed = true; } } // Eagerly cache singletons to be able to resolve circular references // even when triggered by lifecycle interfaces like BeanFactoryAware. //这一块的意思是 将早期对象加入缓存中,以便解决循环依赖问题 boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && isSingletonCurrentlyInCreation(beanName)); if (earlySingletonExposure) { if (logger.isDebugEnabled()) { logger.debug("Eagerly caching bean '" + beanName + "' to allow for resolving potential circular references"); } //提前暴露早期对象 加入缓存 加入三级缓存 addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)); } // Initialize the bean instance. //初始化bean实例 Object exposedObject = bean; try { //用属性值填充bean定义的实例,如果是在这一步存在依赖,就会去创建依赖的bean populateBean(beanName, mbd, instanceWrapper); //初始化方法 exposedObject = initializeBean(beanName, exposedObject, mbd); } catch (Throwable ex) { if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) { throw (BeanCreationException) ex; } else { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex); } } if (earlySingletonExposure) { Object earlySingletonReference = getSingleton(beanName, false); if (earlySingletonReference != null) { if (exposedObject == bean) { exposedObject = earlySingletonReference; } else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) { String[] dependentBeans = getDependentBeans(beanName); Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length); for (String dependentBean : dependentBeans) { if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) { actualDependentBeans.add(dependentBean); } } if (!actualDependentBeans.isEmpty()) { throw new BeanCurrentlyInCreationException(beanName, "Bean with name '" + beanName + "' has been injected into other beans [" + StringUtils.collectionToCommaDelimitedString(actualDependentBeans) + "] in its raw version as part of a circular reference, but has eventually been " + "wrapped. This means that said other beans do not use the final version of the " + "bean. This is often the result of over-eager type matching - consider using " + "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example."); } } } } // Register bean as disposable. //将bean注册为一次性的。 try { registerDisposableBeanIfNecessary(beanName, bean, mbd); } catch (BeanDefinitionValidationException ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); } return exposedObject; }

这个方法里面需要重点关注
1.instanceWrapper = createBeanInstance(beanName, mbd, args);这是调用bean的构造器方法,创建早期对象,此时属性值还没有赋值,可以尝试在bean的构造器里面加上打印语句,执行到这一步之后就会输出
2.addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));提前暴露早期对象 加入缓存 加入三级缓存,这一步是解决非构造注入循环依赖的关键,因为如果a和b相互依赖,那么在创建a的时候,要给a的早期对象的属性赋值,他就会去创建bean b,然后创建b的时候,b又需要a,如果不把早期对象加入缓存,那么b就会在缓存中获取不到a,然后就会去创建a,然后就形成了死循环,所以这一步是把bean暴露出来,让循环依赖的bean可以使用
3.populateBean(beanName, mbd, instanceWrapper);这一步就是给早期对象赋属性值,如果这个bean依赖的有其他bean,那么也会在这个方法上赋值给它,如果依赖的bean获取不到,就会直接去创建这个依赖的bean,这个循环依赖的关系,下篇文章介绍
4.exposedObject = initializeBean(beanName, exposedObject, mbd);初始化bean
这一部分是创建bean的关键部分,也就是在这一步中把bean加入了三级缓存之中。

10.createBeanInstance(beanName, mbd, args)

源码:

	/**
	 * Create a new instance for the specified bean, using an appropriate instantiation strategy:
	 * factory method, constructor autowiring, or simple instantiation.
	 * @param beanName the name of the bean
	 * @param mbd the bean definition for the bean
	 * @param args explicit arguments to use for constructor or factory method invocation
	 * @return a BeanWrapper for the new instance
	 * @see #obtainFromSupplier
	 * @see #instantiateUsingFactoryMethod
	 * @see #autowireConstructor
	 * @see #instantiateBean
	 */
	protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
		// Make sure bean class is actually resolved at this point.
		Class<?> beanClass = resolveBeanClass(mbd, beanName);

		if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
			throw new BeanCreationException(mbd.getResourceDescription(), beanName,
					"Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
		}

		Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
		if (instanceSupplier != null) {
			return obtainFromSupplier(instanceSupplier, beanName);
		}

		if (mbd.getFactoryMethodName() != null) {
			return instantiateUsingFactoryMethod(beanName, mbd, args);
		}

		// Shortcut when re-creating the same bean...
		boolean resolved = false;
		boolean autowireNecessary = false;
		if (args == null) {
			synchronized (mbd.constructorArgumentLock) {
				if (mbd.resolvedConstructorOrFactoryMethod != null) {
					resolved = true;
					autowireNecessary = mbd.constructorArgumentsResolved;
				}
			}
		}
		if (resolved) {
			if (autowireNecessary) {
				return autowireConstructor(beanName, mbd, null, null);
			}
			else {
				return instantiateBean(beanName, mbd);
			}
		}

		// Candidate constructors for autowiring?
		Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
		if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
				mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
			return autowireConstructor(beanName, mbd, ctors, args);
		}

		// No special handling: simply use no-arg constructor.
		//无特殊处理:只需使用无参数构造函数。
		return instantiateBean(beanName, mbd);
	}

这一部分是创建早期对象的代码,使用的是bean的构造器的方法,因为这个时候属性值还没赋值给bean所以叫做早期对象

11.addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean))

源码:

	/**
	 * Add the given singleton factory for building the specified singleton
	 * if necessary.
	 * 

To be called for eager registration of singletons, e.g. to be able to * resolve circular references. * @param beanName the name of the bean * @param singletonFactory the factory for the singleton object */ protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) { Assert.notNull(singletonFactory, "Singleton factory must not be null"); synchronized (this.singletonObjects) { //判断缓存中没有就往缓存singletonFactories中放 这是三级缓存 if (!this.singletonObjects.containsKey(beanName)) { this.singletonFactories.put(beanName, singletonFactory); this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } } }

可以看出,这一段就是往三级缓存singletonFactories中放入bean,将bean暴露出来

12.populateBean(beanName, mbd, instanceWrapper)

这个的源码,我们需要关注的是下面这部分,这部分是依赖的关键

if (pvs != null) {
			//为属性赋值
			applyPropertyValues(beanName, mbd, bw, pvs);
		}

applyPropertyValues的源码关注

//给A中的属性B赋值的过程
Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue);

继续往里看
resolveValueIfNecessary的源码片段

// We must check each value to see whether it requires a runtime reference
		// to another bean to be resolved.
		//我们必须检查每个值,看看它是否需要解析另一个bean的运行时引用。
		if (value instanceof RuntimeBeanReference) {
			RuntimeBeanReference ref = (RuntimeBeanReference) value;
			return resolveReference(argName, ref);
		}

resolveReference的源码

	/**
	 * Resolve a reference to another bean in the factory.
	 */
	@Nullable
	private Object resolveReference(Object argName, RuntimeBeanReference ref) {
		try {
			Object bean;
			String refName = ref.getBeanName();
			refName = String.valueOf(doEvaluate(refName));
			if (ref.isToParent()) {
				if (this.beanFactory.getParentBeanFactory() == null) {
					throw new BeanCreationException(
							this.beanDefinition.getResourceDescription(), this.beanName,
							"Can't resolve reference to bean '" + refName +
									"' in parent factory: no parent factory available");
				}
				bean = this.beanFactory.getParentBeanFactory().getBean(refName);
			}
			else {
				bean = this.beanFactory.getBean(refName);
				this.beanFactory.registerDependentBean(refName, this.beanName);
			}
			if (bean instanceof NullBean) {
				bean = null;
			}
			return bean;
		}
		catch (BeansException ex) {
			throw new BeanCreationException(
					this.beanDefinition.getResourceDescription(), this.beanName,
					"Cannot resolve reference to bean '" + ref.getBeanName() + "' while setting " + argName, ex);
		}
	}

这个方法的源码之中,调用了getbean方法,我们继续往里面看

	//---------------------------------------------------------------------
	// Implementation of BeanFactory interface
	//---------------------------------------------------------------------
	//beanfactory接口的实现
	@Override
	public Object getBean(String name) throws BeansException {
		return doGetBean(name, null, null, false);
	}

这个地方是不是和我们之前的一致,也就是说在这个populateBean方法里面会去获取依赖的bean。

13.继续执行getSingleton(beanName,…)的方法

执行

if (newSingleton) {
					addSingleton(beanName, singletonObject);
				}

这一部分的源码如下

	/**
	 * Add the given singleton object to the singleton cache of this factory.
	 * 

To be called for eager registration of singletons. * @param beanName the name of the bean * @param singletonObject the singleton object */ protected void addSingleton(String beanName, Object singletonObject) { synchronized (this.singletonObjects) { //加入一级缓存 this.singletonObjects.put(beanName, singletonObject); //清除三级缓存数据 this.singletonFactories.remove(beanName); //清除二级缓存数据 this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } }

这部分把bean加入一级缓存之中,然后清除二三级缓存。

创建总结

以上就是创建bean的流程,总结一下就是
1.判断缓存中是否存在bean,不存在就去创建
2.根据bean的构造器方法创建无属性值的早期对象
3.将早期对象暴露出来,存放入三级缓存singletonFactories
4.早期对象赋属性值(包括依赖)
5.初始化bean
6.bean存入一级缓存singletonObjects,清空二三级缓存

Bean的使用

看一下源码的流转
1.获取bean

//获取bean  去容器的缓存中直接拿
		InstantC instantC=(InstantC) ctx.getBean("ic");

2.getBean(“ic”)
源码:

	//---------------------------------------------------------------------
	// Implementation of BeanFactory interface
	//---------------------------------------------------------------------

	@Override
	public Object getBean(String name) throws BeansException {
		assertBeanFactoryActive();
		return getBeanFactory().getBean(name);
	}

3.getBeanFactory().getBean(name);
源码如下:

	//---------------------------------------------------------------------
	// Implementation of BeanFactory interface
	//---------------------------------------------------------------------
	//beanfactory接口的实现
	@Override
	public Object getBean(String name) throws BeansException {
		return doGetBean(name, null, null, false);
	}

看到这一部分就不用再往下看了。因为上面创建bean的时候已经有了这个方法,只不过现在我们在缓存中可以拿到bean,就不会往下继续走了。

Bean的销毁

调用close

ctx.close();

close的源码

	@Override
	public void close() {
		synchronized (this.startupShutdownMonitor) {
			doClose();
			// If we registered a JVM shutdown hook, we don't need it anymore now:
			// We've already explicitly closed the context.
			if (this.shutdownHook != null) {
				try {
					Runtime.getRuntime().removeShutdownHook(this.shutdownHook);
				}
				catch (IllegalStateException ex) {
					// ignore - VM is already shutting down
				}
			}
		}
	}

doClose()方法源码

	protected void doClose() {
		// Check whether an actual close attempt is necessary...
		if (this.active.get() && this.closed.compareAndSet(false, true)) {
			if (logger.isInfoEnabled()) {
				logger.info("Closing " + this);
			}

			LiveBeansView.unregisterApplicationContext(this);

			try {
				// Publish shutdown event.
				publishEvent(new ContextClosedEvent(this));
			}
			catch (Throwable ex) {
				logger.warn("Exception thrown from ApplicationListener handling ContextClosedEvent", ex);
			}

			// Stop all Lifecycle beans, to avoid delays during individual destruction.
			if (this.lifecycleProcessor != null) {
				try {
					this.lifecycleProcessor.onClose();
				}
				catch (Throwable ex) {
					logger.warn("Exception thrown from LifecycleProcessor on context close", ex);
				}
			}

			// Destroy all cached singletons in the context's BeanFactory.
			destroyBeans();

			// Close the state of this context itself.
			closeBeanFactory();

			// Let subclasses do some final clean-up if they wish...
			onClose();

			// Reset local application listeners to pre-refresh state.
			if (this.earlyApplicationListeners != null) {
				this.applicationListeners.clear();
				this.applicationListeners.addAll(this.earlyApplicationListeners);
			}

			// Switch to inactive.
			this.active.set(false);
		}
	}

这个就不做仔细探寻了

总结

  1. 三级缓存:第三级:singletonFactories 第二级:earlySingletonObjects 一级:singletonObjects
  2. bean的创建流程简述:
    1.判断缓存中是否存在bean,不存在就去创建
    2.根据bean的构造器方法创建无属性值的早期对象
    3.将早期对象暴露出来,存放入三级缓存singletonFactories
    4.早期对象赋属性值(包括依赖)
    5.初始化bean
    6.bean存入一级缓存singletonObjects,清空二三级缓存
  3. dependsOn指的是要先进行实例化的bean,也就是有beanA和beanB,如果用注解在beanB上加上@DependsOn(beanA),那么在创建 beanB之前一点更要先创建beanA,和依赖不同,依赖是指在创建beanB的过程中去创建beanA。
    最后,想要搭建源码阅读环境的可以联系我,手把手教学。

你可能感兴趣的:(spring,spring三级缓存,bean生命周期)