AbstractBeanFactory.getBean的流程,有點像老外點餐,不信我們往下看。。spring
AbstractBeanFactory中有getBean的通用邏輯緩存
//AbstractBeanFactory 中getBean方法第源碼
@Override
public Object getBean(String name, Object... args) throws BeansException {
return doGetBean(name, null, args, false);
}
複製代碼
核心邏輯在:doGetBean中。bash
玩笑事後讓咱們看點實在的。app
/**
* Return an instance, which may be shared or independent, of
the specified bean.
* 得到一個實例,能夠是共享(原型的),獨立(單例的),指定的
* @param name the name of the bean to retrieve
* @param requiredType the required type of the bean to retrieve
* @param args arguments to use when creating a bean instance
* using explicit arguments
* (only applied when creating a new instance as opposed to
retrieving an existing one)
* (僅在建立一個新實例時使用,而不是獲取一個已經存在的bean)
* @param typeCheckOnly whether the instance is obtained
for a type check,not for actual use
* 是否僅僅是類型檢查
* @return an instance of the bean
* @throws BeansException if the bean could not be created
*/
@SuppressWarnings("unchecked")
protected <T> T doGetBean(
final String name, @Nullable final Class<T> requiredType,
@Nullable final Object[] args, boolean typeCheckOnly)
throws BeansException {
//1.獲取對應的beanName ,BeanFactory去掉&,別名找到對應的BeanName
final String beanName = transformedBeanName(name);
Object bean;
// Eagerly check singleton cache for manually registered singletons.
//2. 嘗試從緩存中獲取單例 Spring會,在Bean未建立完成的狀況下,建立Bean的ObjectFactory對象,提前曝光,以方便解決循環依賴。
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實例獲取對象,若是是FactoryBean 獲取它建立的對象
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
else {
// Fail if we're already creating this bean instance: // We're assumably within a circular reference.
//3.原型依賴的檢查,若是是原型,假設存在循環引用,拋出異常
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
// Check if bean definition exists in this factory.
// 4.檢查bean,是否認義在了BeanFactory中,
BeanFactory parentBeanFactory = getParentBeanFactory();
// 若是父BeanFactory不爲空&&當前並無beanDefinitionMap不包含,
// 委託給父BeanFactory
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.
//委託給,須要參數的 getBean方法
return (T) parentBeanFactory.getBean(nameToLookup, args);
}
else if (requiredType != null) {
// No args -> delegate to standard getBean method.
// 委託給標準的getBean方法
return parentBeanFactory.getBean(nameToLookup, requiredType);
}
else {
return (T) parentBeanFactory.getBean(nameToLookup);
}
}
if (!typeCheckOnly) {
//不只是,類型檢查,標記bean爲建立
// 容許bean能夠從新合併
markBeanAsCreated(beanName);
}
try {
//5.將GernericBeanDefinition轉換爲RootBeanDefinition,
//若是Bean有父Bean,時會合並父類的相關屬性。
final RootBeanDefinition mbd =
getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args);
// Guarantee initialization of beans that
// the current bean depends on.
// 6.保證bean的依賴先初始化了
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
for (String dep : dependsOn) {
// 對應@DependsOn 註解,
/**
* 檢測是否存在 depends-on 循環依賴,若存在則拋異常。
* 好比 A 依賴 B,
* B 又依賴 A,他們的配置以下:
@Bean(name="a")
@DependsOn("b")public A a () {
return new A();}
@Bean(name = "b")
@DependsOn("a")
public B b () {
return new B();
}
* a 要求 b 在其以前被建立,但 b 又要求 a 先於它
* 建立。這個時候造成了循環,對於 depends-on 循環,Spring 會直接
* 拋出異常
*
*@see org.springframework.context.annotation.ClassPathBeanDefinitionScanner#doScan
*/
if (isDependent(beanName, dep)) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
}
//註冊,Dependent和Bean的關係
registerDependentBean(dep, beanName);
try {
//先建立Dependent
getBean(dep);
}
catch (NoSuchBeanDefinitionException ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
}
}
}
// Create bean instance.
//7.建立Bean的實例
if (mbd.isSingleton()) {
sharedInstance = getSingleton(beanName, () -> {
try {
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
});
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
else if (mbd.isPrototype()) {
// It's a prototype -> create a new instance. Object prototypeInstance = null; try { beforePrototypeCreation(beanName); prototypeInstance = createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } else { String scopeName = mbd.getScope(); final Scope scope = this.scopes.get(scopeName); if (scope == null) { throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'"); } try { Object scopedInstance = scope.get(beanName, () -> { beforePrototypeCreation(beanName); try { return createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } }); bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd); } catch (IllegalStateException ex) { throw new BeanCreationException(beanName, "Scope '" + scopeName + "' is not active for the current thread; consider " + "defining a scoped proxy for this bean if you intend to refer to it from a singleton", ex); } } } catch (BeansException ex) { cleanupAfterBeanCreationFailure(beanName); throw ex; } } // Check if required type matches the type of the actual bean instance. // 8.進行類型轉換 // requiredType.isInstance(bean) 相似 bean instanceof requiredType // 爲true表示能夠直接返回,或強轉 //clazz.isAssignableFrom(obj.getClass()) == clazz.isInstance(obj) //僅當obj not null if (requiredType != null && !requiredType.isInstance(bean)) { try { T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType); if (convertedBean == null) { throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } return convertedBean; } catch (TypeMismatchException ex) { if (logger.isTraceEnabled()) { logger.trace("Failed to convert bean '" + name + "' to required type '" + ClassUtils.getQualifiedName(requiredType) + "'", ex); } throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } } return (T) bean; } 複製代碼
當看完主流程時,我有以下疑問:ide
getObjectForBeanInstance和createBean的邏輯下一篇再聊,預計20190921前上傳。 這裏先說下2,4。ui
簡單說,就是清除原有的RootBeanDefinition,再經過當前的多個GenericBeanDefinition合併成新的RootBeanDefinition,供BeanFactory使用。this
若是有興趣能夠搜索url
org.springframework.beans.factory.config.BeanDefinition org.springframework.beans.factory.support.GenericBeanDefinition org.springframework.beans.factory.support.RootBeanDefinitionspa
我最開始覺得是bean須要依賴,處理Autowired註解,然而不是。 利用IDEA查找了調用BeanDefinition的setDependsOn的地方, .net
發現了AnnotationConfigUtils.processCommonDefinitionAnnotations方法中如以下代碼:
AnnotationAttributes dependsOn = attributesFor(metadata, DependsOn.class);
if (dependsOn != null) {
abd.setDependsOn(dependsOn.getStringArray("value"));
}
複製代碼
也就是說doGetBean中的
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
.....
}
複製代碼
是對@DependOn註解的支持,我孤陋寡聞,工做中重來沒有使用過@DependOn註解。從網上查閱了資料。
當時我查下的資料的url:blog.csdn.net/qq_30257149…
@DependOn註解用來表示一個bean A的實例化依賴另外一個bean B的實例化, 可是A並不須要持有一個B的對象
讀源碼不容易,最開始我老是揪住一個方法不放,個把小時下來,已經不知道本身讀到哪裏了,後來老是渾淪吞棗,一味求快,好像看了不少,實際上什麼也不知道。今年9月初,不知道是北京的天氣變涼快了,仍是什麼別的緣由,我本身也不清楚,居然能靜下心來,不求快,一層一層讀,一點一點翻譯,天天僅讀40分鐘,總算感受是明白了點,不由感慨以下:
只抓細節太糊塗
渾淪吞棗淨瞎看
靜心慢讀未必快
反正我是入門了