您好,登錄后才能下訂單哦!
這篇文章主要介紹了Spring Bean中Bean的實例化分析的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Spring Bean中Bean的實例化分析文章都會有所收獲,下面我們一起來看看吧。
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException { //省略無關代碼 try { // 這里就是我們分析的重點了 ?? Object bean = resolveBeforeInstantiation(beanName, mbdToUse); //?? 注意這個邏輯:如果postProcessBeforeInstantiation方法返回非null 則將返回值作為創建的Bean。并中斷正常的創建流程 if (bean != null) { return bean; } } catch (Throwable ex) { //省略異常信息 } try { //真正創建Bean的邏輯 實例化Bean對象,為Bean屬性賦值等,這里暫不展開 Object beanInstance = doCreateBean(beanName, mbdToUse, args); //省略日志輸出 return beanInstance; } catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {//省略異常信息 }
resolveBeforeInstantiation
這個方法在BeanPostProcessor淺析 這一節分析過了 這里不再具體展開了。
如果InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation
方法返回null,那么將不會中斷正常Bean創建過程。
下面就來到的Bean實例化部分了。
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) { //解析BeanClass,在BeanDefinition中類信息是以字符串形式展現,這里解析到字符串后 會將其加載為Class 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 回調,則使用給定的回調方法初始化策略,通過獲取Supplier#get得到實例化對象 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) { //條件成立 說明構造函數或FactoryMethod已經被解析并被緩存,可直接利用構造函數解析 //與后面的SimpleInstantiationStrategy#instantiate呼應 if (mbd.resolvedConstructorOrFactoryMethod != null) { resolved = true; autowireNecessary = mbd.constructorArgumentsResolved; } } } //如果已經被解析過 if (resolved) { //條件成立 使用構造函數注入 if (autowireNecessary) { return autowireConstructor(beanName, mbd, null, null); } else { //使用默認構造函數解析 return instantiateBean(beanName, mbd); } } // 使用SmartInstantiationAwareBeanPostProcessor 找到候選的構造函數 用于注入 Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); // AutowireMode==AUTOWIRE_CONSTRUCTOR 條件成立 說明使用基于構造函數的注入方式 (默認是AUTOWIRE_NO,需要動態檢測) // mbd.hasConstructorArgumentValues() 條件成立 說明構造函數中擁有參數 if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { //基于構造函數自動注入 return autowireConstructor(beanName, mbd, ctors, args); } // 如果mbd中配置了構造函數 則使用它進行注入 ctors = mbd.getPreferredConstructors(); if (ctors != null) { return autowireConstructor(beanName, mbd, ctors, null); } // 使用默認構造函數實例化 return instantiateBean(beanName, mbd); }
上面將doCreateBean
精簡一下,只暴露出我們比較關系的部分。一目了然,Bean的實例化過程就藏在createBeanInstance
方法中。
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) { //解析BeanClass,在BeanDefinition中類信息是以字符串形式展現,這里解析到字符串后 會將其加載為Class 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 回調,則使用給定的回調方法初始化策略,通過獲取Supplier#get得到實例化對象 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) { //條件成立 說明構造函數或FactoryMethod已經被解析并被緩存,可直接利用構造函數解析 //與后面的SimpleInstantiationStrategy#instantiate呼應 if (mbd.resolvedConstructorOrFactoryMethod != null) { resolved = true; autowireNecessary = mbd.constructorArgumentsResolved; } } } //如果已經被解析過 if (resolved) { //條件成立 使用構造函數注入 if (autowireNecessary) { return autowireConstructor(beanName, mbd, null, null); } else { //使用默認構造函數解析 return instantiateBean(beanName, mbd); } } // 使用SmartInstantiationAwareBeanPostProcessor 找到候選的構造函數 用于注入 Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); // AutowireMode==AUTOWIRE_CONSTRUCTOR 條件成立 說明使用基于構造函數的注入方式 (默認是AUTOWIRE_NO,需要動態檢測) // mbd.hasConstructorArgumentValues() 條件成立 說明構造函數中擁有參數 if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { //基于構造函數自動注入 return autowireConstructor(beanName, mbd, ctors, args); } // 如果mbd中配置了構造函數 則使用它進行注入 ctors = mbd.getPreferredConstructors(); if (ctors != null) { return autowireConstructor(beanName, mbd, ctors, null); } // 使用默認構造函數實例化 return instantiateBean(beanName, mbd); }
分析了上面的源碼之后,我們試著總結一下上面代碼主要完成的事情:
1、如果mbd配置了instanceSupplier回調,則使用instanceSupplier去初始化BeanDefinition
2、如果mbd配置了工廠方法,則使用工廠方法區初始化BeanDefinition
3、實例化BeanDefinition
如果mbd已經被解析過了,則根據緩存 選擇使用有參構造函數注入還是默認構造函數注入
如果mbd沒有被解析過,找到mbd中候選的構造函數(一個類可能有多個構造函數),再根據一些限定條件 選擇是基于有參構造函數初始化還是默認構造函數初始化
針對第1點,其實就是lambda8 supplier接口的使用,不再介紹。
針對第3點,其實就是通過反射機制 創建實例對象,最終調用了SimpleInstantiationStrategy#instantiate
方法
針對第2點 舉例說明下 工廠方法
與靜態工廠
生成Bean的兩種形式,再來展開說下instantiateUsingFactoryMethod
源碼。
配置Xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="peopleFactory" class="com.wojiushiwo.factorymethod.PeopleFactory"/> <!--實例方法--> <bean id="instanceMethod" factory-bean="peopleFactory" factory-method="createPeople"/> <!--靜態方法--> <bean id="staticFactoryMethod" class="com.wojiushiwo.factorymethod.People" factory-method="createPeople"/> </beans>
//實體對象 @Data public class People implements Serializable { private String name; private Integer age; public People() { } public static People createPeople() { People people = new People(); people.setAge(18); people.setName("我就是我"); return people; } } //People工廠類 public class PeopleFactory { public People createPeople() { return People.createPeople(); } } public class FactoryMethodDemo { public static void main(String[] args) { ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("META-INF/spring.xml"); context.refresh(); People people = (People) context.getBean("staticFactoryMethod"); System.out.println(people); People people = (People) context.getBean("instanceMethod"); System.out.println(people); context.close(); } }
public BeanWrapper instantiateUsingFactoryMethod( String beanName, RootBeanDefinition mbd, @Nullable Object[] explicitArgs) { BeanWrapperImpl bw = new BeanWrapperImpl(); this.beanFactory.initBeanWrapper(bw); Object factoryBean; Class<?> factoryClass; boolean isStatic; //獲取FactoryBeanName,實例方法與靜態工廠方法的區別就在于有沒有FactoryBeanName String factoryBeanName = mbd.getFactoryBeanName(); if (factoryBeanName != null) { //如果存在FactoryBeanName,則說明是實例方法 if (factoryBeanName.equals(beanName)) { throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, "factory-bean reference points back to the same bean definition"); } //獲取當前factoryBeanName名稱的Bean factoryBean = this.beanFactory.getBean(factoryBeanName); if (mbd.isSingleton() && this.beanFactory.containsSingleton(beanName)) { throw new ImplicitlyAppearedSingletonException(); } //獲取工廠類Class 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 factoryBean = null; //獲取工廠類Class,這里使用BeanDefinition作為其工廠類 factoryClass = mbd.getBeanClass(); //標記為非靜態 isStatic = true; } Method factoryMethodToUse = null; ArgumentsHolder argsHolderToUse = null; Object[] argsToUse = null; //explicitArgs 這個是getBean方法傳遞過來的,一般為null if (explicitArgs != null) { argsToUse = explicitArgs; } else { Object[] argsToResolve = null; //加鎖 synchronized (mbd.constructorArgumentLock) { //獲取被解析的工廠方法 factoryMethodToUse = (Method) mbd.resolvedConstructorOrFactoryMethod; //條件成立 說明工廠方法已經被解析過了,并存到了mbd中緩存起來了 if (factoryMethodToUse != null && mbd.constructorArgumentsResolved) { // Found a cached factory method... argsToUse = mbd.resolvedConstructorArguments; if (argsToUse == null) { argsToResolve = mbd.preparedConstructorArguments; } } } if (argsToResolve != null) { argsToUse = resolvePreparedArguments(beanName, mbd, bw, factoryMethodToUse, argsToResolve, true); } } if (factoryMethodToUse == null || argsToUse == null) { //獲取工廠類 factoryClass = ClassUtils.getUserClass(factoryClass); //獲取類中的方法 Method[] rawCandidates = getCandidateMethods(factoryClass, mbd); List<Method> candidateList = new ArrayList<>(); for (Method candidate : rawCandidates) { //如果方法修飾符包含static,并且方法名稱是配置的FactoryMethod,則添加到候選集合中 if (Modifier.isStatic(candidate.getModifiers()) == isStatic && mbd.isFactoryMethod(candidate)) { candidateList.add(candidate); } } //如果候選集合有1個元素 并且BeanDefinition中未設置構造參數 (explicitArgs一般都為null ) if (candidateList.size() == 1 && explicitArgs == null && !mbd.hasConstructorArgumentValues()) { //獲取方法 Method uniqueCandidate = candidateList.get(0); //如果方法參數為空 if (uniqueCandidate.getParameterCount() == 0) { mbd.factoryMethodToIntrospect = uniqueCandidate; synchronized (mbd.constructorArgumentLock) { //將下面這些全緩存到mbd中,下次直接用(與createBeanInstance方法呼應上了) mbd.resolvedConstructorOrFactoryMethod = uniqueCandidate; mbd.constructorArgumentsResolved = true; mbd.resolvedConstructorArguments = EMPTY_ARGS; } //實例化bd,設置到BeanWrapper中 bw.setBeanInstance(instantiate(beanName, mbd, factoryBean, uniqueCandidate, EMPTY_ARGS)); return bw; } } //程序走到這里 大概率是BeanDefinition中設置了構造參數 Method[] candidates = candidateList.toArray(new Method[0]); //按照修飾符及方法參數 進行排序 AutowireUtils.sortFactoryMethods(candidates); ConstructorArgumentValues resolvedValues = null; //是否構造函數注入 boolean autowiring = (mbd.getResolvedAutowireMode() == AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR); int minTypeDiffWeight = Integer.MAX_VALUE; Set<Method> ambiguousFactoryMethods = null; //最小參數數量 默認是0 int minNrOfArgs; if (explicitArgs != null) { minNrOfArgs = explicitArgs.length; } else { //走到這里 說明explicitArgs未被設置參數 //如果bd設置了構造參數,則從bd中解析參數 if (mbd.hasConstructorArgumentValues()) { ConstructorArgumentValues cargs = mbd.getConstructorArgumentValues(); resolvedValues = new ConstructorArgumentValues(); //得到解析的最小參數數量 minNrOfArgs = resolveConstructorArguments(beanName, mbd, bw, cargs, resolvedValues); } else { minNrOfArgs = 0; } } LinkedList<UnsatisfiedDependencyException> causes = null; //下面主要是推斷參數、FactoryMethod,代碼比較長 就先不分析了 for (Method candidate : candidates) { Class<?>[] paramTypes = candidate.getParameterTypes(); if (paramTypes.length >= minNrOfArgs) { ArgumentsHolder argsHolder; if (explicitArgs != null) { // Explicit arguments given -> arguments length must match exactly. if (paramTypes.length != explicitArgs.length) { continue; } argsHolder = new ArgumentsHolder(explicitArgs); } else { // Resolved constructor arguments: type conversion and/or autowiring necessary. 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.length == 1); } catch (UnsatisfiedDependencyException ex) { if (logger.isTraceEnabled()) { logger.trace("Ignoring factory method [" + candidate + "] of bean '" + beanName + "': " + ex); } // Swallow and try next overloaded factory method. if (causes == null) { causes = new LinkedList<>(); } causes.add(ex); continue; } } int typeDiffWeight = (mbd.isLenientConstructorResolution() ? argsHolder.getTypeDifferenceWeight(paramTypes) : argsHolder.getAssignabilityWeight(paramTypes)); // Choose this factory method if it represents the closest match. if (typeDiffWeight < minTypeDiffWeight) { factoryMethodToUse = candidate; argsHolderToUse = argsHolder; argsToUse = argsHolder.arguments; minTypeDiffWeight = typeDiffWeight; ambiguousFactoryMethods = null; } // Find out about ambiguity: In case of the same type difference weight // for methods with the same number of parameters, collect such candidates // and eventually raise an ambiguity exception. // However, only perform that check in non-lenient constructor resolution mode, // and explicitly ignore overridden methods (with the same parameter signature). 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); } } } if (factoryMethodToUse == null) { if (causes != null) { UnsatisfiedDependencyException ex = causes.removeLast(); for (Exception cause : causes) { this.beanFactory.onSuppressedException(cause); } throw ex; } List<String> argTypes = new ArrayList<>(minNrOfArgs); if (explicitArgs != null) { for (Object arg : explicitArgs) { argTypes.add(arg != null ? arg.getClass().getSimpleName() : "null"); } } else if (resolvedValues != null) { Set<ValueHolder> valueHolders = new LinkedHashSet<>(resolvedValues.getArgumentCount()); valueHolders.addAll(resolvedValues.getIndexedArgumentValues().values()); valueHolders.addAll(resolvedValues.getGenericArgumentValues()); for (ValueHolder value : valueHolders) { String argType = (value.getType() != null ? ClassUtils.getShortName(value.getType()) : (value.getValue() != null ? value.getValue().getClass().getSimpleName() : "null")); argTypes.add(argType); } } String argDesc = StringUtils.collectionToCommaDelimitedString(argTypes); //拋出異常 } else if (void.class == factoryMethodToUse.getReturnType()) { //拋出異常 } else if (ambiguousFactoryMethods != null) { //拋出異常 } if (explicitArgs == null && argsHolderToUse != null) { mbd.factoryMethodToIntrospect = factoryMethodToUse; argsHolderToUse.storeCache(mbd, factoryMethodToUse); } } Assert.state(argsToUse != null, "Unresolved factory method arguments"); //實例化bd 設置到BeanWrapper中 bw.setBeanInstance(instantiate(beanName, mbd, factoryBean, factoryMethodToUse, argsToUse)); return bw; }
至此經過createBeanInstance
方法 就為我們創建了一個實例對象,但是現在這個對象屬性還未被賦值。
實例對象創建之后,就來到了對象屬性賦值過程了,我們大致看一下populateBean
方法,觀察下InstantiationAwareBeanPostProcessor
對屬性賦值過程的影響
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) { //省略無關代碼 boolean continueWithPropertyPopulation = true; //條件一 synthetic默認值是false 一般都會成立 //條件二 成立的話 說明存在InstantiationAwareBeanPostProcessor if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; //在BeanPostProcessor淺析中分析到此方法時說過,若Bean實例化后回調不返回true 則對屬性賦值過程產生影響。以下代碼就是說明 if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) { continueWithPropertyPopulation = false; break; } } } } //ibp.postProcessAfterInstantiation=false時 屬性賦值過程終止 if (!continueWithPropertyPopulation) { return; }
關于“Spring Bean中Bean的實例化分析”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Spring Bean中Bean的實例化分析”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。