基于springboot bean的實例化過程和屬性注入過程
bean的實例化過程和屬性注入過程
了解過springboot的加載流程的都知道springboot初始化bean都在refresh方法中。
這個方法代碼如下:
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.新建beanFactory
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
// 加載實現(xiàn)beanFactoryPostProcessor的bean,bean定義的時候執(zhí)行
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
// 加載實現(xiàn)了beanPostProcessor,在bean實例化前、后執(zhí)行
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
//實例化非懶加載的bean、bean封裝、屬性注入、注解注入(主要使用BeanPostProcessor或子類實現(xiàn))等
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
這里我們主要看下finishBeanfactoryInitialization方法。此方法實現(xiàn)了bean的實例和屬性注入等。進入此方法的最后一行。
// Instantiate all remaining (non-lazy-init) singletons. beanFactory.preInstantiateSingletons();
注釋寫的很清楚,實例化剩余非懶加載的bean。此方法的實現(xiàn)是核心類DefaultLisListableBeanFactory實現(xiàn)。這個方法中有個判斷:bean是否是beanFactory的實現(xiàn)類。
如果是則獲取bean的時候會從beanFactory實現(xiàn)類的getObject中獲取,我們重點看看getBean這個方法。
getBean是spring中最重要、最牛逼的方法之一,具體的邏輯是通過doGetBean方法處理的。
我們看下doGetBean方法,方法很長。我們分成幾個部分去看。
1、先判斷是否已經(jīng)存在緩存中,代碼如下:
if (sharedInstance != null && args == null) {
if (logger.isDebugEnabled()) {
if (isSingletonCurrentlyInCreation(beanName)) {
logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
"' that is not fully initialized yet - a consequence of a circular reference");
}
else {
logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
}
}
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
2、從父beanfactory中獲取
BeanFactory parentBeanFactory = getParentBeanFactory();
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
// Not found -> check parent.
String nameToLookup = originalBeanName(name);
if (parentBeanFactory instanceof AbstractBeanFactory) {
return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
nameToLookup, requiredType, args, typeCheckOnly);
}
else if (args != null) {
// Delegation to parent with explicit args.
return (T) parentBeanFactory.getBean(nameToLookup, args);
}
else {
// No args -> delegate to standard getBean method.
return parentBeanFactory.getBean(nameToLookup, requiredType);
}
}
3、直接創(chuàng)建RootBeanDefinition
//mark 給此bean 馬克一下。防止重復(fù)創(chuàng)建
if (!typeCheckOnly) {
markBeanAsCreated(beanName);
}
try {
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args);
// Guarantee initialization of beans that the current bean depends on.
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
for (String dep : dependsOn) {
if (isDependent(beanName, dep)) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
}
registerDependentBean(dep, beanName);
getBean(dep);
}
}
4、是singleton還是prototype類型的,根據(jù)不同類型去實例化bean,代碼只貼了單例的類型:
// Create bean instance.
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);
5、檢查bean的類型是否匹配
// Check if required type matches the type of the actual bean instance.
if (requiredType != null && !requiredType.isInstance(bean)) {
try {
T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
if (convertedBean == null) {
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
return convertedBean;
}
catch (TypeMismatchException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to convert bean '" + name + "' to required type '" +
ClassUtils.getQualifiedName(requiredType) + "'", ex);
}
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
}
整個doGetBean方法改成這5個部分,重點看下第4個部分中的createBean和getObjectForBeanInstance方法。
1、createBean方法,里面主要是2個部分,bean直接是通過BeanPostProcessor的postProcessBeforeIntantiation方法獲取的。
注釋也是描述的很清楚:Give BeanPostProcessors a chance to return a proxy instead of the bean instance(英語渣渣不敢瞎翻譯誤導(dǎo)大家,理解就好)
代碼如下:
try {
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
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);
}
第一個部分自己實現(xiàn),那么createBean方法第2個部分毋庸置疑肯定是spring去實例化bean,代碼如下:
try {
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
if (logger.isDebugEnabled()) {
logger.debug("Finished creating instance of bean '" + beanName + "'");
}
return beanInstance;
}
又是do開頭的方法,說明這個也是一個創(chuàng)建方法,spring中一般do開頭的都是用于創(chuàng)建某個對象。跟著代碼走,看下doCreateBean方法,在查看此方法之前,可能需要了解下BeanWarpper這個封裝類。bean的封裝、屬性注入等都是用BeanWarpper去完成的。
看下代碼:
// Instantiate the bean.
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
進入createBeanInstance方法中,就是調(diào)用構(gòu)造器去實例化bean,返回beanWrapper。通過構(gòu)造器獲取實例代碼如下:
// Need to determine the constructor...
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (ctors != null ||
mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR ||
mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
return autowireConstructor(beanName, mbd, ctors, args);
}
// No special handling: simply use no-arg constructor.
return instantiateBean(beanName, mbd);
這里實例化我一開始以為就是簡單的反射,后面我點進去時候發(fā)現(xiàn)里面一直提策略實例化,我目前了解的是可以解決Override的問題等。如果有興趣的可以自行查看。到這里為止。我們終于實例化bean了。下面看下第二步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);
}
}
populateBean方法名稱就暴露他是干啥的:填充bean。我放一段比較重要的部分:
if (hasInstAwareBpps || needsDepCheck) {
if (pvs == null) {
pvs = mbd.getPropertyValues();
}
PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
if (hasInstAwareBpps) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
if (pvs == null) {
return;
}
}
}
}
if (needsDepCheck) {
checkDependencies(beanName, mbd, filteredPds, pvs);
}
}
這段代碼貌似沒有什么注入、填充什么的,我們看下InstantiationAwareBeanPostProcessor這個接口,發(fā)現(xiàn)這個接口有個很熟悉的實現(xiàn)類是:AutowiredAnnotationBeanPostProcessor。這個類的方法中我們看到終于看到了jnject方法。但是在inject之前需要調(diào)用下findAutowiringMatedata方法獲取一下元數(shù)據(jù):
// Fall back to class name as cache key, for backwards compatibility with custom callers.
String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());
// Quick check on the concurrent map first, with minimal locking.
InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
synchronized (this.injectionMetadataCache) {
metadata = this.injectionMetadataCache.get(cacheKey);
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
if (metadata != null) {
metadata.clear(pvs);
}
metadata = buildAutowiringMetadata(clazz);
this.injectionMetadataCache.put(cacheKey, metadata);
}
}
}
return metadata;
那么問題又來了,這個injectionMetadataCache集合值是從那里來的呢?AutowiredAnnotationBeanPostProcessor實現(xiàn)了MergeBeandefinitionPostProcessor,那么就好了,肯定是在bean實例的時候調(diào)用了postProcessMergedBeanDefintion這個方法。
果然在doCreateBean方法中有這么一段:
// Allow post-processors to modify the merged bean definition.
synchronized (mbd.postProcessingLock) {
if (!mbd.postProcessed) {
try {
//通過 BeanPostProcessor將需要注解的元數(shù)據(jù) 放到Map中
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Post-processing of merged bean definition failed", ex);
}
mbd.postProcessed = true;
}
}
哎,spring縝密啊。元數(shù)據(jù)有了,下面我們看下element.inject()方法是如何操作的:
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();
}
}
方法簡單明了,直接使用反射將值set到屬性中,至此 bean的實例、屬性注入基本完成,下面我們回頭來看doGetBean的另一個方法getObjectForBeanInstance。
2、getObjectForBeanInstance方法。對于是FactoryBean類型的 bean通過getObject獲取到bean的代理實例,跟著方法一直走下去會到getObejct()方法中。
if (System.getSecurityManager() != null) {
AccessControlContext acc = getAccessControlContext();
try {
object = AccessController.doPrivileged((PrivilegedExceptionAction<Object>) factory::getObject, acc);
}
catch (PrivilegedActionException pae) {
throw pae.getException();
}
}
else {
//從FactoryBean實現(xiàn)bean中g(shù)etObejct獲取到bean
object = factory.getObject();
}
到此 finishBeanfactoryInitialization方法執(zhí)行結(jié)束!
bean實例化流程說明
bean實例化流程流程是在onRefresh方法的finishBeanFactoryInitialization中。進入該方法
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
// Initialize conversion service for this context.
if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
beanFactory.setConversionService(
beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
}
// Register a default embedded value resolver if no bean post-processor
// (such as a PropertyPlaceholderConfigurer bean) registered any before:
// at this point, primarily for resolution in annotation attribute values.
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
getBean(weaverAwareName);
}
// Stop using the temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(null);
// Allow for caching all bean definition metadata, not expecting further changes.
beanFactory.freezeConfiguration();
// Instantiate all remaining (non-lazy-init) singletons.
beanFactory.preInstantiateSingletons();
}
1、beanFactory.freezeConfiguration(); 標(biāo)記正在實例化

當(dāng)前有183個bean正在實例化中。
2、調(diào)用beanFactory.preInstantiateSingletons(); 實例化bean
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
C/C++中的struct結(jié)構(gòu)體詳細(xì)解讀
這篇文章主要介紹了C/C++中的struct結(jié)構(gòu)體詳細(xì)解讀,結(jié)構(gòu)體是由一批數(shù)據(jù)組合而成的結(jié)構(gòu)型數(shù)據(jù),組成結(jié)構(gòu)型數(shù)據(jù)的每個數(shù)據(jù)稱為結(jié)構(gòu)型數(shù)據(jù)的“成員”,其描述了一塊內(nèi)存區(qū)間的大小及意義,需要的朋友可以參考下2023-10-10
生產(chǎn)環(huán)境jvm常用的參數(shù)設(shè)置建議分享
在Java應(yīng)用程序的部署過程中,合理配置JVM(Java虛擬機)參數(shù)對于提升應(yīng)用性能、穩(wěn)定性和資源利用效率至關(guān)重要,本文將探討一些常用的JVM參數(shù)設(shè)置建議,幫助開發(fā)者在生產(chǎn)環(huán)境中優(yōu)化Java應(yīng)用,需要的朋友可以參考下2025-04-04
Spring(AbstractRoutingDataSource)實現(xiàn)動態(tài)數(shù)據(jù)源切換示例
本篇文章主要介紹了詳解Spring(AbstractRoutingDataSource)實現(xiàn)動態(tài)數(shù)據(jù)源切換,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02
SpringBoot實現(xiàn)動態(tài)插拔的AOP的完整案例
在現(xiàn)代軟件開發(fā)中,面向切面編程(AOP) 是一種非常重要的技術(shù),能夠有效實現(xiàn)日志記錄、安全控制、性能監(jiān)控等橫切關(guān)注點的分離,在傳統(tǒng)的 AOP 實現(xiàn)中,切面邏輯往往是固定的,難以動態(tài)調(diào)整,本文將詳細(xì)探討如何利用 Spring Boot 實現(xiàn)動態(tài)插拔的 AOP,需要的朋友可以參考下2025-01-01
spring無法讀取properties文件數(shù)據(jù)問題詳解
這篇文章主要介紹了spring無法讀取properties文件數(shù)據(jù)問題詳解,需要的朋友可以參考下2020-02-02

