原文釋出于: http://blog.ztgreat.cn/article/58
前言
在前面分析了Spring IOC的初始化過程的前半部分,今天分析一下初始化過程中一個非常重要的環節—初始化所有的 singleton beans
需要以下基礎内容:
Spring源碼分析:Spring IOC容器初始化(一)
Spring-BeanFactory源碼分析(一)
Spring-統一資源加載政策
Spring-BeanFactory源碼分析(二)
finishBeanFactoryInitialization
初始化 singleton beans 了,如果沒有設定懶加載,那麼 Spring 會在接下來初始化所有的 singleton beans。
bean的初始化過程如果要認真說的話那就很,很多地方就點到為止了,重點梳理重要的邏輯過程,其餘的可以自行專研。
放出源碼
AbstractApplicationContext
->
finishBeanFactoryInitialization
:
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
// 首先,初始化名字為 conversionService 的 Bean。這裡暫時不講
if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
beanFactory.setConversionService(
beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
}
//不管
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
// 這是 AspectJ 相關的内容,先不管
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
getBean(weaverAwareName);
}
// Stop using the temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(null);
// 當機 BeanDefinition,不再修改配置了
beanFactory.freezeConfiguration();
// Instantiate all remaining (non-lazy-init) singletons.
// 開始初始化 單例bean
beanFactory.preInstantiateSingletons();
}
重點當然是
beanFactory.preInstantiateSingletons()
,這個beanFactory是 DefaultListableBeanFactory 執行個體,現在我們又回到了DefaultListableBeanFactory 這個BeanFactory上了。
preInstantiateSingletons
放源碼
DefaultListableBeanFactory
->
preInstantiateSingletons
:
public void preInstantiateSingletons() throws BeansException {
// this.beanDefinitionNames 儲存了所有的 beanNames
List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
// 觸發所有的非懶加載的 singleton beans 的初始化操作
for (String beanName : beanNames) {
// 合并父 Bean 中的配置,注意 <bean id="" class="" parent="" /> 中的 parent,用的不多
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
// 非抽象、非懶加載的 singletons。如果配置了 'abstract = true',那是不需要初始化的
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
// 處理 FactoryBean
if (isFactoryBean(beanName)) {
// FactoryBean 的話,在 beanName 前面加上 ‘&’ 符号。再調用 getBean
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
if (bean instanceof FactoryBean) {
final FactoryBean<?> factory = (FactoryBean<?>) bean;
boolean isEagerInit;
// 判斷目前 FactoryBean 是否是 SmartFactoryBean 的實作,忽略算了
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
((SmartFactoryBean<?>) factory)::isEagerInit,
getAccessControlContext());
}
else {
isEagerInit = (factory instanceof SmartFactoryBean &&
((SmartFactoryBean<?>) factory).isEagerInit());
}
if (isEagerInit) {
getBean(beanName);
}
}
}
else {
// 對于普通的 Bean,隻要調用 getBean(beanName) 這個方法就可以進行初始化了
getBean(beanName);
}
}
}
// 到這裡說明所有的非懶加載的 singleton beans 已經完成了初始化
// 如果我們定義的 bean 是實作了 SmartInitializingSingleton 接口的,那麼在這裡得到回調,忽略
for (String beanName : beanNames) {
Object singletonInstance = getSingleton(beanName);
if (singletonInstance instanceof SmartInitializingSingleton) {
final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
smartSingleton.afterSingletonsInstantiated();
return null;
}, getAccessControlContext());
}
else {
smartSingleton.afterSingletonsInstantiated();
}
}
}
}
很明顯,我們需要進入到 getBean(beanName) 方法了,這個方法我們也經常用來從 BeanFactory 中擷取一個 Bean,而初始化的過程也封裝到了這個方法裡,不過再之前呢,需要插播一下關于FactoryBean的資訊,在最開始分析BeanFactory的時候,也有簡單提到,這裡再提一下,如果讀者已經明白FactoryBean,那麼可以跳過這部分。
FactoryBean
public interface FactoryBean<T> {
@Nullable
T getObject() throws Exception;
@Nullable
Class<?> getObjectType();
default boolean isSingleton() {
return true;
}
}
FactoryBean 其實很簡單,首先FactoryBean 是Bean,隻是這是一個非常特殊的Bean,這種特殊的bean會生産另一種bean, 對于普通的bean,通過BeanFactory 的 getBean方法可以擷取這個bean,而對于FactoryBean 來說,通過getBean 獲得的是 FactoryBean 生産的bean(實際會調用FactoryBean的
getObject
方法),而不是FactoryBean 本身,如果想要擷取FactoryBean 本身,那麼可以加
字首&
,那麼spring 就明白,原來你是需要FactoryBean 。這個可能會在後面AOP的部分,展開來講,這裡就先說這麼多了。
getBean
現在回到getBean 這上面來,代碼稍微有點長。
AbstractBeanFactory
->
getBean
:
@Override
public Object getBean(String name) throws BeansException {
return doGetBean(name, null, null, false);
}
doGetBean
protected <T> T doGetBean(final String name, final Class<T> requiredType,
final Object[] args, boolean typeCheckOnly) throws BeansException {
// 擷取一個 标準的 beanName,處理兩種情況:
//一個是前面說的 FactoryBean(前面帶 ‘&’),
//如果指定的是别名,将别名轉換為規範的Bean名稱
final String beanName = transformedBeanName(name);
Object bean;
// 檢查下是不是已經存在了,如果已經建立了的單例bean,會放入Map 中
Object sharedInstance = getSingleton(beanName);
// 但是如果 args 不為空的時候,那麼不管是否該bean已經存在都會重新建立
if (sharedInstance != null && args == null) {
// 下面這個方法:如果是普通 Bean 的話,直接傳回 sharedInstance,
// 如果是 FactoryBean 的話,傳回它建立的那個執行個體對象,調用FactoryBean的getObject 方法
//這裡就不展開了
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
else {
// 建立過了此 beanName 的 prototype 類型的 bean,那麼抛異常,
// 往往是因為陷入了循環引用
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
// 檢查一下這個 BeanDefinition 在容器中是否存在
BeanFactory parentBeanFactory = getParentBeanFactory();
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
// 如果目前容器不存在這個 BeanDefinition,看看父容器中有沒有
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 {
// No args -> delegate to standard getBean method.
return parentBeanFactory.getBean(nameToLookup, requiredType);
}
}
if (!typeCheckOnly) {
// typeCheckOnly 為 false,将目前 beanName 放入一個 alreadyCreated 的 Set 集合中,标記一下。
markBeanAsCreated(beanName);
}
/*
* 到這裡的話,要準備建立 Bean 了,
* 對于 singleton 的 Bean 來說,容器中還沒建立過此 Bean;
* 對于 prototype 的 Bean 來說,本來就是要建立一個新的 Bean。
*/
try {
//根據指定Bean名稱擷取其父級的Bean定義,主要解決Bean繼承時子類
//合并父類公共屬性問題
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args);
// 先初始化依賴的所有 Bean
//檢查是不是有循環依賴,這裡的循環依賴和我們前面說的循環依賴又不一樣
//這裡的依賴指的是 depends-on 中定義的依賴
//depends-on用來表示一個Bean的執行個體化依靠另一個Bean先執行個體化。
//如果在一個bean A上定義了depend-on B那麼就表示:A 執行個體化前先執行個體化 B。
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方法,擷取目前Bean的依賴Bean
getBean(dep);
}
catch (NoSuchBeanDefinitionException ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
}
}
}
// 如果是 singleton scope 的,建立 singleton 的執行個體
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);
}
// 如果是 prototype scope 的,建立 prototype 的執行個體
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 {
// 如果不是 singleton 和 prototype 的話,需要委托給相應的實作類來處理
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方法
beforePrototypeCreation(beanName);
try {
return createBean(beanName, mbd, args);
}
finally {
//回調afterPrototypeCreation方法
afterPrototypeCreation(beanName);
}
});
//擷取給定Bean的執行個體對象
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;
}
}
//對建立的Bean執行個體對象進行類型檢查
if (requiredType != null && !requiredType.isInstance(bean)) {
try {
T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
if (convertedBean == null) {
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
return convertedBean;
}
catch (TypeMismatchException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to convert bean '" + name + "' to required type '" +
ClassUtils.getQualifiedName(requiredType) + "'", ex);
}
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
}
return (T) bean;
}
上面,我們可以看見在建立執行個體時做了判斷
- 如果Bean定義的單态模式(Singleton),則容器在建立之前先從緩存中查找,以確定整個容器中隻存在一個執行個體對象
- 如果Bean定義的是原型模式(Prototype),則容器每次都會建立一個新的執行個體對象。
- 兩者都不是,則根據Bean定義資源中配置的生命周期範圍,選擇執行個體化Bean的合适方法,這種在Web應用程式中 比較常用,如:request、session、application等生命周期
通過上面的代碼基本上了解大概邏輯是不成問題的,接下來肯定就是分析
createBean
方法了。
這裡我們要接觸一個新的類了 AbstractAutowireCapableBeanFactory,通過類名我們知道,這個是處理
@Autowired 注解
的,在spring中經常混用了 xml 和 注解 兩種方式的配置方式。
createBean
AbstractAutowireCapableBeanFactory
->
createBean
:
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
if (logger.isDebugEnabled()) {
logger.debug("Creating instance of bean '" + beanName + "'");
}
RootBeanDefinition mbdToUse = mbd;
// 確定 BeanDefinition 中的 Class 被加載
Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
mbdToUse = new RootBeanDefinition(mbd);
mbdToUse.setBeanClass(resolvedClass);
}
// 準備方法覆寫,這裡又涉及到一個概念:MethodOverrides,
// 它來自于 bean 定義中的 <replaced-method />
//沒怎麼了解
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.
//如果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 {
//建立 bean
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
return beanInstance;
}
catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
throw ex;
}
catch (Throwable ex) {
throw new BeanCreationException(
mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
}
}
繼續看
doCreateBean
方法吧。
doCreateBean
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
throws BeanCreationException {
// Instantiate the bean.
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
//移除BeanWrapper緩存
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
//建立 BeanWrapper
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
//獲得bean 執行個體
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.
//調用PostProcessor後置處理器
synchronized (mbd.postProcessingLock) {
if (!mbd.postProcessed) {
try {
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Post-processing of merged bean definition failed", ex);
}
mbd.postProcessed = true;
}
}
// Eagerly cache singletons to be able to resolve circular references
// even when triggered by lifecycle interfaces like BeanFactoryAware.
// 下面代碼是為了解決循環依賴的問題
//循環依賴問題,下篇文章來描述和舉例,這裡就不說了
//是以這裡如果不明白,那也沒關系,放一放
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
isSingletonCurrentlyInCreation(beanName));
//提前曝光bean
if (earlySingletonExposure) {
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}
// Initialize the bean instance.
Object exposedObject = bean;
try {
//執行個體化後,需要進行依賴注入
populateBean(beanName, mbd, instanceWrapper);
// 這裡就是處理 bean 初始化完成後的各種回調,例如init-method 配置,BeanPostProcessor接口
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) {
//如果已經送出曝光了bean,那麼就從緩存中擷取bean
Object earlySingletonReference = getSingleton(beanName, false);
if (earlySingletonReference != null) {
//根據名稱擷取的以注冊的Bean和正在執行個體化的Bean是同一個
if (exposedObject == bean) {
exposedObject = earlySingletonReference;
}
else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
String[] dependentBeans = getDependentBeans(beanName);
Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
for (String dependentBean : dependentBeans) {
if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
actualDependentBeans.add(dependentBean);
}
}
if (!actualDependentBeans.isEmpty()) {
throw new BeanCurrentlyInCreationException(beanName,
"Bean with name '" + beanName + "' has been injected into other beans [" +
StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
"] in its raw version as part of a circular reference, but has eventually been " +
"wrapped. This means that said other beans do not use the final version of the " +
"bean. This is often the result of over-eager type matching - consider using " +
"'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
}
}
}
}
// Register bean as disposable.
try {
registerDisposableBeanIfNecessary(beanName, bean, mbd);
}
catch (BeanDefinitionValidationException ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
}
return exposedObject;
}
到這裡,基本上簡單的分析了 doCreateBean 方法,整個bean就已經初始化完成了,這裡面有三個重點的方法(過程)
1、建立 Bean 執行個體(
createBeanInstance
) 方法,
2、依賴注入(
populateBean
) 方法,
3、一系列初始化或者回調(
initializeBean
)。
注意了,接下來的這三個方法要認真說那也是極其複雜的,很多地方我就點到為止了,感興趣的讀者可以自己往裡看,最好就是碰到不懂的,自己寫代碼去調試它。
createBeanInstance -建立 Bean 執行個體
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
// Make sure bean class is actually resolved at this point.
Class<?> beanClass = resolveBeanClass(mbd, beanName);
// 校驗 類的通路權限
if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
}
Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
if (instanceSupplier != null) {
return obtainFromSupplier(instanceSupplier, beanName);
}
if (mbd.getFactoryMethodName() != null) {
// 采用工廠方法執行個體化,配置 factory-method
return instantiateUsingFactoryMethod(beanName, mbd, args);
}
// Shortcut when re-creating the same bean...
// 如果不是第一次建立,比如第二次建立 prototype bean。
// 這種情況下,我們可以從第一次建立知道,采用無參構造函數,還是構造函數依賴注入 來完成執行個體化
// 這個可以通過代碼來測試,多次通過getbean(name)來擷取 prototype的bean
boolean resolved = false;
boolean autowireNecessary = false;
if (args == null) {
synchronized (mbd.constructorArgumentLock) {
if (mbd.resolvedConstructorOrFactoryMethod != null) {
resolved = true;
autowireNecessary = mbd.constructorArgumentsResolved;
}
}
}
if (resolved) {
if (autowireNecessary) {
/配置了自動裝配屬性,使用容器的自動裝配執行個體化
//容器的自動裝配是根據參數類型比對Bean的構造方法
return autowireConstructor(beanName, mbd, null, null);
}
else {
//使用預設的無參構造方法執行個體化
return instantiateBean(beanName, mbd);
}
}
// 判斷是否采用有參構造函數.
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (ctors != null ||
mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR ||
mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
//使用容器的自動裝配特性,調用比對的構造方法執行個體化
return autowireConstructor(beanName, mbd, ctors, args);
}
//使用預設的無參構造方法執行個體化
return instantiateBean(beanName, mbd);
}
當然接下來看最簡單的無參構成函數建立執行個體咯
instantiateBean
protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) {
try {
Object beanInstance;
final BeanFactory parent = this;
if (System.getSecurityManager() != null) {
beanInstance = AccessController.doPrivileged((PrivilegedAction<Object>) () ->
getInstantiationStrategy().instantiate(mbd, beanName, parent),
getAccessControlContext());
}
else {
//執行個體化
beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);
}
// 包裝一下
BeanWrapper bw = new BeanWrapperImpl(beanInstance);
//初始化BeanWrapper
//會設定 conversionService,注冊customEditors
initBeanWrapper(bw);
return bw;
}
catch (Throwable ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
}
}
我們看到createBeanInstance方法和instantiateBean的傳回值是 BeanWrapper,那這個BeanWrapper到底是什麼?
BeanWrapper相當于一個代理器,Spring通過BeanWrapper完成Bean屬性的填充工作。
在Bean執行個體被InstantiationStrategy建立出來之後,容器将Bean執行個體通過BeanWrapper包裝起來。
BeanWrapper還有三個頂級類接口,分别是
PropertyAccessor
和
PropertyEditorRegistry
,
TypeConverter
。PropertyAccessor接口定義了各種通路Bean屬性的方法,如setPropertyValue(String,Object),setPropertyValues(PropertyValues pvs)等,而PropertyEditorRegistry是屬性編輯器的系統資料庫。
TypeConverter 支援屬性值的類型轉換
是以BeanWrapper實作類BeanWrapperImpl具有了多重身份:
- Bean包裹器;
- 屬性通路器;
- 屬性編輯器系統資料庫。
- 屬性值的類型轉換
進入
beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);
看看
SimpleInstantiationStrategy
->
instantiate
:
public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
// 如果不存在方法覆寫(replaced-method 配置),那就使用 java 反射進行執行個體化,否則使用 CGLIB,
if (!bd.hasMethodOverrides()) {
Constructor<?> constructorToUse;
synchronized (bd.constructorArgumentLock) {
constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
if (constructorToUse == null) {
final Class<?> clazz = bd.getBeanClass();
if (clazz.isInterface()) {
throw new BeanInstantiationException(clazz, "Specified class is an interface");
}
try {
if (System.getSecurityManager() != null) {
constructorToUse = AccessController.doPrivileged(
(PrivilegedExceptionAction<Constructor<?>>) clazz::getDeclaredConstructor);
}
else {
constructorToUse = clazz.getDeclaredConstructor();
}
bd.resolvedConstructorOrFactoryMethod = constructorToUse;
}
catch (Throwable ex) {
throw new BeanInstantiationException(clazz, "No default constructor found", ex);
}
}
}
// 利用構造方法進行執行個體化
return BeanUtils.instantiateClass(constructorToUse);
}
else {
// Must generate CGLIB subclass.
// 存在方法覆寫,利用 CGLIB 來完成執行個體化,需要依賴于 CGLIB 生成子類,這裡就不展開了。
return instantiateWithMethodInjection(bd, beanName, owner);
}
}
ok,到這裡bean的執行個體化就完成了,就不繼續跟進去了,完成執行個體化後,還有一個重要的任務就是依賴注入,這個也是一個麻煩事情。
populateBean - 依賴注入
AbstractAutowireCapableBeanFactory
->
populateBean
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
if (bw == null) {
if (mbd.hasPropertyValues()) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
}
else {
// Skip property population phase for null instance.
return;
}
}
// Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
// state of the bean before properties are set. This can be used, for example,
// to support styles of field injection.
boolean continueWithPropertyPopulation = true;
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
//InstantiationAwareBeanPostProcessor 在執行個體前和執行個體後進行回調處理
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
//在設定屬性之前調用Bean的PostProcessor後置處理器
if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
continueWithPropertyPopulation = false;
break;
}
}
}
}
if (!continueWithPropertyPopulation) {
return;
}
// bean 執行個體的所有屬性
PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);
if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME ||
mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
// Add property values based on autowire by name if applicable.
if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) {
// 通過名字找到所有屬性值,如果是 bean 依賴,先初始化依賴的 bean。記錄依賴關系
autowireByName(beanName, mbd, bw, newPvs);
}
// Add property values based on autowire by type if applicable.
if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
// 通過類型裝配
autowireByType(beanName, mbd, bw, newPvs);
}
pvs = newPvs;
}
//檢查容器是否持有用于處理單态模式Bean關閉時的後置處理器
boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
//Bean執行個體對象沒有依賴(此依賴是depends-on),即沒有繼承基類
boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE);
if (hasInstAwareBpps || needsDepCheck) {
if (pvs == null) {
pvs = mbd.getPropertyValues();
}
PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
if (hasInstAwareBpps) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
//處理特殊的BeanPostProcessor
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
//使用BeanPostProcessor處理器處理屬性值
pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
if (pvs == null) {
return;
}
}
}
}
if (needsDepCheck) {
//為要設定的屬性進行依賴檢查
checkDependencies(beanName, mbd, filteredPds, pvs);
}
}
if (pvs != null) {
// 設定 bean 執行個體的屬性值
applyPropertyValues(beanName, mbd, bw, pvs);
}
}
繼續applyPropertyValues
applyPropertyValues
protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) {
if (pvs.isEmpty()) {
return;
}
if (System.getSecurityManager() != null && bw instanceof BeanWrapperImpl) {
//設定安全上下文
((BeanWrapperImpl) bw).setSecurityContext(getAccessControlContext());
}
MutablePropertyValues mpvs = null;
List<PropertyValue> original;
if (pvs instanceof MutablePropertyValues) {
mpvs = (MutablePropertyValues) pvs;
//屬性值已經轉換
if (mpvs.isConverted()) {
// Shortcut: use the pre-converted values as-is.
try {
//為執行個體化對象設定屬性值
bw.setPropertyValues(mpvs);
return;
}
catch (BeansException ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Error setting property values", ex);
}
}
//擷取屬性值對象的原始類型值
original = mpvs.getPropertyValueList();
}
else {
original = Arrays.asList(pvs.getPropertyValues());
}
//擷取使用者自定義的類型轉換
TypeConverter converter = getCustomTypeConverter();
if (converter == null) {
converter = bw;
}
//建立一個Bean定義屬性值解析器,将Bean定義中的屬性值解析為Bean執行個體對象 的實際值
BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter);
// Create a deep copy, resolving any references for values.
List<PropertyValue> deepCopy = new ArrayList<>(original.size());
boolean resolveNecessary = false;
for (PropertyValue pv : original) {
//屬性值已經轉換
if (pv.isConverted()) {
deepCopy.add(pv);
}
else {//屬性值需要轉換
String propertyName = pv.getName();
//原始的屬性值,即轉換之前的屬性值
Object originalValue = pv.getValue();
//轉換屬性值,例如将引用轉換為IoC容器中執行個體化對象引用
//如果屬性值是其它bean,且沒有建立,那麼就會先去建立這個bean
Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue);
Object convertedValue = resolvedValue;
//屬性值是否可以轉換
boolean convertible = bw.isWritableProperty(propertyName) &&
!PropertyAccessorUtils.isNestedOrIndexedProperty(propertyName);
if (convertible) {
//使用使用者自定義的類型轉換器轉換屬性值
convertedValue = convertForProperty(resolvedValue, propertyName, bw, converter);
}
//存儲轉換後的屬性值,避免每次屬性注入時的轉換工作
if (resolvedValue == originalValue) {
if (convertible) {
pv.setConvertedValue(convertedValue);
}
deepCopy.add(pv);
}
else if (convertible && originalValue instanceof TypedStringValue &&
!((TypedStringValue) originalValue).isDynamic() &&
!(convertedValue instanceof Collection || ObjectUtils.isArray(convertedValue))) {
pv.setConvertedValue(convertedValue);
deepCopy.add(pv);
}
else {
resolveNecessary = true;
deepCopy.add(new PropertyValue(pv, convertedValue));
}
}
}
if (mpvs != null && !resolveNecessary) {
//标記屬性值已經轉換過
mpvs.setConverted();
}
try {
//進行屬性依賴注入
bw.setPropertyValues(new MutablePropertyValues(deepCopy));
}
catch (BeansException ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Error setting property values", ex);
}
}
從上面代碼可以看出屬性轉換分為了兩種情況
- 屬性值類型不需要轉換時,不需要解析屬性值,直接準備進行依賴注入。
- 屬性值需要進行類型轉換時,如對其他對象的引用等,首先需要解析屬性值,然後對解析後的屬性值進行依賴注入。
對屬性值的解析是在
BeanDefinitionValueResolver
類中的
resolveValueIfNecessary
方法中進行的,這裡就不再展開了,可以看到裡面對list,set,map類型的解析。
initializeBean
屬性注入完成後,就可以"初始化"bean了,這一步其實就是處理各種回調了。
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareMethods(beanName, bean);
return null;
}, getAccessControlContext());
}
else {
// 如果 bean 實作了 BeanNameAware、BeanClassLoaderAware 或 BeanFactoryAware 接口,回調
invokeAwareMethods(beanName, bean);
}
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
// 處理 bean 中定義的 init-method,
// 或者如果 bean 實作了 InitializingBean 接口,調用 afterPropertiesSet() 方法
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()) {
// BeanPostProcessor 的 postProcessAfterInitialization 回調
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
這裡有個需要注意的地方,BeanPostProcessor 的兩個回調都發生在這邊,隻不過中間處理了 init-method。
到這裡Bean的初始化過程總算完成了,同時IOC的初始化過程也就差不多完成了,最後還有一步 finishRefresh。
finishRefresh
這個簡單多了,初始化一下lifecycle processor,釋出容器完成初始化事件。
protected void finishRefresh() {
// Clear context-level resource caches (such as ASM metadata from scanning).
clearResourceCaches();
// Initialize lifecycle processor for this context.
initLifecycleProcessor();
// Propagate refresh to lifecycle processor first.
getLifecycleProcessor().onRefresh();
// Publish the final event.
publishEvent(new ContextRefreshedEvent(this));
// Participate in LiveBeansView MBean, if active.
LiveBeansView.registerApplicationContext(this);
}
至此,SpringIOC 初始化過程就完成了,整個過程還是非常複雜的,裡面涉及到很多東西,隻是梳理了一下整個邏輯過程,對于一些細節的地方,在需要用的時候再進行分析。
參考
Spring IOC 容器源碼分析
Spring 技術内幕