Spring-framework源码分析(七)bean的生命周期
实例化 填充属性 初始化 销毁
getBean getBean(String name)
doGetBean()
name 就是bean名字
requiredType 表示需要的类型,如果有类型,创建后会进行类型转换
args 表示参数,也就是构造方法的参数
typeCheckOnly 表示只是做检查,并不是真的要用,这个会影响一些逻辑
doGetBean–1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 String beanName = transformedBeanName(name); Object bean; 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 ); }
AbstractAutowireCapableBeanFactory
#getObjectForBeanInstance
1 2 3 4 5 6 7 8 9 10 11 12 13 @Override protected Object getObjectForBeanInstance ( Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) { String currentlyCreatedBean = this .currentlyCreatedBean.get(); if (currentlyCreatedBean != null ) { registerDependentBean(beanName, currentlyCreatedBean); } return super .getObjectForBeanInstance(beanInstance, name, beanName, mbd); }
AbstractBeanFactory
#getObjectForBeanInstance
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 protected Object getObjectForBeanInstance ( Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) { if (BeanFactoryUtils.isFactoryDereference(name)) { if (beanInstance instanceof NullBean) { return beanInstance; } if (!(beanInstance instanceof FactoryBean)) { throw new BeanIsNotAFactoryException(beanName, beanInstance.getClass()); } if (mbd != null ) { mbd.isFactoryBean = true ; } return beanInstance; } if (!(beanInstance instanceof FactoryBean)) { return beanInstance; } Object object = null ; if (mbd != null ) { mbd.isFactoryBean = true ; } else { object = getCachedObjectForFactoryBean(beanName); } if (object == null ) { FactoryBean<?> factory = (FactoryBean<?>) beanInstance; if (mbd == null && containsBeanDefinition(beanName)) { mbd = getMergedLocalBeanDefinition(beanName); } boolean synthetic = (mbd != null && mbd.isSynthetic()); object = getObjectFromFactoryBean(factory, beanName, !synthetic); } return object; }
getCachedObjectForFactoryBean
1 2 3 4 @Nullable protected Object getCachedObjectForFactoryBean (String beanName) { return this .factoryBeanObjectCache.get(beanName); }
FactoryBeanRegistrySupport#getObjectFromFactoryBean
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 protected Object getObjectFromFactoryBean (FactoryBean<?> factory, String beanName, boolean shouldPostProcess) { if (factory.isSingleton() && containsSingleton(beanName)) { synchronized (getSingletonMutex()) { Object object = this .factoryBeanObjectCache.get(beanName); if (object == null ) { object = doGetObjectFromFactoryBean(factory, beanName); Object alreadyThere = this .factoryBeanObjectCache.get(beanName); if (alreadyThere != null ) { object = alreadyThere; } else { if (shouldPostProcess) { if (isSingletonCurrentlyInCreation(beanName)) { return object; } beforeSingletonCreation(beanName); try { object = postProcessObjectFromFactoryBean(object, beanName); } catch (Throwable ex) { throw new BeanCreationException(beanName, "Post-processing of FactoryBean's singleton object failed" , ex); } finally { afterSingletonCreation(beanName); } } if (containsSingleton(beanName)) { this .factoryBeanObjectCache.put(beanName, object); } } } return object; } } else { Object object = doGetObjectFromFactoryBean(factory, beanName); if (shouldPostProcess) { try { object = postProcessObjectFromFactoryBean(object, beanName); } catch (Throwable ex) { throw new BeanCreationException(beanName, "Post-processing of FactoryBean's object failed" , ex); } } return object; } }
doGetObjectFromFactoryBean
就是调用 getObject()
创建对象,如果返回null的话,就封装成一个 NullBean
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 private Object doGetObjectFromFactoryBean (FactoryBean<?> factory, String beanName) throws BeanCreationException { Object object; try { if (System.getSecurityManager() != null ) { AccessControlContext acc = getAccessControlContext(); try { object = AccessController.doPrivileged((PrivilegedExceptionAction<Object>) factory::getObject, acc); } catch (PrivilegedActionException pae) { throw pae.getException(); } } else { object = factory.getObject(); } } catch (FactoryBeanNotInitializedException ex) { throw new BeanCurrentlyInCreationException(beanName, ex.toString()); } catch (Throwable ex) { throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation" , ex); } if (object == null ) { if (isSingletonCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException( beanName, "FactoryBean which is currently in creation returned null from getObject" ); } object = new NullBean(); } return object; }
beforeSingletonCreation
和 afterSingletonCreation
这个就是标记下,正在创建这个bean,创建处理完了就清除标记。
1 2 3 4 5 6 7 8 9 10 11 protected void beforeSingletonCreation (String beanName) { if (!this .inCreationCheckExclusions.contains(beanName) && !this .singletonsCurrentlyInCreation.add(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } }protected void afterSingletonCreation (String beanName) { if (!this .inCreationCheckExclusions.contains(beanName) && !this .singletonsCurrentlyInCreation.remove(beanName)) { throw new IllegalStateException("Singleton '" + beanName + "' isn't currently in creation" ); } }
postProcessObjectFromFactoryBean
就是进行 BeanPostProcessor
的 postProcessAfterInitialization
处理,也就是可以扩展的地方。
1 2 3 4 @Override protected Object postProcessObjectFromFactoryBean (Object object, String beanName) { return applyBeanPostProcessorsAfterInitialization(object, beanName); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Override public Object applyBeanPostProcessorsAfterInitialization (Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor processor : getBeanPostProcessors()) { Object current = processor.postProcessAfterInitialization(result, beanName); if (current == null ) { return result; } result = current; } return result; }
doGetBean–2
判断是不是正在创建的原型类型,原型类型无法解决循环应用问题,会报异常。
原型的定义就是每次都是需要新的对象,如果A是原型,引用B,这个时候B也是要被创建的,如果B是原型,也引用A,那A也要被重新创建,然后A里面又要引用B,又重新创建,这样下去就变成创建的死循环了,所以原型循环引用不行
如果发现父工厂存在, bean
定义不存在,就会从父工厂去找,让父工厂去创建。这种情况比较少
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 if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } BeanFactory parentBeanFactory = getParentBeanFactory();if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { String nameToLookup = originalBeanName(name); if (parentBeanFactory instanceof AbstractBeanFactory) { return ((AbstractBeanFactory) parentBeanFactory).doGetBean( nameToLookup, requiredType, args, typeCheckOnly); } else if (args != null ) { return (T) parentBeanFactory.getBean(nameToLookup, args); } else if (requiredType != null ) { return parentBeanFactory.getBean(nameToLookup, requiredType); } else { return (T) parentBeanFactory.getBean(nameToLookup); } }
doGetBean–3
先判断是否仅仅是检查类型,不是就标记为已经创建,并设置需要 bean 定义合并标记
处理 dependsOn
依赖
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 if (!typeCheckOnly) { markBeanAsCreated(beanName); }try { RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args); 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); } } }
markBeanAsCreated
1 2 3 4 5 6 7 8 9 10 11 12 13 protected void markBeanAsCreated (String beanName) { if (!this .alreadyCreated.contains(beanName)) { synchronized (this .mergedBeanDefinitions) { if (!this .alreadyCreated.contains(beanName)) { clearMergedBeanDefinition(beanName); this .alreadyCreated.add(beanName); } } } }
1 2 3 4 5 6 protected void clearMergedBeanDefinition (String beanName) { RootBeanDefinition bd = this .mergedBeanDefinitions.get(beanName); if (bd != null ) { bd.stale = true ; } }
doGetBean–4 下面就是 Bean
的创建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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); }
getSingleton(String beanName, ObjectFactory<?> singletonFactory)
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 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 + "'" ); } beforeSingletonCreation(beanName); boolean newSingleton = false ; boolean recordSuppressedExceptions = (this .suppressedExceptions == null ); if (recordSuppressedExceptions) { this .suppressedExceptions = new LinkedHashSet<>(); } try { singletonObject = singletonFactory.getObject(); newSingleton = true ; } catch (IllegalStateException ex) { 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; } }
createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
解析类型
进行方法覆盖
InstantiationAwareBeanPostProcessor
处理
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 @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; Class<?> resolvedClass = resolveBeanClass(mbd, beanName); if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null ) { mbdToUse = new RootBeanDefinition(mbd); mbdToUse.setBeanClass(resolvedClass); } try { mbdToUse.prepareMethodOverrides(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(), beanName, "Validation of method overrides failed" , ex); } try { 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) { throw ex; } catch (Throwable ex) { throw new BeanCreationException( mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation" , ex); } }
resolveBeforeInstantiation
实例化前处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @Nullable protected Object resolveBeforeInstantiation (String beanName, RootBeanDefinition mbd) { Object bean = null ; if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) { 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
实例化前处理器处理
1 2 3 4 5 6 7 8 9 10 @Nullable protected Object applyBeanPostProcessorsBeforeInstantiation (Class<?> beanClass, String beanName) { for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) { Object result = bp.postProcessBeforeInstantiation(beanClass, beanName); if (result != null ) { return result; } } return null ; }
applyBeanPostProcessorsAfterInitialization
初始化后处理器处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Override public Object applyBeanPostProcessorsAfterInitialization (Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor processor : getBeanPostProcessors()) { Object current = processor.postProcessAfterInitialization(result, beanName); if (current == null ) { return result; } result = current; } return result; }
InstantiationAwareBeanPostProcessor 拓展点
doCreateBean 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 protected Object doCreateBean (String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException { BeanWrapper instanceWrapper = null ; if (mbd.isSingleton()) { instanceWrapper = this .factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null ) { instanceWrapper = createBeanInstance(beanName, mbd, args); } Object bean = instanceWrapper.getWrappedInstance(); Class<?> beanType = instanceWrapper.getWrappedClass(); if (beanType != NullBean.class) { mbd.resolvedTargetType = beanType; } 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 ; } } 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" ); } addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)); } Object exposedObject = bean; try { 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 " + "'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example." ); } } } } try { registerDisposableBeanIfNecessary(beanName, bean, mbd); } catch (BeanDefinitionValidationException ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Invalid destruction signature" , ex); } return exposedObject; }
createBeanInstance
创建 bean
实例
获取类型
获取修饰符,只有 public 才允许创建
获取自定义的实例提供器,如果有的话直接获取返回
如果有工厂方法名字,使用工厂方法创建
推断构造函数
以上都不行,使用默认的无参构造函数进行实例化
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 protected BeanWrapper createBeanInstance (String beanName, RootBeanDefinition mbd, @Nullable Object[] args) { 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); } 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); } } Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { return autowireConstructor(beanName, mbd, ctors, args); } ctors = mbd.getPreferredConstructors(); if (ctors != null ) { return autowireConstructor(beanName, mbd, ctors, null ); } return instantiateBean(beanName, mbd); }
obtainFromSupplier 实例提供器 这是又一个拓展点
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 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; }
instantiateUsingFactoryMethod 工厂方法实例化 1 2 3 4 5 protected BeanWrapper instantiateUsingFactoryMethod ( String beanName, RootBeanDefinition mbd, @Nullable Object[] explicitArgs) { return new ConstructorResolver(this ).instantiateUsingFactoryMethod(beanName, mbd, explicitArgs); }
ConstructorResolver
保存 BeanFactory ,然后利用 BeanFactory 来做一些事情
1 2 3 4 public ConstructorResolver (AbstractAutowireCapableBeanFactory beanFactory) { this .beanFactory = beanFactory; this .logger = beanFactory.getLogger(); }
instantiateUsingFactoryMethod
根据工厂方法的是不是唯一来进行创建,如果唯一,就直接创建,不唯一的话还要进行方法获取,然后选出工厂方法,再处理
instantiateUsingFactoryMethod–1
创建一个实例持有器,后从 RootBeanDefinition中 获取 factoryBeanName,也就是工厂方法,比如说 bean 注解的方法就是
如果存在判断下是不是要创建自身,会报错,创建自身等于无限循环创建了
如果不是的话就获取factoryBean即工厂实例,获取工厂类型,设置非静态的。如果获取不到工厂实例,说明是静态方法
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 BeanWrapperImpl bw = new BeanWrapperImpl();this .beanFactory.initBeanWrapper(bw); Object factoryBean; Class<?> factoryClass;boolean isStatic; String factoryBeanName = mbd.getFactoryBeanName();if (factoryBeanName != null ) { if (factoryBeanName.equals(beanName)) { throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, "factory-bean reference points back to the same bean definition" ); } factoryBean = this .beanFactory.getBean(factoryBeanName); if (mbd.isSingleton() && this .beanFactory.containsSingleton(beanName)) { throw new ImplicitlyAppearedSingletonException(); } factoryClass = factoryBean.getClass(); isStatic = false ; }else { if (!mbd.hasBeanClass()) { throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, "bean definition declares neither a bean class nor a factory-bean reference" ); } factoryBean = null ; factoryClass = mbd.getBeanClass(); isStatic = true ; }
instantiateUsingFactoryMethod–2
准备好参数,尝试获取已经解析的数据,第一次都是空的。
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 Method factoryMethodToUse = null ; ArgumentsHolder argsHolderToUse = null ; Object[] argsToUse = null ;if (explicitArgs != null ) { argsToUse = explicitArgs; }else { Object[] argsToResolve = null ; synchronized (mbd.constructorArgumentLock) { factoryMethodToUse = (Method) mbd.resolvedConstructorOrFactoryMethod; if (factoryMethodToUse != null && mbd.constructorArgumentsResolved) { argsToUse = mbd.resolvedConstructorArguments; if (argsToUse == null ) { argsToResolve = mbd.preparedConstructorArguments; } } } if (argsToResolve != null ) { argsToUse = resolvePreparedArguments(beanName, mbd, bw, factoryMethodToUse, argsToResolve, true ); } }
instantiateUsingFactoryMethod–3
如果没解析过,就获取factoryClass的用户定义类型,因为此时factoryClass可能是CGLIB动态代理类型,所以要获取用父类的类型。
如果工厂方法是唯一的,就是没重载的,就获取解析的工厂方法,如果不为空,就添加到一个不可变列表里,如果为空的话,就要去找出factoryClass的以及父类的所有的方法,进一步找出方法修饰符一致且名字跟工厂方法名字相同的且是bean注解的方法,并放入列表里。
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 if (factoryMethodToUse == null || argsToUse == null ) { factoryClass = ClassUtils.getUserClass(factoryClass); List<Method> candidates = null ; if (mbd.isFactoryMethodUnique) { if (factoryMethodToUse == null ) { factoryMethodToUse = mbd.getResolvedFactoryMethod(); } if (factoryMethodToUse != null ) { candidates = Collections.singletonList(factoryMethodToUse); } } if (candidates == null ) { candidates = new ArrayList<>(); Method[] rawCandidates = getCandidateMethods(factoryClass, mbd); for (Method candidate : rawCandidates) { if (Modifier.isStatic(candidate.getModifiers()) == isStatic && mbd.isFactoryMethod(candidate)) { candidates.add(candidate); } } }
ClassUtils.getUserClass 找出用户定义的类型
1 2 3 4 5 6 7 8 9 10 public static Class<?> getUserClass(Class<?> clazz) { if (clazz.getName().contains(CGLIB_CLASS_SEPARATOR)) { Class<?> superclass = clazz.getSuperclass(); if (superclass != null && superclass != Object.class) { return superclass; } } return clazz; }
getCandidateMethods 获取所有候选方法包括父类的
1 2 3 4 5 6 7 8 9 10 11 12 private Method[] getCandidateMethods(Class<?> factoryClass, RootBeanDefinition mbd) { if (System.getSecurityManager() != null ) { return AccessController.doPrivileged((PrivilegedAction<Method[]>) () -> (mbd.isNonPublicAccessAllowed() ? ReflectionUtils.getAllDeclaredMethods(factoryClass) : factoryClass.getMethods())); } else { return (mbd.isNonPublicAccessAllowed() ? ReflectionUtils.getAllDeclaredMethods(factoryClass) : factoryClass.getMethods()); } }
ReflectionUtils.getAllDeclaredMethods 用反射获取方法
如果允许非Public的方法,就获取所有申明的方法,包括父类的,否则就是Public的方法,包括父类的,默认是允许的
1 2 3 4 5 6 7 8 9 10 11 public static Method[] getAllDeclaredMethods(Class<?> leafClass) { final List<Method> methods = new ArrayList<>(32 ); doWithMethods(leafClass, methods::add); return methods.toArray(EMPTY_METHOD_ARRAY); } ...public static void doWithMethods (Class<?> clazz, MethodCallback mc) { doWithMethods(clazz, mc, null ); }
doWithMethods 递归获取
递归获取所有的方法以及父类的,获取后执行mc.doWith(method)方法,其实就是上面的methods::add把方法添加到列表里,最后转换成数组返回。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public static void doWithMethods (Class<?> clazz, MethodCallback mc, @Nullable MethodFilter mf) { Method[] methods = getDeclaredMethods(clazz, false ); for (Method method : methods) { if (mf != null && !mf.matches(method)) { continue ; } try { mc.doWith(method); } catch (IllegalAccessException ex) { throw new IllegalStateException("Not allowed to access method '" + method.getName() + "': " + ex); } } if (clazz.getSuperclass() != null && (mf != USER_DECLARED_METHODS || clazz.getSuperclass() != Object.class)) { doWithMethods(clazz.getSuperclass(), mc, mf); } else if (clazz.isInterface()) { for (Class<?> superIfc : clazz.getInterfaces()) { doWithMethods(superIfc, mc, mf); } } }
instantiateUsingFactoryMethod–4
只有一个无参工厂方法 如果只有一个构造函数的话,看是否有参数,没有的话直接就设置bean定义的属性,然后实例化设置进包装对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 if (candidates.size() == 1 && explicitArgs == null && !mbd.hasConstructorArgumentValues()) { Method uniqueCandidate = candidates.get(0 ); if (uniqueCandidate.getParameterCount() == 0 ) { mbd.factoryMethodToIntrospect = uniqueCandidate; synchronized (mbd.constructorArgumentLock) { mbd.resolvedConstructorOrFactoryMethod = uniqueCandidate; mbd.constructorArgumentsResolved = true ; mbd.resolvedConstructorArguments = EMPTY_ARGS; } bw.setBeanInstance(instantiate(beanName, mbd, factoryBean, uniqueCandidate, EMPTY_ARGS)); return bw; } }
instantiate 工厂方法实例化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 private Object instantiate (String beanName, RootBeanDefinition mbd, @Nullable Object factoryBean, Method factoryMethod, Object[] args) { try { if (System.getSecurityManager() != null ) { return AccessController.doPrivileged((PrivilegedAction<Object>) () -> this .beanFactory.getInstantiationStrategy().instantiate( mbd, beanName, this .beanFactory, factoryBean, factoryMethod, args), this .beanFactory.getAccessControlContext()); } else { return this .beanFactory.getInstantiationStrategy().instantiate( mbd, beanName, this .beanFactory, factoryBean, factoryMethod, args); } } catch (Throwable ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Bean instantiation via factory method failed" , ex); } }
SimpleInstantiationStrategy#instantiate
其实默认的实例化策略是CglibSubclassingInstantiationStrategy,就是可以用CGLIB做动态代理,但是仅限于方法注入的形式,所以这里是无参工厂方法还是调用父类SimpleInstantiationStrategy的实现。其实就是调用工厂实例的工厂方法,传入参数,只是参数是个空数组EMPTY_ARGS,返回对象。
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 @Override public Object instantiate (RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner, @Nullable Object factoryBean, final Method factoryMethod, Object... args) { try { if (System.getSecurityManager() != null ) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { ReflectionUtils.makeAccessible(factoryMethod); return null ; }); } else { ReflectionUtils.makeAccessible(factoryMethod); } Method priorInvokedFactoryMethod = currentlyInvokedFactoryMethod.get(); try { currentlyInvokedFactoryMethod.set(factoryMethod); Object result = factoryMethod.invoke(factoryBean, args); if (result == null ) { result = new NullBean(); } return result; } finally { if (priorInvokedFactoryMethod != null ) { currentlyInvokedFactoryMethod.set(priorInvokedFactoryMethod); } else { currentlyInvokedFactoryMethod.remove(); } } } catch (IllegalArgumentException ex) { throw new BeanInstantiationException(factoryMethod, "Illegal arguments to factory method '" + factoryMethod.getName() + "'; " + "args: " + StringUtils.arrayToCommaDelimitedString(args), ex); } catch (IllegalAccessException ex) { throw new BeanInstantiationException(factoryMethod, "Cannot access factory method '" + factoryMethod.getName() + "'; is it public?" , ex); } catch (InvocationTargetException ex) { String msg = "Factory method '" + factoryMethod.getName() + "' threw exception" ; if (bd.getFactoryBeanName() != null && owner instanceof ConfigurableBeanFactory && ((ConfigurableBeanFactory) owner).isCurrentlyInCreation(bd.getFactoryBeanName())) { msg = "Circular reference involving containing bean '" + bd.getFactoryBeanName() + "' - consider " + "declaring the factory method as static for independence from its containing instance. " + msg; } throw new BeanInstantiationException(factoryMethod, msg, ex.getTargetException()); } }
多个有参工厂方法
怎么处理有多个参数的工厂方法呢,到底哪一个才是应该调用呢。
方法排序
1 2 3 4 if (candidates.size() > 1 ) { candidates.sort(AutowireUtils.EXECUTABLE_COMPARATOR); }
AutowireUtils.EXECUTABLE_COMPARATOR
1 2 3 4 5 public static final Comparator<Executable> EXECUTABLE_COMPARATOR = (e1, e2) -> { int result = Boolean.compare(Modifier.isPublic(e2.getModifiers()), Modifier.isPublic(e1.getModifiers())); return result != 0 ? result : Integer.compare(e2.getParameterCount(), e1.getParameterCount()); };
准备工作
因为接下去解析的工厂方法是有参数的,所以要进行一些处理,比如获取构造器参数值(如果有传的话),然后获取自动装配模式,工厂方法一般默认是AUTOWIRE_CONSTRUCTOR,还会定义一个minTypeDiffWeight 差异值,这个就是用在如果有多个工厂方法的时候,看工厂方法的参数和具体装配的bean的类型的差异,取最小的。还定义了有个ambiguousFactoryMethods ,用来存放差异值一样的方法,说明是一样的类型,无法判断要用哪个工厂方法实例化。比如protected UserDao userDao(TestBean2 t2)和public UserDao userDao(TestBean t1)两个构造方法类型差异是一样的,所以不知道要用哪个,就会报异常啦。还有minNrOfArgs也很关键,用来做优化的,如果有最小参数个数,那么一些参数个数少于最小个数的就不需要去判断了,直接跳过,否则就算获取到了也不匹配,反而浪费资源了。如果minNrOfArgs为0,那就说明没有参数个数限制,那后面就需要一个个去判断哪个差异最小,最符合了。
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 ConstructorArgumentValues resolvedValues = null ;boolean autowiring = (mbd.getResolvedAutowireMode() == AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);int minTypeDiffWeight = Integer.MAX_VALUE; Set<Method> ambiguousFactoryMethods = null ;int minNrOfArgs;if (explicitArgs != null ) { minNrOfArgs = explicitArgs.length; }else { if (mbd.hasConstructorArgumentValues()) { ConstructorArgumentValues cargs = mbd.getConstructorArgumentValues(); resolvedValues = new ConstructorArgumentValues(); minNrOfArgs = resolveConstructorArguments(beanName, mbd, bw, cargs, resolvedValues); } else { minNrOfArgs = 0 ; } }
自动装配
这段最核心的,自动装配就在里面,首先前面已经过滤出candidates工厂方法了,但是我们这不知道要选哪个去实例化,所以得有个选取规则,首先参数个数不能小于最少的,如果有显示的参数存在,那个数不一致的也不要,然后就获取参数名字探测器去进行每一个工厂方法的参数名字探测,然后创建一个参数持有器,这里面就会涉及自定装配,依赖的参数会实例化。最后根据参数持有器的参数和工厂方法的参数类型作比较,保存最小的差异值的那个,把模棱两可的集合设置空。如果有相同差异值的就放入一个集合里,如果集合有数据,说明不知道用哪个工厂方法来实例化,会报异常
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 69 70 71 72 73 74 for (Method candidate : candidates) { int parameterCount = candidate.getParameterCount(); if (parameterCount >= minNrOfArgs) { ArgumentsHolder argsHolder; Class<?>[] paramTypes = candidate.getParameterTypes(); if (explicitArgs != null ) { if (paramTypes.length != explicitArgs.length) { continue ; } argsHolder = new ArgumentsHolder(explicitArgs); } else { try { String[] paramNames = null ; ParameterNameDiscoverer pnd = this .beanFactory.getParameterNameDiscoverer(); if (pnd != null ) { paramNames = pnd.getParameterNames(candidate); } argsHolder = createArgumentArray(beanName, mbd, resolvedValues, bw, paramTypes, paramNames, candidate, autowiring, candidates.size() == 1 ); } catch (UnsatisfiedDependencyException ex) { if (logger.isTraceEnabled()) { logger.trace("Ignoring factory method [" + candidate + "] of bean '" + beanName + "': " + ex); } if (causes == null ) { causes = new LinkedList<>(); } causes.add(ex); continue ; } } int typeDiffWeight = (mbd.isLenientConstructorResolution() ? argsHolder.getTypeDifferenceWeight(paramTypes) : argsHolder.getAssignabilityWeight(paramTypes)); if (typeDiffWeight < minTypeDiffWeight) { factoryMethodToUse = candidate; argsHolderToUse = argsHolder; argsToUse = argsHolder.arguments; minTypeDiffWeight = typeDiffWeight; ambiguousFactoryMethods = null ; } else if (factoryMethodToUse != null && typeDiffWeight == minTypeDiffWeight && !mbd.isLenientConstructorResolution() && paramTypes.length == factoryMethodToUse.getParameterCount() && !Arrays.equals(paramTypes, factoryMethodToUse.getParameterTypes())) { if (ambiguousFactoryMethods == null ) { ambiguousFactoryMethods = new LinkedHashSet<>(); ambiguousFactoryMethods.add(factoryMethodToUse); } ambiguousFactoryMethods.add(candidate); } } }
determineConstructorsFromBeanPostProcessors
前面是工厂方法的实例化
现在可以讲一般的构造方法实例化了,首先会进行处理器处理,找出合适的构造方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Nullable protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(@Nullable Class<?> beanClass, String beanName) throws BeansException { if (beanClass != null && hasInstantiationAwareBeanPostProcessors()) { for (SmartInstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().smartInstantiationAware) { Constructor<?>[] ctors = bp.determineCandidateConstructors(beanClass, beanName); if (ctors != null ) { return ctors; } } } return null ; }
SmartInstantiationAwareBeanPostProcessor 接口
AutowiredAnnotationBeanPostProcessor#determineCandidateConstructors
方法很复杂,拷一份过来
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 @Override @Nullable public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, final String beanName) throws BeanCreationException { if (!this .lookupMethodsChecked.contains(beanName)) { if (AnnotationUtils.isCandidateClass(beanClass, Lookup.class)) { try { Class<?> targetClass = beanClass; do { ReflectionUtils.doWithLocalMethods(targetClass, method -> { Lookup lookup = method.getAnnotation(Lookup.class); if (lookup != null ) { Assert.state(this .beanFactory != null , "No BeanFactory available" ); LookupOverride override = new LookupOverride(method, lookup.value()); try { RootBeanDefinition mbd = (RootBeanDefinition) this .beanFactory.getMergedBeanDefinition(beanName); mbd.getMethodOverrides().addOverride(override); } catch (NoSuchBeanDefinitionException ex) { throw new BeanCreationException(beanName, "Cannot apply @Lookup to beans without corresponding bean definition" ); } } }); targetClass = targetClass.getSuperclass(); } while (targetClass != null && targetClass != Object.class); } catch (IllegalStateException ex) { throw new BeanCreationException(beanName, "Lookup method resolution failed" , ex); } } this .lookupMethodsChecked.add(beanName); } Constructor<?>[] candidateConstructors = this .candidateConstructorsCache.get(beanClass); if (candidateConstructors == null ) { synchronized (this .candidateConstructorsCache) { candidateConstructors = this .candidateConstructorsCache.get(beanClass); if (candidateConstructors == null ) { Constructor<?>[] rawCandidates; try { rawCandidates = beanClass.getDeclaredConstructors(); } catch (Throwable ex) { throw new BeanCreationException(beanName, "Resolution of declared constructors on bean Class [" + beanClass.getName() + "] from ClassLoader [" + beanClass.getClassLoader() + "] failed" , ex); } List<Constructor<?>> candidates = new ArrayList<>(rawCandidates.length); Constructor<?> requiredConstructor = null ; Constructor<?> defaultConstructor = null ; Constructor<?> primaryConstructor = BeanUtils.findPrimaryConstructor(beanClass); int nonSyntheticConstructors = 0 ; for (Constructor<?> candidate : rawCandidates) { if (!candidate.isSynthetic()) { nonSyntheticConstructors++; } else if (primaryConstructor != null ) { continue ; } MergedAnnotation<?> ann = findAutowiredAnnotation(candidate); if (ann == null ) { Class<?> userClass = ClassUtils.getUserClass(beanClass); if (userClass != beanClass) { try { Constructor<?> superCtor = userClass.getDeclaredConstructor(candidate.getParameterTypes()); ann = findAutowiredAnnotation(superCtor); } catch (NoSuchMethodException ex) { } } } if (ann != null ) { if (requiredConstructor != null ) { throw new BeanCreationException(beanName, "Invalid autowire-marked constructor: " + candidate + ". Found constructor with 'required' Autowired annotation already: " + requiredConstructor); } boolean required = determineRequiredStatus(ann); if (required) { if (!candidates.isEmpty()) { throw new BeanCreationException(beanName, "Invalid autowire-marked constructors: " + candidates + ". Found constructor with 'required' Autowired annotation: " + candidate); } requiredConstructor = candidate; } candidates.add(candidate); } else if (candidate.getParameterCount() == 0 ) { defaultConstructor = candidate; } } if (!candidates.isEmpty()) { if (requiredConstructor == null ) { if (defaultConstructor != null ) { candidates.add(defaultConstructor); } else if (candidates.size() == 1 && logger.isInfoEnabled()) { logger.info("Inconsistent constructor declaration on bean with name '" + beanName + "': single autowire-marked constructor flagged as optional - " + "this constructor is effectively required since there is no " + "default constructor to fall back to: " + candidates.get(0 )); } } candidateConstructors = candidates.toArray(new Constructor<?>[0 ]); } else if (rawCandidates.length == 1 && rawCandidates[0 ].getParameterCount() > 0 ) { candidateConstructors = new Constructor<?>[] {rawCandidates[0 ]}; } else if (nonSyntheticConstructors == 2 && primaryConstructor != null && defaultConstructor != null && !primaryConstructor.equals(defaultConstructor)) { candidateConstructors = new Constructor<?>[] {primaryConstructor, defaultConstructor}; } else if (nonSyntheticConstructors == 1 && primaryConstructor != null ) { candidateConstructors = new Constructor<?>[] {primaryConstructor}; } else { candidateConstructors = new Constructor<?>[0 ]; } this .candidateConstructorsCache.put(beanClass, candidateConstructors); } } } return (candidateConstructors.length > 0 ? candidateConstructors : null ); }
总结如下:
如果有多个Autowired,required有true,不管有没有默认构造方法,会报异常。
如果只有一个Autowired,required是false,没有默认构造方法,会报警告。
如果没有Autowired注解,定义了2个及以上有参数的构造方法,没有无参构造方法,就会报错。
其他情况都可以,但是以有Autowired的构造方法优先,然后才是默认构造方法。
如果构造方法存在且是有参数的,那就会调用autowireConstructor进行自动装配,如果不存在基本上无参构造方法了
autowireConstructor 有参构造方法自动装配 1 2 3 4 5 6 protected BeanWrapper autowireConstructor ( String beanName, RootBeanDefinition mbd, @Nullable Constructor<?>[] ctors, @Nullable Object[] explicitArgs) { return new ConstructorResolver(this ).autowireConstructor(beanName, mbd, ctors, explicitArgs); }
ConstructorResolver#autowireConstructor
这个和前面讲过的工厂方法实例化instantiateUsingFactoryMethod很像,但是有几个地方不一样
工厂方法会遍历完所有的方法,然后找出参数类型差异最小的方法,而自动装配构造方法找到一个满足条件的就停止了。
参数类型差异算法不一样,工厂方法是用严格的方法getAssignabilityWeight,而自动装配是宽松的方法getTypeDifferenceWeight。
instantiateBean 无参构造方法实例化 其实里面就是获取实例化的策略,然后进行instantiate实例化
前面有说过策略是CglibSubclassingInstantiationStrategy的,但是只会在注入的时候起作用
一般的还是用父类SimpleInstantiationStrategy的instantiate。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 protected BeanWrapper instantiateBean (String beanName, RootBeanDefinition mbd) { try { Object beanInstance; if (System.getSecurityManager() != null ) { beanInstance = AccessController.doPrivileged( (PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(mbd, beanName, this ), getAccessControlContext()); } else { beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this ); } BeanWrapper bw = new BeanWrapperImpl(beanInstance); initBeanWrapper(bw); return bw; } catch (Throwable ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Instantiation of bean failed" , ex); } }
BeanUtils#instantiateClass
调用构造方法反射创建对象
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 public static <T> T instantiateClass (Constructor<T> ctor, Object... args) throws BeanInstantiationException { Assert.notNull(ctor, "Constructor must not be null" ); try { ReflectionUtils.makeAccessible(ctor); if (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(ctor.getDeclaringClass())) { return KotlinDelegate.instantiateClass(ctor, args); } else { Class<?>[] parameterTypes = ctor.getParameterTypes(); Assert.isTrue(args.length <= parameterTypes.length, "Can't specify more arguments than constructor parameters" ); Object[] argsWithDefaultValues = new Object[args.length]; for (int i = 0 ; i < args.length; i++) { if (args[i] == null ) { Class<?> parameterType = parameterTypes[i]; argsWithDefaultValues[i] = (parameterType.isPrimitive() ? DEFAULT_TYPE_VALUES.get(parameterType) : null ); } else { argsWithDefaultValues[i] = args[i]; } } return ctor.newInstance(argsWithDefaultValues); } } catch (InstantiationException ex) { throw new BeanInstantiationException(ctor, "Is it an abstract class?" , ex); } catch (IllegalAccessException ex) { throw new BeanInstantiationException(ctor, "Is the constructor accessible?" , ex); } catch (IllegalArgumentException ex) { throw new BeanInstantiationException(ctor, "Illegal arguments for constructor" , ex); } catch (InvocationTargetException ex) { throw new BeanInstantiationException(ctor, "Constructor threw exception" , ex.getTargetException()); } }
CglibSubclassingInstantiationStrategy#instantiateWithMethodInjection
1 2 3 4 5 6 7 8 9 10 11 12 @Override protected Object instantiateWithMethodInjection (RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) { return instantiateWithMethodInjection(bd, beanName, owner, null ); }@Override protected Object instantiateWithMethodInjection (RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner, @Nullable Constructor<?> ctor, Object... args) { return new CglibSubclassCreator(bd, owner).instantiate(ctor, args); }
CglibSubclassCreator#instantiate
创建一个GCLIB的增强器子类
如果传入构造方法是空的话就用BeanUtils进行GCLIB子类的实例化,否则就用增强子类的构造方法直接实例化。
然后设置方法拦截器,这样调用的时候就会进入拦截器里,就可以去容器里寻找,有就直接返回,没有就创建。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public Object instantiate (@Nullable Constructor<?> ctor, Object... args) { Class<?> subclass = createEnhancedSubclass(this .beanDefinition); Object instance; if (ctor == null ) { instance = BeanUtils.instantiateClass(subclass); } else { try { Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes()); instance = enhancedSubclassConstructor.newInstance(args); } catch (Exception ex) { throw new BeanInstantiationException(this .beanDefinition.getBeanClass(), "Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]" , ex); } } Factory factory = (Factory) instance; factory.setCallbacks(new Callback[] {NoOp.INSTANCE, new LookupOverrideMethodInterceptor(this .beanDefinition, this .owner), new ReplaceOverrideMethodInterceptor(this .beanDefinition, this .owner)}); return instance; }
至此,spring bean 实例化就已经完成了,但是此时的Bean只实例化出来,什么都没有做,是一个白板
applyMergedBeanDefinitionPostProcessors 处理器修改合并bean定义
前面实例化完成了,之后要进行处理器的处理,也就是 MergedBeanDefinitionPostProcessor
处理器的处理:
1 2 3 4 5 protected void applyMergedBeanDefinitionPostProcessors (RootBeanDefinition mbd, Class<?> beanType, String beanName) { for (MergedBeanDefinitionPostProcessor processor : getBeanPostProcessorCache().mergedDefinition) { processor.postProcessMergedBeanDefinition(mbd, beanType, beanName); } }
MergedBeanDefinitionPostProcessor 的实现有很多,但是绝大多数为spring定义的
CommonAnnotationBeanPostProcessor
AutowiredAnnotationBeanPostProcessor
实现待补充
AutowiredAnnotationBeanPostProcessor#applyMergedBeanDefinitionPostProcessors主要做的事就是用处理器把属性和方法上的自动装配的信息记录下来,放到bean定义里,后面进行填充的时候可以用到,其中就包括CommonAnnotationBeanPostProcessor的生命周期方法和webService,ejb,Resource注解信息以及AutowiredAnnotationBeanPostProcessor的Autowired和Value注解信息。
populateBean 属性填充 InstantiationAwareBeanPostProcessor 1 2 3 4 5 6 7 8 9 10 11 12 13 if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) { if (!bp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) { return ; } } }
获取自动装配模式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null );int resolvedAutowireMode = mbd.getResolvedAutowireMode();if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) { MutablePropertyValues newPvs = new MutablePropertyValues(pvs); if (resolvedAutowireMode == AUTOWIRE_BY_NAME) { autowireByName(beanName, mbd, bw, newPvs); } if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) { autowireByType(beanName, mbd, bw, newPvs); } pvs = newPvs; }
InstantiationAwareBeanPostProcessor#postProcessProperties 这里会进行InstantiationAwareBeanPostProcessor处理器的postProcessProperties处理;拓展点
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 boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE); PropertyDescriptor[] filteredPds = null ;if (hasInstAwareBpps) { if (pvs == null ) { pvs = mbd.getPropertyValues(); } for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) { PropertyValues pvsToUse = bp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName); if (pvsToUse == null ) { if (filteredPds == null ) { filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); } pvsToUse = bp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); if (pvsToUse == null ) { return ; } } pvs = pvsToUse; } }if (needsDepCheck) { if (filteredPds == null ) { filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); } checkDependencies(beanName, mbd, filteredPds, pvs); }if (pvs != null ) { applyPropertyValues(beanName, mbd, bw, pvs); }
ImportAwareBeanPostProcessor#postProcessProperties
这个就是前面如果配置类有Configuration注解,会进行动态代理,会实现EnhancedConfiguration接口,里面有个setBeanFactory接口方法,会传入beanFactory。
1 2 3 4 5 6 7 8 9 @Override public PropertyValues postProcessProperties (@Nullable PropertyValues pvs, Object bean, String beanName) { if (bean instanceof EnhancedConfiguration) { ((EnhancedConfiguration) bean).setBeanFactory(this .beanFactory); } return pvs; }
CommonAnnotationBeanPostProcessor
的 postProcessProperties
处理 @Resource 注解
这里就处理我们前面 applyMergedBeanDefinitionPostProcessors
处理注入注解元数据。
1 2 3 4 5 6 7 8 9 10 11 @Override public PropertyValues postProcessProperties (PropertyValues pvs, Object bean, String beanName) { InjectionMetadata metadata = findResourceMetadata(beanName, bean.getClass(), pvs); try { metadata.inject(bean, beanName, pvs); } catch (Throwable ex) { throw new BeanCreationException(beanName, "Injection of resource dependencies failed" , ex); } return pvs; }
1 2 3 4 5 6 7 8 9 10 11 12 13 public void inject (Object target, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable { Collection<InjectedElement> checkedElements = this .checkedElements; Collection<InjectedElement> elementsToIterate = (checkedElements != null ? checkedElements : this .injectedElements); if (!elementsToIterate.isEmpty()) { for (InjectedElement element : elementsToIterate) { if (logger.isTraceEnabled()) { logger.trace("Processing injected element of bean '" + beanName + "': " + element); } element.inject(target, beanName, pvs); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 protected void inject (Object target, @Nullable String requestingBeanName, @Nullable PropertyValues pvs) throws Throwable { if (this .isField) { Field field = (Field) this .member; ReflectionUtils.makeAccessible(field); field.set(target, getResourceToInject(target, requestingBeanName)); } else { if (checkPropertySkipping(pvs)) { return ; } try { Method method = (Method) this .member; ReflectionUtils.makeAccessible(method); method.invoke(target, getResourceToInject(target, requestingBeanName)); } catch (InvocationTargetException ex) { throw ex.getTargetException(); } } }
ResourceElement#getResourceToInject
1 2 3 4 5 @Override protected Object getResourceToInject (Object target, @Nullable String requestingBeanName) { return (this .lazyLookup ? buildLazyResourceProxy(this , requestingBeanName) : getResource(this , requestingBeanName)); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 protected Object getResource (LookupElement element, @Nullable String requestingBeanName) throws NoSuchBeanDefinitionException { if (StringUtils.hasLength(element.mappedName)) { return this .jndiFactory.getBean(element.mappedName, element.lookupType); } if (this .alwaysUseJndiLookup) { return this .jndiFactory.getBean(element.name, element.lookupType); } if (this .resourceFactory == null ) { throw new NoSuchBeanDefinitionException(element.lookupType, "No resource factory configured - specify the 'resourceFactory' property" ); } return autowireResource(this .resourceFactory, element, requestingBeanName); }
会调到 CommonAnnotationBeanPostProcessor#autowireResource
这里主要就是根据依赖描述进行工厂的解析,最后都是调用getBean(String name, Class requiredType)方法,最后获取之后再注册到依赖映射里。其实这个有个resolveBeanByName,看起来好像是名字优先,其实也会获取类型的。
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 protected Object autowireResource (BeanFactory factory, LookupElement element, @Nullable String requestingBeanName) throws NoSuchBeanDefinitionException { Object resource; Set<String> autowiredBeanNames; String name = element.name; if (factory instanceof AutowireCapableBeanFactory) { AutowireCapableBeanFactory beanFactory = (AutowireCapableBeanFactory) factory; DependencyDescriptor descriptor = element.getDependencyDescriptor(); if (this .fallbackToDefaultTypeMatch && element.isDefaultName && !factory.containsBean(name)) { autowiredBeanNames = new LinkedHashSet<>(); resource = beanFactory.resolveDependency(descriptor, requestingBeanName, autowiredBeanNames, null ); if (resource == null ) { throw new NoSuchBeanDefinitionException(element.getLookupType(), "No resolvable resource object" ); } } else { resource = beanFactory.resolveBeanByName(name, descriptor); autowiredBeanNames = Collections.singleton(name); } } else { resource = factory.getBean(name, element.lookupType); autowiredBeanNames = Collections.singleton(name); } if (factory instanceof ConfigurableBeanFactory) { ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) factory; for (String autowiredBeanName : autowiredBeanNames) { if (requestingBeanName != null && beanFactory.containsBean(autowiredBeanName)) { beanFactory.registerDependentBean(autowiredBeanName, requestingBeanName); } } } return resource; }
AutowiredAnnotationBeanPostProcessor#postProcessProperties
处理 @Autowire 注解
和前面CommonAnnotationBeanPostProcessor的很像,都是这个模板。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Override public PropertyValues postProcessProperties (PropertyValues pvs, Object bean, String beanName) { InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass(), pvs); try { metadata.inject(bean, beanName, pvs); } catch (BeanCreationException ex) { throw ex; } catch (Throwable ex) { throw new BeanCreationException(beanName, "Injection of autowired dependencies failed" , ex); } return pvs; }
但是最后是AutowiredFieldElement类型的方法,前面是ResourceElement的,他们都是InjectedElement的子类,ResourceElement把处理属性和方法放一起判断处理,自动装配的AutowiredFieldElement和AutowiredMethodElement是分开处理的。
AutowiredFieldElement#inject
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 @Override protected void inject (Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable { Field field = (Field) this .member; Object value; if (this .cached) { value = resolvedCachedArgument(beanName, this .cachedFieldValue); } else { DependencyDescriptor desc = new DependencyDescriptor(field, this .required); desc.setContainingClass(bean.getClass()); Set<String> autowiredBeanNames = new LinkedHashSet<>(1 ); Assert.state(beanFactory != null , "No BeanFactory available" ); TypeConverter typeConverter = beanFactory.getTypeConverter(); try { value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter); } catch (BeansException ex) { throw new UnsatisfiedDependencyException(null , beanName, new InjectionPoint(field), ex); } synchronized (this ) { if (!this .cached) { if (value != null || this .required) { this .cachedFieldValue = desc; registerDependentBeans(beanName, autowiredBeanNames); if (autowiredBeanNames.size() == 1 ) { String autowiredBeanName = autowiredBeanNames.iterator().next(); if (beanFactory.containsBean(autowiredBeanName) && beanFactory.isTypeMatch(autowiredBeanName, field.getType())) { this .cachedFieldValue = new ShortcutDependencyDescriptor( desc, autowiredBeanName, field.getType()); } } } else { this .cachedFieldValue = null ; } this .cached = true ; } } } if (value != null ) { ReflectionUtils.makeAccessible(field); field.set(bean, value); } }
AutowiredAnnotationBeanPostProcessor#resolvedCachedArgument
1 2 3 4 5 6 7 8 9 10 11 @Nullable private Object resolvedCachedArgument (@Nullable String beanName, @Nullable Object cachedArgument) { if (cachedArgument instanceof DependencyDescriptor) { DependencyDescriptor descriptor = (DependencyDescriptor) cachedArgument; Assert.state(this .beanFactory != null , "No BeanFactory available" ); return this .beanFactory.resolveDependency(descriptor, beanName, null , null ); } else { return cachedArgument; } }
关于属性主注入:
其实不管哪种注入的注解,原理都是一样的,都要根据名字和类型去容器里找,然后建立依赖关系
DefaultListableBeanFactory#resolveDependency
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 @Override @Nullable public Object resolveDependency (DependencyDescriptor descriptor, @Nullable String requestingBeanName, @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException { descriptor.initParameterNameDiscovery(getParameterNameDiscoverer()); if (Optional.class == descriptor.getDependencyType()) { return createOptionalDependency(descriptor, requestingBeanName); } else if (ObjectFactory.class == descriptor.getDependencyType() || ObjectProvider.class == descriptor.getDependencyType()) { return new DependencyObjectProvider(descriptor, requestingBeanName); } else if (javaxInjectProviderClass == descriptor.getDependencyType()) { return new Jsr330Factory().createDependencyProvider(descriptor, requestingBeanName); } else { Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary( descriptor, requestingBeanName); if (result == null ) { result = doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter); } return result; } }
DefaultListableBeanFactory#doResolveDependency
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 @Nullable public Object doResolveDependency (DependencyDescriptor descriptor, @Nullable String beanName, @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException { InjectionPoint previousInjectionPoint = ConstructorResolver.setCurrentInjectionPoint(descriptor); try { Object shortcut = descriptor.resolveShortcut(this ); if (shortcut != null ) { return shortcut; } Class<?> type = descriptor.getDependencyType(); Object value = getAutowireCandidateResolver().getSuggestedValue(descriptor); if (value != null ) { if (value instanceof String) { String strVal = resolveEmbeddedValue((String) value); BeanDefinition bd = (beanName != null && containsBean(beanName) ? getMergedBeanDefinition(beanName) : null ); value = evaluateBeanDefinitionString(strVal, bd); } TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter()); try { return converter.convertIfNecessary(value, type, descriptor.getTypeDescriptor()); } catch (UnsupportedOperationException ex) { return (descriptor.getField() != null ? converter.convertIfNecessary(value, type, descriptor.getField()) : converter.convertIfNecessary(value, type, descriptor.getMethodParameter())); } } Object multipleBeans = resolveMultipleBeans(descriptor, beanName, autowiredBeanNames, typeConverter); if (multipleBeans != null ) { return multipleBeans; } Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor); if (matchingBeans.isEmpty()) { if (isRequired(descriptor)) { raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor); } return null ; } String autowiredBeanName; Object instanceCandidate; if (matchingBeans.size() > 1 ) { autowiredBeanName = determineAutowireCandidate(matchingBeans, descriptor); if (autowiredBeanName == null ) { if (isRequired(descriptor) || !indicatesMultipleBeans(type)) { return descriptor.resolveNotUnique(descriptor.getResolvableType(), matchingBeans); } else { return null ; } } instanceCandidate = matchingBeans.get(autowiredBeanName); } else { Map.Entry<String, Object> entry = matchingBeans.entrySet().iterator().next(); autowiredBeanName = entry.getKey(); instanceCandidate = entry.getValue(); } if (autowiredBeanNames != null ) { autowiredBeanNames.add(autowiredBeanName); } if (instanceCandidate instanceof Class) { instanceCandidate = descriptor.resolveCandidate(autowiredBeanName, type, this ); } Object result = instanceCandidate; if (result instanceof NullBean) { if (isRequired(descriptor)) { raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor); } result = null ; } if (!ClassUtils.isAssignableValue(type, result)) { throw new BeanNotOfRequiredTypeException(autowiredBeanName, type, instanceCandidate.getClass()); } return result; } finally { ConstructorResolver.setCurrentInjectionPoint(previousInjectionPoint); } }
@AutoWire 自动注入,现根据type,后根据 name
initializeBean 初始化 经过前面两步,实例已经初始化,属性也已经注入进去,此时就应该执行初始化方法
1 2 exposedObject = initializeBean(beanName, exposedObject, mbd);
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 protected Object initializeBean (String beanName, Object bean, @Nullable RootBeanDefinition mbd) { if (System.getSecurityManager() != null ) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { invokeAwareMethods(beanName, bean); return null ; }, getAccessControlContext()); } else { invokeAwareMethods(beanName, bean); } Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { invokeInitMethods(beanName, wrappedBean, mbd); } catch (Throwable ex) { throw new BeanCreationException( (mbd != null ? mbd.getResourceDescription() : null ), beanName, "Invocation of init method failed" , ex); } if (mbd == null || !mbd.isSynthetic()) { wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; }
invokeAwareMethods 处理BeanNameAware,BeanClassLoaderAware,BeanFactoryAware接口方法,这里BeanFactoryAware的接口要注意,如果前面配置类有CGLIB动态代理的话,这里这个方法就重复了,也就是setBeanFactory被重复调用了两次,前面一次是在EnhancedConfiguration接口的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 private void invokeAwareMethods (String beanName, Object bean) { if (bean instanceof Aware) { if (bean instanceof BeanNameAware) { ((BeanNameAware) bean).setBeanName(beanName); } if (bean instanceof BeanClassLoaderAware) { ClassLoader bcl = getBeanClassLoader(); if (bcl != null ) { ((BeanClassLoaderAware) bean).setBeanClassLoader(bcl); } } if (bean instanceof BeanFactoryAware) { ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this ); } } }
applyBeanPostProcessorsBeforeInitialization 所有BeanPostProcessor 处理,如果有返回null了就直接返回,否则就把处理后的对象替换原来的,返回最新对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Override public Object applyBeanPostProcessorsBeforeInitialization (Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor processor : getBeanPostProcessors()) { Object current = processor.postProcessBeforeInitialization(result, beanName); if (current == null ) { return result; } result = current; } return result; }
这里是一个拓展点,这里说几个常用的 spring 的实现
ApplicationContextAwareProcessor#postProcessBeforeInitialization
如果发现bean有实现那一堆接口任意一个的话,就会执行invokeAwareInterfaces方法。
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 @Override @Nullable public Object postProcessBeforeInitialization (Object bean, String beanName) throws BeansException { if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware || bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware || bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)){ return bean; } AccessControlContext acc = null ; if (System.getSecurityManager() != null ) { acc = this .applicationContext.getBeanFactory().getAccessControlContext(); } if (acc != null ) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { invokeAwareInterfaces(bean); return null ; }, acc); } else { invokeAwareInterfaces(bean); } return bean; }
ApplicationContextAwareProcessor#invokeAwareInterfaces
其实就是设置一些值,方便外部获取,做一些扩展。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 private void invokeAwareInterfaces (Object bean) { if (bean instanceof EnvironmentAware) { ((EnvironmentAware) bean).setEnvironment(this .applicationContext.getEnvironment()); } if (bean instanceof EmbeddedValueResolverAware) { ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this .embeddedValueResolver); } if (bean instanceof ResourceLoaderAware) { ((ResourceLoaderAware) bean).setResourceLoader(this .applicationContext); } if (bean instanceof ApplicationEventPublisherAware) { ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this .applicationContext); } if (bean instanceof MessageSourceAware) { ((MessageSourceAware) bean).setMessageSource(this .applicationContext); } if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware) bean).setApplicationContext(this .applicationContext); } }
ConfigurationClassPostProcessor内部类ImportAwareBeanPostProcessor#postProcessBeforeInitialization
如果是ImportAware类型的,就会设置bean的注解信息。
1 2 3 4 5 6 7 8 9 10 11 @Override public Object postProcessBeforeInitialization (Object bean, String beanName) { if (bean instanceof ImportAware) { ImportRegistry ir = this .beanFactory.getBean(IMPORT_REGISTRY_BEAN_NAME, ImportRegistry.class); AnnotationMetadata importingClass = ir.getImportingClassFor(ClassUtils.getUserClass(bean).getName()); if (importingClass != null ) { ((ImportAware) bean).setImportMetadata(importingClass); } } return bean; }
InitDestroyAnnotationBeanPostProcessor#postProcessBeforeInitialization
生命周期元数据,这个时候要进行调用了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Override public Object postProcessBeforeInitialization (Object bean, String beanName) throws BeansException { LifecycleMetadata metadata = findLifecycleMetadata(bean.getClass()); try { metadata.invokeInitMethods(bean, beanName); } catch (InvocationTargetException ex) { throw new BeanCreationException(beanName, "Invocation of init method failed" , ex.getTargetException()); } catch (Throwable ex) { throw new BeanCreationException(beanName, "Failed to invoke init method" , ex); } return bean; }
1 2 3 4 5 6 7 8 9 10 11 12 13 public void invokeInitMethods (Object target, String beanName) throws Throwable { Collection<LifecycleElement> checkedInitMethods = this .checkedInitMethods; Collection<LifecycleElement> initMethodsToIterate = (checkedInitMethods != null ? checkedInitMethods : this .initMethods); if (!initMethodsToIterate.isEmpty()) { for (LifecycleElement element : initMethodsToIterate) { if (logger.isTraceEnabled()) { logger.trace("Invoking init method on bean '" + beanName + "': " + element.getMethod()); } element.invoke(target); } } }
1 2 3 4 public void invoke (Object target) throws Throwable { ReflectionUtils.makeAccessible(this .method); this .method.invoke(target, (Object[]) null ); }
invokeInitMethods 执行初始化方法
如果实现了 InitializingBean 接口的话,就可能要调用 afterPropertiesSet 方法,这是一个回调方法
调用自定义的初始化方法 invokeCustomInitMethod
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 protected void invokeInitMethods (String beanName, Object bean, @Nullable RootBeanDefinition mbd) throws Throwable { boolean isInitializingBean = (bean instanceof InitializingBean); if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet" ))) { if (logger.isTraceEnabled()) { logger.trace("Invoking afterPropertiesSet() on bean with name '" + beanName + "'" ); } if (System.getSecurityManager() != null ) { try { AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> { ((InitializingBean) bean).afterPropertiesSet(); return null ; }, getAccessControlContext()); } catch (PrivilegedActionException pae) { throw pae.getException(); } } else { ((InitializingBean) bean).afterPropertiesSet(); } } if (mbd != null && bean.getClass() != NullBean.class) { String initMethodName = mbd.getInitMethodName(); if (StringUtils.hasLength(initMethodName) && !(isInitializingBean && "afterPropertiesSet" .equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName)) { invokeCustomInitMethod(beanName, bean, mbd); } } }
invokeCustomInitMethod
获取bean的自定义初始化方法,如果自身或者父类是接口类型的话,就反射出接口方法来,最后调用。
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 protected void invokeCustomInitMethod (String beanName, Object bean, RootBeanDefinition mbd) throws Throwable { String initMethodName = mbd.getInitMethodName(); Assert.state(initMethodName != null , "No init method set" ); Method initMethod = (mbd.isNonPublicAccessAllowed() ? BeanUtils.findMethod(bean.getClass(), initMethodName) : ClassUtils.getMethodIfAvailable(bean.getClass(), initMethodName)); if (initMethod == null ) { if (mbd.isEnforceInitMethod()) { throw new BeanDefinitionValidationException("Could not find an init method named '" + initMethodName + "' on bean with name '" + beanName + "'" ); } else { if (logger.isTraceEnabled()) { logger.trace("No default init method named '" + initMethodName + "' found on bean with name '" + beanName + "'" ); } return ; } } if (logger.isTraceEnabled()) { logger.trace("Invoking init method '" + initMethodName + "' on bean with name '" + beanName + "'" ); } Method methodToInvoke = ClassUtils.getInterfaceMethodIfPossible(initMethod); if (System.getSecurityManager() != null ) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { ReflectionUtils.makeAccessible(methodToInvoke); return null ; }); try { AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> methodToInvoke.invoke(bean), getAccessControlContext()); } catch (PrivilegedActionException pae) { InvocationTargetException ex = (InvocationTargetException) pae.getException(); throw ex.getTargetException(); } } else { try { ReflectionUtils.makeAccessible(methodToInvoke); methodToInvoke.invoke(bean); } catch (InvocationTargetException ex) { throw ex.getTargetException(); } } }
ClassUtils#getInterfaceMethodIfPossible
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public static Method getInterfaceMethodIfPossible (Method method) { if (!Modifier.isPublic(method.getModifiers()) || method.getDeclaringClass().isInterface()) { return method; } return interfaceMethodCache.computeIfAbsent(method, key -> { Class<?> current = key.getDeclaringClass(); while (current != null && current != Object.class) { Class<?>[] ifcs = current.getInterfaces(); for (Class<?> ifc : ifcs) { try { return ifc.getMethod(key.getName(), key.getParameterTypes()); } catch (NoSuchMethodException ex) { } } current = current.getSuperclass(); } return key; }); }
applyBeanPostProcessorsAfterInitialization 调用初始化后处理器,Aop就是在此处处理的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Override public Object applyBeanPostProcessorsAfterInitialization (Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor processor : getBeanPostProcessors()) { Object current = processor.postProcessAfterInitialization(result, beanName); if (current == null ) { return result; } result = current; } return result; }
registerDisposableBeanIfNecessary 销毁回调 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 protected void registerDisposableBeanIfNecessary (String beanName, Object bean, RootBeanDefinition mbd) { AccessControlContext acc = (System.getSecurityManager() != null ? getAccessControlContext() : null ); if (!mbd.isPrototype() && requiresDestruction(bean, mbd)) { if (mbd.isSingleton()) { registerDisposableBean(beanName, new DisposableBeanAdapter( bean, beanName, mbd, getBeanPostProcessorCache().destructionAware, acc)); } else { Scope scope = this .scopes.get(mbd.getScope()); if (scope == null ) { throw new IllegalStateException("No Scope registered for scope name '" + mbd.getScope() + "'" ); } scope.registerDestructionCallback(beanName, new DisposableBeanAdapter( bean, beanName, mbd, getBeanPostProcessorCache().destructionAware, acc)); } } }
AbstractBeanFactory#requiresDestruction
1 2 3 4 5 protected boolean requiresDestruction (Object bean, RootBeanDefinition mbd) { return (bean.getClass() != NullBean.class && (DisposableBeanAdapter.hasDestroyMethod(bean, mbd) || (hasDestructionAwareBeanPostProcessors() && DisposableBeanAdapter.hasApplicableProcessors( bean, getBeanPostProcessorCache().destructionAware)))); }
DisposableBeanAdapter#hasDestroyMethod
实现 DisposableBean 和 AutoCloseable 接口的,或者 INFER_METHOD 等于 destroyMethodName 且有 close,shutdown 方法名的
1 2 3 4 5 6 7 8 9 10 11 public static boolean hasDestroyMethod (Object bean, RootBeanDefinition beanDefinition) { if (bean instanceof DisposableBean || bean instanceof AutoCloseable) { return true ; } String destroyMethodName = beanDefinition.getDestroyMethodName(); if (AbstractBeanDefinition.INFER_METHOD.equals(destroyMethodName)) { return (ClassUtils.hasMethod(bean.getClass(), CLOSE_METHOD_NAME) || ClassUtils.hasMethod(bean.getClass(), SHUTDOWN_METHOD_NAME)); } return StringUtils.hasLength(destroyMethodName); }
DisposableBeanAdapter#hasApplicableProcessors
如果有处理器来处理的话。
1 2 3 4 5 6 7 8 9 10 public static boolean hasApplicableProcessors (Object bean, List<DestructionAwareBeanPostProcessor> postProcessors) { if (!CollectionUtils.isEmpty(postProcessors)) { for (DestructionAwareBeanPostProcessor processor : postProcessors) { if (processor.requiresDestruction(bean)) { return true ; } } } return false ; }
DefaultSingletonBeanRegistry#registerDisposableBean
其实就是把bean注册进去,到时候回调就行。
1 2 3 4 5 public void registerDisposableBean (String beanName, DisposableBean bean) { synchronized (this .disposableBeans) { this .disposableBeans.put(beanName, bean); } }
至此,整个 getBean的流程都已经走完了。
循环依赖问题
后置处理器拓展问题
AOP的实现原理问题