spring源码学习(5.1.0版本)——Bean的初始化(中)

目录

 

前言

createBean

有自定义TargetSource代理类的生成

resolveBeforeInstantiation

applyBeanPostProcessorsBeforeInstantiation

postProcessBeforeInstantiation

applyBeanPostProcessorsAfterInitialization

postProcessAfterInitialization与wrapIfNecessary

无代理Bean以及无自定义TargetSource Bean的代理的生成

doCreateBean

createBeanInstance:创建Bean实例

收获

Bean初始化的轨迹


前言

上篇博客主要介绍了getBean方法,Bean的初始化会有三个主要的方法

  1. createBean:初始化Bean,此时还没有进行属性填充
  2.  populateBean:进行属性填充
  3. initializeBean:调用生命周期回调

这篇博客主要介绍createBean方法

本文难免有错误,欢迎大家指出错误

 

createBean

该方法在AbstractAutowireCapableBeanFactory

	/**
	 * Central method of this class: creates a bean instance,
	 * populates the bean instance, applies post-processors, etc.
	 * @see #doCreateBean
	 */
	@Override
	protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
			throws BeanCreationException {

		if (logger.isTraceEnabled()) {
			logger.trace("Creating instance of bean '" + beanName + "'");
		}
		RootBeanDefinition mbdToUse = mbd;

		// Make sure bean class is actually resolved at this point, and
		// clone the bean definition in case of a dynamically resolved Class
		// which cannot be stored in the shared merged bean definition.
        英文讲的有点抽象,这里举一个例子,并不是所有的BeanDefinition都有BeanClass属性值的,例如使用@Bean注解
		初始化的Bean,这种情况下,就需要通过beanName获得Bean的类名
		Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
		if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
			mbdToUse = new RootBeanDefinition(mbd);
			mbdToUse.setBeanClass(resolvedClass);
		}

		
		// Prepare method overrides.
		try {
			对于 methodOverrides的校验,即 lookup-method和 replaced-method属性指定的方法,
			如果指定的方法不存在,会抛出异常,这两个属性的效果都是通过动态代理实现的
			mbdToUse.prepareMethodOverrides();
		}
		catch (BeanDefinitionValidationException ex) {
			throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
					beanName, "Validation of method overrides failed", ex);
		}

		try {
			// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
			这里初始化有自定义TargetSource的代理类
			Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
			if (bean != null) {
				return bean;
			}
		}
		catch (Throwable ex) {
			throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
					"BeanPostProcessor before instantiation of bean failed", ex);
		}

		try {
		    这里会干两件事:
			1、初始化Bean
			2、如果有切面应用于Bean,则会为其生成代理
			Object beanInstance = doCreateBean(beanName, mbdToUse, args);
			if (logger.isTraceEnabled()) {
				logger.trace("Finished creating instance of bean '" + beanName + "'");
			}
			return beanInstance;
		}
		catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
			// A previously detected exception with proper bean creation context already,
			// or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
			throw ex;
		}
		catch (Throwable ex) {
			throw new BeanCreationException(
					mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
		}
	}

关于TargetSource,请查看:https://my.oschina.net/lixin91/blog/688188,spring定义的TargetSource在大多数情况下已经够我们使了,截至5.1.0版本,TargetSource的种类远不止《spring揭秘》中所写的五种,这点会在springAOP原理学习时进行补充

 

有自定义TargetSource代理类的生成

 

resolveBeforeInstantiation

该方法位于AbstractAutowireCapableBeanFactory类中

@Nullable
	protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
		Object bean = null;
		if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
			// Make sure bean class is actually resolved at this point.
			不是合成类,存在InstantiationAwareBeanPostProcessors实例,这个类用于处理动态代理
			if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
				Class<?> targetType = determineTargetType(beanName, mbd);
				if (targetType != null) {
					接下里展开此方法
					bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
					if (bean != null) {
						展开此方法
						bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
					}
				}
			}
			mbd.beforeInstantiationResolved = (bean != null);
		}
		return bean;
	}

 

applyBeanPostProcessorsBeforeInstantiation

该方法位于AbstractAutowireCapableBeanFactory类中

	/**
	 * Apply InstantiationAwareBeanPostProcessors to the specified bean definition
	 * (by class and name), invoking their {@code postProcessBeforeInstantiation} methods.
	 * <p>Any returned object will be used as the bean instead of actually instantiating
	 * the target bean. A {@code null} return value from the post-processor will
	 * result in the target bean being instantiated.
	 * @param beanClass the class of the bean to be instantiated
	 * @param beanName the name of the bean
	 * @return the bean object to use instead of a default instance of the target bean, or {@code null}
	 * @see InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation
	 */
	@Nullable
	protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
		for (BeanPostProcessor bp : getBeanPostProcessors()) {
			if (bp instanceof InstantiationAwareBeanPostProcessor) {
				InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                                其中某个InstantiationAwareBeanPostProcessor的此方法用于实例化代理类
				Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
				if (result != null) {
					return result;
				}
			}
		}
		return null;
	}

 

postProcessBeforeInstantiation

该方法位于AbstractAutoProxyCreator中

	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
		Object cacheKey = getCacheKey(beanClass, beanName);

		
		若targetSourcedBeans中含有beanName,说明beanName的代理对象已经生成了
		if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(beanName)) {
			adviseBeans中存储是否需要为某个Bean生成代理,如果里面存在cacheKey,
			说明之前已经初始化过Bean的代理类了,此时不需要再次初始化
			if (this.advisedBeans.containsKey(cacheKey)) {
				return null;
			}
		        不代理的条件一:不是 Advice、Pointcut、Advisor、AopInfrastructureBean类的子类
                        不代理的条件二:shouldSkip返回 true,默认返回 false
                        不代理的条件三:AspectJAwareAdvisorAutoProxyCreator:应该跳过所有 AspectJPointcutAdvisor指定的增强 bean
			if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
				this.advisedBeans.put(cacheKey, Boolean.FALSE);
				return null;
			}
		}

		// Create proxy here if we have a custom TargetSource.
		// Suppresses unnecessary default instantiation of the target bean:
		// The TargetSource will handle target instances in a custom fashion.
		尝试根据beanClass、beanName获得自定义的TargetSource
		TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
		if (targetSource != null) {
			if (StringUtils.hasLength(beanName)) {
			    在这里放入targetSourcedBeans缓存中,这样在函数开头就可以返回
				this.targetSourcedBeans.add(beanName);
			}
			getAdvicesAndAdvisorsForBean方法的作用:
			Return whether the given bean is to be proxied, what additional advices (e.g. AOP Alliance interceptors) and advisors to apply.
			
			Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
			创建代理,这里暂不展开此方法,在springaop篇在解析
			Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
			this.proxyTypes.put(cacheKey, proxy.getClass());
			return proxy;
		}

		return null;
	}

 

applyBeanPostProcessorsAfterInitialization

该方法位于AbstractAutowireCapableBeanFactory类中

	@Override
	public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
			throws BeansException {

		Object result = existingBean;
		for (BeanPostProcessor processor : getBeanPostProcessors()) {
			如果之前两步有返回代理Bean,那么将会跑到这个方法里,完成生命周期,但是有个BeanPostProcessor会处理无自定义TargetSource,但又想生成代理的情况,接下来展开介绍此postProcessAfterInitialization方法,
			Object current = processor.postProcessAfterInitialization(result, beanName);
			if (current == null) {
				return result;
			}
			result = current;
		}
		return result;
	}

 

postProcessAfterInitialization与wrapIfNecessary

这两个方法位于均AbstractAutowireCapableBeanFactory类中

	@Override
	public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
			throws BeansException {

		Object result = existingBean;
		for (BeanPostProcessor processor : getBeanPostProcessors()) {
			如果之前两部有返回代理Bean,那么将会跑到这个方法里接下来展开介绍postProcessAfterInitialization方法,
			这个方法用于处理无自定义TargetSource,但又想生成代理的情况
			Object current = processor.postProcessAfterInitialization(result, beanName);
			if (current == null) {
				return result;
			}
			result = current;
		}
		return result;
	}
	
	/**
	 * Wrap the given bean if necessary, i.e. if it is eligible for being proxied.
	 * @param bean the raw bean instance
	 * @param beanName the name of the bean
	 * @param cacheKey the cache key for metadata access
	 * @return a proxy wrapping the bean, or the raw bean instance as-is
	 */
	protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
		对于有自定义TargetSource的Bean来说,上文已经生成代理了,所以不在生成代理,直接返回
		if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
			return bean;
		}
		在advisedBean中有缓存,并且显示不应该生成代理,直接返回
		if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
			return bean;
		}
		不代理的条件一:不是 Advice、Pointcut、Advisor、AopInfrastructureBean类的子类
        不代理的条件二:shouldSkip返回 true,默认返回 false
        不代理的条件三:AspectJAwareAdvisorAutoProxyCreator:应该跳过所有 AspectJPointcutAdvisor指定的增强 bean
		if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
			this.advisedBeans.put(cacheKey, Boolean.FALSE);
			return bean;
		}
		
		创建代理
		// Create proxy if we have advice.
		Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
		if (specificInterceptors != DO_NOT_PROXY) {
			this.advisedBeans.put(cacheKey, Boolean.TRUE);
			Object proxy = createProxy(
					对于一般的代理类,其也会使用TargetSource,而且是SingletonTargetSource,这说明spring
					的动态代理与我们平常的认知还是有点区别的(多了个TargetSource)
					bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
			this.proxyTypes.put(cacheKey, proxy.getClass());
			return proxy;
		}

		放入缓存,表明不需要生成代理(由于已经生成,并且是单例)
		this.advisedBeans.put(cacheKey, Boolean.FALSE);
		return bean;
	}

 

无代理Bean以及无自定义TargetSource Bean的代理的生成

 

doCreateBean

	/**
	 * Actually create the specified bean. Pre-creation processing has already happened
	 * at this point, e.g. checking {@code postProcessBeforeInstantiation} callbacks.
	 * <p>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.
		BeanWrapper负责Bean属性的填充
		BeanWrapper instanceWrapper = null;
		if (mbd.isSingleton()) {
			instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
		}
		if (instanceWrapper == null) {
			createBeanInstance方法会创建Bean实例,调用构造函数初始化Bean
			instanceWrapper = createBeanInstance(beanName, mbd, args);
		}
		获得初始化好的Bean,此时并未进行构造函数以外的依赖注入
		final Object bean = instanceWrapper.getWrappedInstance();
		获得Bean的class类型,并将其保存到RootBeanDefinition中,后续就不需要通过解析获得class类型
		Class<?> beanType = instanceWrapper.getWrappedClass();
		if (beanType != NullBean.class) {
			mbd.resolvedTargetType = beanType;
		}

		// Allow post-processors to modify the merged bean definition.
		这个属性锁住了RootBeanDefinition的两个属性,之前博客有讲,这里不赘述
		上锁的目的是确保MergedBeanDefinitionPostProcessor只处理一次该Bean的RootBeanDefinition对象
		synchronized (mbd.postProcessingLock) {
			if (!mbd.postProcessed) {
				try {
				    让MergedBeanDefinitionPostProcessor处理RootBeanDefinition
					applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
				}
				catch (Throwable ex) {
					throw new BeanCreationException(mbd.getResourceDescription(), beanName,
							"Post-processing of merged bean definition failed", ex);
				}
				mbd.postProcessed = true;
			}
		}
		
		
		代码跑到这里,Bean已经初步实例化了,接下来进行构造函数以外的依赖注入
		// Eagerly cache singletons to be able to resolve circular references
		// even when triggered by lifecycle interfaces like BeanFactoryAware.
		这里可以看到setter循环依赖的处理机制,允许setter循环依赖的条件是:
		1、Bean是单例
		2、允许循环依赖
		3、当前的Bean正处于创建阶段(未进行依赖注入,故没有完全创建完毕)
		boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
				isSingletonCurrentlyInCreation(beanName));
		if (earlySingletonExposure) {
			if (logger.isTraceEnabled()) {
				logger.trace("Eagerly caching bean '" + beanName +
						"' to allow for resolving potential circular references");
			}
			这一步是解决setter循环依赖的关键,在DefaultSingletonBeanRegistry的解析中,已经解析过spring是如何通过三级缓存解决循环依赖的
			这里不在赘述
			addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
		}

		// Initialize the bean instance.
		Object exposedObject = bean;
		try {
			进行构造函数以外的依赖注入,下一篇博客会展开讲解
			populateBean(beanName, mbd, instanceWrapper);
			
			进行生命周期回调,包括:
            1、BeanNameAware、BeanClassLoaderAware、BeanFactoryAware
            2、生命周期:BeanPostProcessor.postProcessBeforeInitialization
            3、生命周期:InitializingBean.afterPropertiesSet
            4、生命周期:调用初始化方法:init-method
            5、生命周期:BeanPostProcessor的postProcessAfterInitialization
            这里可能会生成代理
			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) {
			注意到getSingleton函数的allowEarlyReference属性值为false,意味着只能从singletonObjects缓存或是
			earlySingletonObjects缓存中获得实例,此时singletonObjects缓存中是不存在实例的,
			只能从earlySingletonObjects缓存中获得
			Object earlySingletonReference = getSingleton(beanName, false);
			如果earlySingletonReference不为空,说明出现setter循环依赖
			if (earlySingletonReference != null) {
			
				由上文可以看出这两者指向相同的内存,这里为什么要判断exposedObject与bean相等呢?
				其实我们忽略了生成代理的情况,initializeBean方法中可能会生成代理,如果生成了代理,
				这句if是不成立的,如果没有生成代理,earlySingletonReference才是最终的结果
				if (exposedObject == bean) {
					exposedObject = earlySingletonReference;
				}
				
				对于代理,需要确保所有的依赖都已经生成
				else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
					String[] dependentBeans = getDependentBeans(beanName);
					actualDependentBeans用于存储未创建的依赖Bean
					Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
					for (String dependentBean : dependentBeans) {
						已经创建完毕的Bean会存储到alreadyCreated中,若alreadyCreated不包含 dependentBean会返回 false
						if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
							actualDependentBeans.add(dependentBean);
						}
					}
					如果actualDependentBeans不为空,则抛出异常,因为beanName已经创建完毕,但是beanName的依赖却没有创建完毕
					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.
		try {
			如果bean实现了销毁回调,则会在这里注册,存储到某个缓存中,在spring销毁Bean时,
			会根据该缓存,调用销毁回调
			registerDisposableBeanIfNecessary(beanName, bean, mbd);
		}
		catch (BeanDefinitionValidationException ex) {
			throw new BeanCreationException(
					mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
		}

		return exposedObject;
    }

 

createBeanInstance:创建Bean实例

	/**
	 * 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);

		isNonPublicAccessAllowed默认返回 true,即非 public的类同样可以实例化
		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());
		}

		如果我们配置了AbstractBeanDefinition的InstanceSupplier属性,则会根据InstanceSupplier的值
		初始化Bean,而不是构造函数与工厂方法,这是java8推出时出现的,以后有空看下咋回事
		Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
		if (instanceSupplier != null) {
			return obtainFromSupplier(instanceSupplier, beanName);
		}

		如果工厂方法不为空,则通过工厂方法实例化,使用@Bean初始化的Bean,在这里初始化
		if (mbd.getFactoryMethodName() != null) {
			return instantiateUsingFactoryMethod(beanName, mbd, args);
		}
夹在横线中间的代码,在我们初始化自己的Bean时,是不会执行的
----------------------------------------------------------------------------------------------------------------------------
		// Shortcut when re-creating the same bean...
		boolean resolved = false;
		boolean autowireNecessary = false;
		第一次初始化时,传入的构造函数参数为空,会跑到下面的if语句中
		if (args == null) {
			
			在第一次初始化时,并不存在已经解析好的构造函数或是工厂方法,所以下面这些语句并不会执行
			synchronized (mbd.constructorArgumentLock) {
				判断是否存在已经解析好的构造函数或是工厂方法
				if (mbd.resolvedConstructorOrFactoryMethod != null) {
					resolved = true;
					如果构造函数参数已解析,即知道要注入什么给构造函数参数,
					则autowireNecessary为true,构造函数所需要的参数由constructorArgumentsResolved
					提供,若constructorArgumentsResolved为空,说明我们没有配置构造函数参数,此时spring
					会使用默认构造函数
					autowireNecessary = mbd.constructorArgumentsResolved;
				}
			}
		}
		if (resolved) {
			if (autowireNecessary) {
				有参构造函数所需要的所有信息都存储在RootBeanDefinition中,
				从而避免再次解析
				return autowireConstructor(beanName, mbd, null, null);
			}
			else {
				使用默认构造函数初始化
				return instantiateBean(beanName, mbd);
			}
		}
---------------------------------------------------------------------------------------------------------------------------------
	
	
		Bean首次初始化时,会在以下代码执行初始化
		// Candidate constructors for autowiring?
		
		获得构造函数列表,判断使用哪个构造器:
		如果实现了 SmartInstantiationAwareBeanPostProcessor,
		可以通过方法 determineCandidateConstructors指定,否则返回 null		
		Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
		if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
				mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
			第一次初始化时,具有有参构造函数的Bean会在这里初始化,这个函数内部会通过createArgumentArray
			方法解析获得构造函数参数,并且会有构造函数循环依赖的检查,这句代码执行完毕后,RootBeanDefinition的关于构造函数的四个属性会发生变化
			1、constructorArgumentsResolved变为true
			2、resolvedConstructorOrFactoryMethod不为空
			3、constructorArgumentValues不为空
			4、preparedConstructorArguments不为空
			以后初始化时,就可以直接利用上述信息,跑到横向内的代码执行初始化

			return autowireConstructor(beanName, mbd, ctors, args);
		}

		// Preferred constructors for default construction?
		
		从5.1.0开始有这一段代码,但是这个函数一般返回null,if语句内的代码基本没用,所以还是会执行默认构造函数初始化
		ctors = mbd.getPreferredConstructors();
		if (ctors != null) {
			return autowireConstructor(beanName, mbd, ctors, null);
		}

		使用默认构造函数初始化
		// No special handling: simply use no-arg constructor.
		return instantiateBean(beanName, mbd);
	}

 

收获

透过本次博客的学习,可以看出BeanDefinition并不是一开始就存储了Bean的所有信息,在Bean第一次进行初始化时,才会进行对应的解析,并且将解析的结果存储在BeanDefinition中

 

Bean初始化的轨迹

spring初始化Bean的结构十分复杂,这里画一张图,以捋清楚Bean初始化的顺序,对于搞清楚setter循环依赖的解决有一定帮助

展开阅读全文

没有更多推荐了,返回首页