废话不多说,我们直接看源码:
package org.springframework.lantao;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class XmlBeanFactoryTest {
public static void main(String[] args) {
ClassPathResource classPathResource = new ClassPathResource("spring-bean.xml");
BeanFactory beanFactory = new XmlBeanFactory(classPathResource);
UserBean userBean = (UserBean) beanFactory.getBean("userBean");
System.out.println(userBean.getName());
}
}
XMLBeanFactory解析就不多说了,如果没看过的可以去看我上一篇文章,这里直接看BeanFactory.getBean()
/**
-
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 doGetBean(final String name, @Nullable final Class requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {//获取实例名字
final String beanName = transformedBeanName(name);
Object bean;// Eagerly check singleton cache for manually registered singletons.
// 检查单例缓存中是否存在实例
Object sharedInstance = getSingleton(beanName);
if (sharedInstance != null && args == null) {
if (logger.isTraceEnabled()) {
if (isSingletonCurrentlyInCreation(beanName)) {
logger.trace(“Returning eagerly cached instance of singleton bean '” + beanName +
“’ that is not fully initialized yet - a consequence of a circular reference”);
}
else {
logger.trace(“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.
// 原型 循环引用 抛异常
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 if (requiredType != null) { // No args -> delegate to standard getBean method. return parentBeanFactory.getBean(nameToLookup, requiredType); }else { return (T) parentBeanFactory.getBean(nameToLookup); } } if (!typeCheckOnly) { // 将指定的bean标记为已经创建(或即将创建)。这允许bean工厂优化其缓存,以便重复创建指定的bean markBeanAsCreated(beanName); } try { final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args); // Guarantee initialization of beans that the current bean depends on. 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()) { sharedInstance = getSingleton(beanName, () -> { try { 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.
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.isTraceEnabled()) {
logger.trace(“Failed to convert bean '” + name + “’ to required type '” +
ClassUtils.getQualifiedName(requiredType) + “’”, ex);
}
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
}
return (T) bean;
}
doGetBean的源码比较长,那它都做了哪些事情呢:
1:转换对应的beanname,可能有很多人不理解,传入进来的不就应该是beanName嘛,其实传入的可能是BeanFactory,也可能是别名,如果是BeanFactory,就要去除它的修饰符,比如传入进来的&aa,就要转换成aa,但如果传入进来的是别名,就要取alias对应的最终的beanName,例如别名A指向了B的bean则返回B的beanName,如果别名A指向了别名B,别名B指向了C,则要返回C的BeanName;
2:检查单例中是否存在实例,会先从缓存中获取,下面会详细讲解;
3:判断原型实例是否存在循环依赖,比如A中有B,B中有A,这种情况只有单例模式才会尝试去创建,因为单例模式会提早包曝光实例,存在缓存中,原型模式是不允许的,会抛出类正在创建异常;
4:通过父BeanFactory获取bean;
5:将指定的bean标记为已经创建(或即将创建)。这允许bean工厂优化其缓存
6:获取RootBeanDefinition,在XmlBeanFactory解析的时候会将bean注册到beanDefinitionMap中,这里就是在beanDefinitionMap中get,如果不存在则会抛出bean not found异常,同时会将GenericBeanDefinition转换成RootBeanDefinition,因为存入时是GenericBeanDefinition类型;
7:检查BeanDefinition是否是abstract,如果是则抛出,bean is Aastract异常;
8:检查依赖,保证该bean所以依赖的bean都已经初始化,首先这里要了解depends-on用来表示一个bean A的实例化依靠另一个bean B的实例化, 但是A并不需要持有一个B的对象,如果需要的话就不用depends-on;不理解可以看这篇文章
9:判断bean的类型,是single还是proptotype,对应的创建bean,或者没有指定scope的判断,其中出现最多的方法就是getObjectForBeanInstance,后续会一点点的解析它的源码;
接下里我们先看单例缓存获取:
/**
- 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);
//检查缓存中是否存在实例 isSingletonCurrentlyInCreation 该实例是否在创建中
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
//如果缓存中实例为null 则锁定全局变量singletonObjects并进行处理
synchronized (this.singletonObjects) {
//尝试从earlySingletonObjects (创建中提早曝光的beanFactory) 获取bean
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
//尝试从singletonFactories获取beanFactory
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
//返回获取到的bean
singletonObject = singletonFactory.getObject();
//增加缓存
this.earlySingletonObjects.put(beanName, singletonObject);
//删除缓存
this.singletonFactories.remove(beanName);
}
}
}
}
return singletonObject;
}
首先单例是只允许创建一次,并且单例支持解决循环依赖,第一步会从singletonObjectsMap中获取单例,如果发现不存在的话,再通过singletonsCurrentlyInCreation判断下当前bean是否在创建,如果是则从提前曝光的Map earlySingletonObjects中获取,如果依旧不能存在则在singletonFactories中获取BeanFactory,通过getBean进行返回,反之结束了,缓存没有,只能去重新创建了;
接下来看isPrototypeCurrentlyInCreation
/**
- Return whether the specified prototype bean is currently in creation
- (within the current thread).
- @param beanName the name of the bean
*/
protected boolean isPrototypeCurrentlyInCreation(String beanName) {
Object curVal = this.prototypesCurrentlyInCreation.get();
return (curVal != null &&
(curVal.equals(beanName) || (curVal instanceof Set && ((Set<?>) curVal).contains(beanName))));
}
在这里判断了原型实例是否存在循环依赖,比如A中有B,B中有A,这种情况只有单例模式才会尝试去创建,因为单例模式会提早包曝光实例,存在缓存中,原型模式是不允许的,会抛出类正在创建异常
markBeanAsCreated方法源码:
if (!typeCheckOnly) {
// 将指定的bean标记为已经创建(或即将创建)。这允许bean工厂优化其缓存
markBeanAsCreated(beanName);
}
protected void markBeanAsCreated(String beanName) {
if (!this.alreadyCreated.contains(beanName)) {
synchronized (this.mergedBeanDefinitions) {
if (!this.alreadyCreated.contains(beanName)) {
// Let the bean definition get re-merged now that we’re actually creating
// the bean… just in case some of its metadata changed in the meantime.
clearMergedBeanDefinition(beanName);
this.alreadyCreated.add(beanName);
}
}
}
}
在上述源码中我们可以看到,这段代码的含义就是将指定的bean标记为已经在创建或者即将创建;在clearMergedBeanDefinition方法中可以看到,如果没有标记bean正在创建则会删除BeanDefinnition,接下来会重新创建;
继续看getMergedLocalBeanDefinition源码:
/**
- Return a merged RootBeanDefinition, traversing the parent bean definition
- if the specified bean corresponds to a child bean definition.
- @param beanName the name of the bean to retrieve the merged definition for
- @return a (potentially merged) RootBeanDefinition for the given bean
- @throws NoSuchBeanDefinitionException if there is no bean with the given name
- @throws BeanDefinitionStoreException in case of an invalid bean definition
*/
protected RootBeanDefinition getMergedLocalBeanDefinition(String beanName) throws BeansException {
// Quick check on the concurrent map first, with minimal locking.
RootBeanDefinition mbd = this.mergedBeanDefinitions.get(beanName);
if (mbd != null) {
return mbd;
}
return getMergedBeanDefinition(beanName, getBeanDefinition(beanName));
}
在getBeanDefinition方法中,spring是直接从DefaultListAbleBeanFactory中的beanDefinitionMap获取注册时的BeanDefinition;
/**
-
Return a RootBeanDefinition for the given bean, by merging with the
-
parent if the given bean’s definition is a child bean definition.
-
@param beanName the name of the bean definition
-
@param bd the original bean definition (Root/ChildBeanDefinition)
-
@param containingBd the containing bean definition in case of inner bean,
-
or {@code null} in case of a top-level bean
-
@return a (potentially merged) RootBeanDefinition for the given bean
-
@throws BeanDefinitionStoreException in case of an invalid bean definition
*/
protected RootBeanDefinition getMergedBeanDefinition(
String beanName, BeanDefinition bd, @Nullable BeanDefinition containingBd)
throws BeanDefinitionStoreException {synchronized (this.mergedBeanDefinitions) {
RootBeanDefinition mbd = null;// Check with full lock now in order to enforce the same merged instance. if (containingBd == null) { mbd = this.mergedBeanDefinitions.get(beanName); } if (mbd == null) { if (bd.getParentName() == null) { // Use copy of given root bean definition. if (bd instanceof RootBeanDefinition) { mbd = ((RootBeanDefinition) bd).cloneBeanDefinition(); } else { mbd = new RootBeanDefinition(bd); } } else { // Child bean definition: needs to be merged with parent. BeanDefinition pbd; try { String parentBeanName = transformedBeanName(bd.getParentName()); if (!beanName.equals(parentBeanName)) { pbd = getMergedBeanDefinition(parentBeanName); } else { BeanFactory parent = getParentBeanFactory(); if (parent instanceof ConfigurableBeanFactory) { pbd = ((ConfigurableBeanFactory) parent).getMergedBeanDefinition(parentBeanName); } else { throw new NoSuchBeanDefinitionException(parentBeanName, "Parent name '" + parentBeanName + "' is equal to bean name '" + beanName + "': cannot be resolved without an AbstractBeanFactory parent"); } } } catch (NoSuchBeanDefinitionException ex) { throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName, "Could not resolve parent bean definition '" + bd.getParentName() + "'", ex); } // Deep copy with overridden values. mbd = new RootBeanDefinition(pbd); mbd.overrideFrom(bd); } // Set default singleton scope, if not configured before. if (!StringUtils.hasLength(mbd.getScope())) { mbd.setScope(RootBeanDefinition.SCOPE_SINGLETON); } // A bean contained in a non-singleton bean cannot be a singleton itself. // Let's correct this on the fly here, since this might be the result of // parent-child merging for the outer bean, in which case the original inner bean // definition will not have inherited the merged outer bean's singleton status. if (containingBd != null && !containingBd.isSingleton() && mbd.isSingleton()) { mbd.setScope(containingBd.getScope()); } // Cache the merged bean definition for the time being // (it might still get re-merged later on in order to pick up metadata changes) if (containingBd == null && isCacheBeanMetadata()) { this.mergedBeanDefinitions.put(beanName, mbd); } } return mbd;
}
}
在getMergedBeanDefinition方法中做了几件事情:
1:首先从缓存中mergedBeanDefinitions获取BeanDefinition
2:通过有参构造方法初始化RootBeanDefinition,这里的实例化涉及到一些参数的Set操作,具体代码就不展示了,在AbstractBeanDefinition的有参构造方法中;
3:指定bean的scope;
4:把RootBeanDefinition加入缓存mergedBeanDefinitions中;
在这里只是实例化就不多说了具体的DeBug一下就明白了,很简单;
我们继续看checkMergedBeanDefinition方法:
/**
-
Check the given merged bean definition,
-
potentially throwing validation exceptions.
-
@param mbd the merged bean definition to check
-
@param beanName the name of the bean
-
@param args the arguments for bean creation, if any
-
@throws BeanDefinitionStoreException in case of validation failure
*/
protected void checkMergedBeanDefinition(RootBeanDefinition mbd, String beanName, @Nullable Object[] args)
throws BeanDefinitionStoreException {if (mbd.isAbstract()) {
throw new BeanIsAbstractException(beanName);
}
}
checkMergedBeanDefinition方法是判断BeanDefinition是否是Abstract,如果是则抛出beanIsAbstractException,这里就不过多解释了,学过java的都懂;
接下来就要看当前Bean的依赖,需要先实例化依赖:
// Guarantee initialization of beans that the current bean depends on.
// 保证当前bean所依赖的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);
}
}
}
在这个方法中,如果当前Bean是有依赖的话就会先去GetBean他的依赖,保证当前bean的所有依赖都是初始化过可用的,getBean大家都不陌生吧,就是BeanFactory的getBean;
下面我们看bean的创建流程singleton:
// Create bean instance.
// 创建bean实例
if (mbd.isSingleton()) {
sharedInstance = getSingleton(beanName, () -> {
try {
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
destroySingleton(beanName);
throw ex;
}
});
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
上述代码就是创建Bean的一个主方法,首先是调用了getSingleton,接着又用lambda执行了createBean方法,紧着这又调用了getObjectForBeanInstance方法;
先来看createBean再看getSingleton方法:
/**
-
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.
// 锁定class根据class 属性或者className 来解析class
Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
mbdToUse = new RootBeanDefinition(mbd);
mbdToUse.setBeanClass(resolvedClass);
}// Prepare method overrides.
try {
// 方法注入准备
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.
// 给BeanPostProcessors一个返回代理而不是目标bean实例的机会
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 {
// 实例化 创建
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);
}
}
上述代码做了什么:
1:解析class,如果BeanDefinition中存在beanClass,则直接返回,反之需要通过ClassLoader加载,代码就不发了,可自行看源码;
2:方法注入,就是lookup-method的注入注入方式,这里就不多赘述了,可看上一遍文章;
3:spring的原文解释是给BeanPostProcessors一个返回代理而不是目标bean实例的机会,这里涉及Aop的代理,后续文章会详细解释;
4:调用doCreateBean进行创建,我们直接看doCreateBean方法:
doCreateBean方法:
/**
-
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.
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
//使用对应的策略创建实例 比如工厂
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.
// 是否需要提前暴露, 单例&允许循环依赖&当前bean正在创建中 singletonsCurrentlyInCreation在DefaultSingletonBeanRegistry 225行创建,在创建bean之前记录 正在创建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”);
}
// 在bean未实例化之前加入到缓存中,单例支持循环依赖
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}// Initialize the bean instance.
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);
//earlySingletonReference 再有在检查循环依赖的时候才不为空
if (earlySingletonReference != null) {
if (exposedObject == bean) {
exposedObject = earlySingletonReference;
}
else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
String[] dependentBeans = getDependentBeans(beanName);
Set 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.
try {
registerDisposableBeanIfNecessary(beanName, bean, mbd);
}
catch (BeanDefinitionValidationException ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, “Invalid destruction signature”, ex);
}
return exposedObject;
}
上述代码中描述了创建bean的流程但是真正的createBean还不是在这个方法,在createBeanInstance中,我们先看看CreateBeann方法都做了什么:
1:使用对应的策略创建实例 createBeanInstance方法;
2:判断是否提前暴露,条件是( 单例&允许循环依赖&当前bean正在创建中 singletonsCurrentlyInCreation在DefaultSingletonBeanRegistry 225行创建,在创建bean之前记录 正在创建bean),如果需要则调用addSingletonFactory方法在bean未实例化之前加入到缓存中,单例支持循环依赖;
3:对bean进行补充,属性注入,bean依赖;对ByName,ByType依赖进行初始化并注册依赖Bean;
4:调用初始化方法;
5:对依赖处理;
看一下createBeanInstance方法:
/**
-
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
Class<?> beanClass = resolveBeanClass(mbd, beanName);
//确保class不为空,并且访问权限为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());
}
//配置的一种特殊的callback回调方法,通过这个callback创建bean
Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
if (instanceSupplier != null) {
return obtainFromSupplier(instanceSupplier, beanName);
}// 如果工厂方式不为空,则使用功能工厂方式进行解析
if (mbd.getFactoryMethodName() != null) {
return instantiateUsingFactoryMethod(beanName, mbd, args);
}// 一个类可能有多个构造器,所以Spring得根据参数个数、类型确定需要调用的构造器
// 在使用构造器创建实例后,Spring会将解析过后确定下来的构造器或工厂方法保存在缓存中,避免再次创建相同bean时再次解析
boolean resolved = false;
boolean autowireNecessary = false;
if (args == null) {
synchronized (mbd.constructorArgumentLock) {
//判断构造器或工厂方法是否为null
if (mbd.resolvedConstructorOrFactoryMethod != null) {
resolved = true;
//已经解析过class的构造器
autowireNecessary = mbd.constructorArgumentsResolved;
}
}
}
if (resolved) {
if (autowireNecessary) {
//构造函数自动注入
return autowireConstructor(beanName, mbd, null, null);
}
else {
//使用默认构造器
return instantiateBean(beanName, mbd);
}
}// 需要根据参数解析、确定构造函数
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
return autowireConstructor(beanName, mbd, ctors, args);
}// Preferred constructors for default construction?
// 默认构造的首选构造器?
ctors = mbd.getPreferredConstructors();
if (ctors != null) {
return autowireConstructor(beanName, mbd, ctors, null);
}
// No special handling: simply use no-arg constructor.
// 使用默认构造器
return instantiateBean(beanName, mbd);
}
上述方法:
1:解析class;
2:如果存在 Supplier 回调,则调用 obtainFromSupplier() 进行初始化,如果不等于null就直接返回;
3:如果工厂方式不为空,则使用功能工厂方式进行解析;
4:构造函数自动注入 autowireConstructor
5:默认构造器 instantiateBean
我们先看看 Supplier回调的使用:
//配置的一种特殊的callback回调方法,通过这个callback创建bean
Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
if (instanceSupplier != null) {
return obtainFromSupplier(instanceSupplier, beanName);
}
/**
-
Obtain a bean instance from the given supplier.
-
@param instanceSupplier the configured supplier
-
@param beanName the corresponding bean name
-
@return a BeanWrapper for the new instance
-
@since 5.0
-
@see #getObjectForBeanInstance
*/
protected BeanWrapper obtainFromSupplier(Supplier<?> instanceSupplier, String beanName) {
Object instance;String outerBean = this.currentlyCreatedBean.get();
this.currentlyCreatedBean.set(beanName);
try {
instance = instanceSupplier.get();
}
finally {
if (outerBean != null) {
this.currentlyCreatedBean.set(outerBean);
}
else {
this.currentlyCreatedBean.remove();
}
}if (instance == null) {
instance = new NullBean();
}
BeanWrapper bw = new BeanWrapperImpl(instance);
initBeanWrapper(bw);
return bw;
}
/** -
Represents a supplier of results.
-
There is no requirement that a new or distinct result be returned each
-
time the supplier is invoked.
-
This is a functional interface
-
whose functional method is {@link #get()}.
-
@param the type of results supplied by this supplier
-
@since 1.8
*/
@FunctionalInterface
public interface Supplier {/**
- Gets a result.
- @return a result
*/
T get();
}
USB Microphone https://www.soft-voice.com/
Wooden Speakers https://www.zeshuiplatform.com/
亚马逊测评 www.yisuping.cn