在IOC容器初始化的梳理以後,對依賴注入作一個總結,就是bean實例化的過程,bean的定義有兩種方式,一種是xml文件配置,一種是註解,這裏是對xml配置文件的依賴注入的介紹,後續對bean與該部分的不一樣會再作介紹。java
先對整個過程作一個總體說明:從refresh()的入口進入以後,由於是注入依賴實例化bean的過程,因此在這個過程當中,會不斷判斷是否有可用的快捷方式,是什麼類型的,是singleton的,仍是protype類型的,進行getBean獲取bean的操做時,針對已經建立的能夠直接返回的直接結束,若無則進行建立,建立中會要判斷是否有parentFactory,是否有依賴bean,set建立過程當中可能須要的條件,例如method overrides的驗證和準備等,獲取可用的constructor,有可用的構造器,有參數的狀況等,都要走自主選擇的邏輯,不然所有最後走兜底策略,就是默認構造器,注意,在對constructor選擇的過程當中,可能會出現無可用,或者是Ambiguous的狀況,會拋出異常;具體最終建立bean要用什麼策略,提供兩種選擇web
首先是xml配置相關,首先在cn.lx.controller包中新建一個TestController2.java,將以前配置的註解的形式所有註釋掉,在xml中將bean配置清楚,TestController2的類內容以下:spring
//@Controller
public class TestController2 { // @RequestMapping("/test.form")
public void execute(){ return ; } }
修改applicationContext.xml文件,爲以下:bootstrap
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/test.form">startControl</prop> </props> </property> </bean> <bean id="startControl" class="cn.lx.controller.TestController2"></bean> </beans>
依賴注入會發生兩種場景,一種是:IOC容器初始化到最後階段,會將none-Lazy-init的bean所有都實例化,其餘的會在具體用到的時候實例化;先從第一種狀況IOC容器初始化最後階段來看實例化的整個過程,實例化的過程後續階段都是相同的,只是入口不一樣而已
如今,正文開始,共分爲三個步驟:入口-xml配置方式的實例化入口、獲取bean、new-bean(實例化bean)
從AbstractApplicationContext類的refresh()方法中,以前在IOC初始化中主要從obtainFreshBeanFactory()方法調用後獲得了ConfigurableListableBeanFactory類型的beanFactory,在beanFactry中已經對xml文件作了解析而且將須要的信息都存儲到了beanDefinitionMap中,以後將beanFactory做爲參數傳遞給finishBeanFactoryInitialization方法,進入到其具體實現中:
//完成beanfactory的初始化,初始化全部保留的單例bean 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. //預實例化,對未設置lazy-init=true的進行預實例化處理 beanFactory.preInstantiateSingletons(); }
以後,以後看最後一步,經過beanFactory.preInstantiateSingletons()進入到:DefaultListableBeanFactory類中,該方法會進行判斷,會判斷是否不是抽象且爲singleton且是non lazy-init的,就會進入到是否爲FactoryBean的判斷,最終發現不是FactoryBean,就進入了else的邏輯分支,即:getBean(beanName)了,從這個部分就進入到下一個關鍵階段:獲取Bean階段
@Override public void preInstantiateSingletons() throws BeansException { if (logger.isTraceEnabled()) { logger.trace("Pre-instantiating singletons in " + this); } // Iterate over a copy to allow for init methods which in turn register new bean definitions. // While this may not be part of the regular factory bootstrap, it does otherwise work fine. List<String> beanNames = new ArrayList<>(this.beanDefinitionNames); // Trigger initialization of all non-lazy singleton beans... //觸發對於全部非懶加載的singleton beans的實例化 //經過getBean()方法重點執行,其中getBean會找到doGetBean for (String beanName : beanNames) { RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); //判斷是否不爲abstract、爲singleton、lazyinit屬性爲false,則進入到下一步 if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { //判斷是不是FactoryBean if (isFactoryBean(beanName)) { Object bean = getBean(FACTORY_BEAN_PREFIX + beanName); if (bean instanceof FactoryBean) { final FactoryBean<?> factory = (FactoryBean<?>) bean; boolean isEagerInit; 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); } } } //判斷不是FactoryBean,直接進入getBean邏輯 else { getBean(beanName); } } }
從亮色的:getBean方法進入到類:AbstractBeanFactory中,而後就看到do真正作事情的來啦:
@Override public Object getBean(String name) throws BeansException { //實際執行 return doGetBean(name, null, null, false); }
興奮地點擊doGetBean,去看看它究竟幹了啥?進入到類:AbstractBeanFactory類中,doGetBean所完成的內容主要包含:判斷是否已經有存在singleton的beanName,如有能夠直接返回,若無則獲取此bean所依賴的beans-dependsOn,而後經過一個for循環判斷,是否這個中的全部dependsOn中的某個bean是否存在環形依賴,若存在的話,直接拋出異常,無異常則爲給定的bean註冊依賴bean(依賴bean也會在給定bean銷燬以前被銷燬),最後走到createBean邏輯,至此進入到下一個階段:new-bean真正建立的過程了
doGetBean部分的重要代碼以下:
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType, @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException { //規範化beanName final String beanName = transformedBeanName(name); //類型:Object Object bean; // Eagerly check singleton cache for manually registered singletons. //獲取已經建立的singleton的bean的實例對象,將之賦給Object的sharedInstance 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); } //沒有已經存在的singleton的bean的實例對象 else { // Fail if we're already creating this bean instance: // We're assumably within a circular reference. if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } // Check if bean definition exists in this factory. 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 if (requiredType != null) { // No args -> delegate to standard getBean method. return parentBeanFactory.getBean(nameToLookup, requiredType); } else { return (T) parentBeanFactory.getBean(nameToLookup); } } if (!typeCheckOnly) { markBeanAsCreated(beanName); } try { final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args); // Guarantee initialization of beans that the current bean depends on. //確保被依賴的bean要初始化以後,其餘的才能繼續初始化 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 + "'"); } //給給定bean註冊依賴bean registerDependentBean(dep, beanName); try { getBean(dep); } catch (NoSuchBeanDefinitionException ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "'" + beanName + "' depends on missing bean '" + dep + "'", ex); } } } // 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); } 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. 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; }
進入到AbstractAutowireCapableBeanFactory類的createBean方法中,該方法主要作了三件事情:首先將RootBeanDefinition(注:一個RootBeanDefinition類型的mbdToUse,RootBeanDefinition就是根bean定義,表示合併的bean定義,在運行時支持Spring Factory中定義的特定的bean)類的一個實例對其設置beanClass,並驗證和準備method override,其次在真正進行create以前先判斷是會否有short-cut快捷方式能夠直接返回,最後,進入到doCreateBean()中,其中doCreateBean()方法的參數有三個,一個是:一個是beanName,一個是mbdToUse,一個是參數args;關鍵部分的代碼以下:
//依賴注入,實例化一個bean @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 //RootBeanDefinition就是根bean定義,表示合併的bean定義,在運行時支持Spring Factory中定義的特定的bean RootBeanDefinition mbdToUse = mbd; // Make sure bean class is actually resolved at this point, and // clone the bean definition in case of a dynamically resolved Class // which cannot be stored in the shared merged bean definition. //resolveBeanClass的做用是:根據特定的beanDefinition解析bean class,並獲得一個class的引用;將已解析的類存儲到bean定義中以供後續使用 Class<?> resolvedClass = resolveBeanClass(mbd, beanName); if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) { //將解析後獲得的resolvedCalss設置給RootBeanDefinition 根bean定義的 mbdToUse mbdToUse = new RootBeanDefinition(mbd); mbdToUse.setBeanClass(resolvedClass); } //在RootBeanDefinition中能夠去準備Bean所須要的各類屬性,例如:beanClass解析後的類信息的set,例如method override //這些信息在後續去進行實際的建立bean實例化的過程都是須要用到的 // Prepare method overrides. try { //要回頭去看解析bean的存入到BeanDefinition中的時候,就已經設置過overrides的相關屬性了 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. //在實例化以前判斷,是否有short-cut快捷方式能夠應用 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 { //doCreateBean是真正作實例化操做的部分 //這裏的三個參數,一個是beanName,一個是mbdToUse,一個是參數args Object beanInstance = doCreateBean(beanName, mbdToUse, args); if (logger.isTraceEnabled()) { logger.trace("Finished creating instance of bean '" + beanName + "'"); } return beanInstance; } catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) { // A previously detected exception with proper bean creation context already, // or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry. throw ex; } catch (Throwable ex) { throw new BeanCreationException( mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex); } }
繼續:doCreateBean,會作三件事情:首先,判斷是不是單例,若是單例的話,須要將其FactoryBeanInstanceCache中的該beanName取出,若爲null,則須要繼續建立,不然直接用cache中取出的接口;後面會重點說一下:建立createBeanInstance的過程,以後initializeBean,最後registerDisposableBeanIfNecessary,將bean註冊爲一次性的
主要部分代碼代碼見下方:
//方法說明:真正執行CreatBean的操做 protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args) throws BeanCreationException { // Instantiate the bean. BeanWrapper instanceWrapper = null; //判斷是單例,則從factoryBeanInstanceCache中將其取出,若存在,則基於當前已經緩存的進行後續處理 //不然須要繼續建立 if (mbd.isSingleton()) { instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) { //建立一個特定bean的new instance新的實例 instanceWrapper = createBeanInstance(beanName, mbd, args); } final Object bean = instanceWrapper.getWrappedInstance(); Class<?> beanType = instanceWrapper.getWrappedClass(); if (beanType != NullBean.class) { mbd.resolvedTargetType = beanType; } // Initialize the bean instance. //填充過程 Object exposedObject = bean; try { populateBean(beanName, mbd, instanceWrapper); //initializeBean 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); } } // Register bean as disposable. //將bean註冊爲一次性的 try { registerDisposableBeanIfNecessary(beanName, bean, mbd); } catch (BeanDefinitionValidationException ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); } return exposedObject; }
下面重點說明:createBeanInstance過程,會根據有參和無參,分別進入到:autowireConstructor和instantiateBean中,重點代碼見:
//建立一個特定bean的new instance新的實例 //會使用恰當的實例化策略:工廠方法、構造函數自動裝配或簡單實例化 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) { return instantiateUsingFactoryMethod(beanName, mbd, args); } // Shortcut when re-creating the same 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) { //有參數的狀況走到的 return autowireConstructor(beanName, mbd, null, null); } else { //無參的狀況走到的 return instantiateBean(beanName, mbd); } } // Candidate constructors for autowiring? Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { return autowireConstructor(beanName, mbd, ctors, args); } // Preferred constructors for default construction? ctors = mbd.getPreferredConstructors(); if (ctors != null) { return autowireConstructor(beanName, mbd, ctors, null); } // No special handling: simply use no-arg constructor. //調用無參構造器 return instantiateBean(beanName, mbd); }
若是是有參所用到的autowireConstructor,主要是:解析參數、獲取constructorToUse,若是一直沒有找到可用的,直接拋出異常,有可用的繼續bw.setBeanInstance(instantiate(beanName, mbd, constructorToUse, argsToUse));進入instantiate中,該方法是在:ContructorReSolver類中,該方法中能夠看到:strategy的字眼,這裏針對不一樣的狀況進行了不一樣的策略選擇,具體見下方:
/類內部的私有方法 //實例化,經過對應的Constructor和參數進行某個類的對象的實例化 //autowire的走到了這裏 private Object instantiate( String beanName, RootBeanDefinition mbd, Constructor constructorToUse, Object[] argsToUse) { try { //獲取初始化策略 InstantiationStrategy strategy = this.beanFactory.getInstantiationStrategy(); if (System.getSecurityManager() != null) { return AccessController.doPrivileged((PrivilegedAction<Object>) () -> strategy.instantiate(mbd, beanName, this.beanFactory, constructorToUse, argsToUse), this.beanFactory.getAccessControlContext()); } else { return strategy.instantiate(mbd, beanName, this.beanFactory, constructorToUse, argsToUse); } } catch (Throwable ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Bean instantiation via constructor failed", ex); } }
那麼具體所給出的不一樣策略的支持是什麼呢?進入到了:經過jdk反射和cglib來進行具體的實例化的選擇
SimpleInstantiationStrategy類中,
//最終的不少實現的調到的都是:Simplexxx、或者是Defalutxxx之類的 //這是有參數的contructor的走的路徑 @Override public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner, final Constructor<?> ctor, Object... args) { //沒有method是override的狀況,就是說不能經過繼承來實現的,那麼必須不能用cglib了, //不然能夠用cglib動態代理 if (!bd.hasMethodOverrides()) { if (System.getSecurityManager() != null) { // use own privileged to change accessibility (when security is on) AccessController.doPrivileged((PrivilegedAction<Object>) () -> { ReflectionUtils.makeAccessible(ctor); return null; }); } return BeanUtils.instantiateClass(ctor, args); } else { return instantiateWithMethodInjection(bd, beanName, owner, ctor, args); } }