Spring IOC源碼之bean的注冊過程講解
BeanDefition加載注冊過程
進(jìn)入obtainFreshBeanFactory方法
這里面refreshBeanFactory方法會去創(chuàng)建beanFactory并加載bean
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() { refreshBeanFactory(); ConfigurableListableBeanFactory beanFactory = getBeanFactory(); if (logger.isDebugEnabled()) { logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory); } return beanFactory; }
進(jìn)入AbstractRefreshableApplicationContext類中的refreshBeanFactory方法
在這里首先會將已經(jīng)創(chuàng)建的bean工程銷毀,然后創(chuàng)建新的bean工廠,設(shè)置bean工廠的一些屬性,這里我們看到bean工廠是可以被序列化的,在customizeBeanFactory里面自定義bean工廠環(huán)境,通過allowBeanDefinitionOverriding和allowCircularReferences這兩個(gè)屬性用戶可以自己來決定是否需要bean的覆蓋,是否需要循環(huán)依賴,目前還暫時(shí)還用不到這兩個(gè)屬性
下面往容器中注冊這些bean的時(shí)候會用到,loadBeanDefinitions這個(gè)方法才會去真正加載bean
protected final void refreshBeanFactory() throws BeansException { //如果之前已經(jīng)創(chuàng)建了bean工廠,那么則銷毀 if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { //創(chuàng)建bean工廠 DefaultListableBeanFactory beanFactory = createBeanFactory(); //設(shè)置序列化id beanFactory.setSerializationId(getId()); //相同名稱的bean是否允許覆蓋,是否允許循環(huán)依賴 customizeBeanFactory(beanFactory); //這里到了最關(guān)鍵的部分,將BeanDefinition加載到bean工廠 loadBeanDefinitions(beanFactory); synchronized (this.beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } }
進(jìn)入AbstractXmlApplicationContext類的loadBeanDefinitions方法
在這里首先給bean工廠創(chuàng)建一個(gè)bean資源加載器并初始化相關(guān)環(huán)境,進(jìn)入loadBeanDefinitions繼續(xù)加載bean,這里也是最關(guān)鍵的地方
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException { // Create a new XmlBeanDefinitionReader for the given BeanFactory. //給bean工廠創(chuàng)建一個(gè)資源加載器 XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); // Configure the bean definition reader with this context's // resource loading environment. //設(shè)置資源加載環(huán)境 beanDefinitionReader.setEnvironment(this.getEnvironment()); beanDefinitionReader.setResourceLoader(this); beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this)); // Allow a subclass to provide custom initialization of the reader, // then proceed with actually loading the bean definitions. //初始化bean資源加載器 initBeanDefinitionReader(beanDefinitionReader); //開始加載bean定義 loadBeanDefinitions(beanDefinitionReader); }
它會將所有的xml進(jìn)行循環(huán)加載,最終會進(jìn)入XmlBeanDefinitionReader這個(gè)類里的loadBeanDefinitions方法。
在這里會獲得一個(gè)inputStream輸入流進(jìn)入doLoadBeanDefinitions方法開始讀取bean,并將xml轉(zhuǎn)換為Document對象
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException { Assert.notNull(encodedResource, "EncodedResource must not be null"); if (logger.isInfoEnabled()) { logger.info("Loading XML bean definitions from " + encodedResource.getResource()); } Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get(); if (currentResources == null) { currentResources = new HashSet<>(4); this.resourcesCurrentlyBeingLoaded.set(currentResources); } if (!currentResources.add(encodedResource)) { throw new BeanDefinitionStoreException( "Detected cyclic loading of " + encodedResource + " - check your import definitions!"); } try { //從資源加載描述中獲得一個(gè)輸入流 InputStream inputStream = encodedResource.getResource().getInputStream(); try { InputSource inputSource = new InputSource(inputStream); if (encodedResource.getEncoding() != null) { inputSource.setEncoding(encodedResource.getEncoding()); } //進(jìn)入關(guān)鍵部分,開始加載bean定義 return doLoadBeanDefinitions(inputSource, encodedResource.getResource()); } finally { inputStream.close(); } } catch (IOException ex) { throw new BeanDefinitionStoreException( "IOException parsing XML document from " + encodedResource.getResource(), ex); } finally { currentResources.remove(encodedResource); if (currentResources.isEmpty()) { this.resourcesCurrentlyBeingLoaded.remove(); } } }
進(jìn)入doLoadBeanDefinitions方法
這里主要是讀取xml并轉(zhuǎn)換為Document對象,關(guān)于xml的解析這個(gè)就不多介紹,直接進(jìn)入到registerBeanDefinitions方法在這里開始往容器中注冊bean
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource) throws BeanDefinitionStoreException { try { //加載解析xml文件并返回一個(gè)document對象 Document doc = doLoadDocument(inputSource, resource); //開始注冊bean定義 return registerBeanDefinitions(doc, resource); } catch (BeanDefinitionStoreException ex) { throw ex; } catch (SAXParseException ex) { throw new XmlBeanDefinitionStoreException(resource.getDescription(), "Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex); } catch (SAXException ex) { throw new XmlBeanDefinitionStoreException(resource.getDescription(), "XML document from " + resource + " is invalid", ex); } catch (ParserConfigurationException ex) { throw new BeanDefinitionStoreException(resource.getDescription(), "Parser configuration exception parsing XML from " + resource, ex); } catch (IOException ex) { throw new BeanDefinitionStoreException(resource.getDescription(), "IOException parsing XML document from " + resource, ex); } catch (Throwable ex) { throw new BeanDefinitionStoreException(resource.getDescription(), "Unexpected exception parsing XML document from " + resource, ex); } }
最終會進(jìn)入DefaultBeanDefinitionDocumentReader類里的parseBeanDefinitions方法,在parseDefaultElement方法會解析在xml中常見的import,alias,bean等標(biāo)簽
Spring IoC——Bean的創(chuàng)建和初始化
Spring介紹
Spring(http://spring.io/)是一個(gè)輕量級的Java 開發(fā)框架,同時(shí)也是輕量級的IoC和AOP的容器框架,主要是針對JavaBean的生命周期進(jìn)行管理的輕量級容器,可以單獨(dú)使用,也可以和Struts框架,MyBatis框架等組合使用。
IoC介紹
IoC是什么
Ioc—Inversion of Control,即“控制反轉(zhuǎn)”,不是什么技術(shù),而是一種設(shè)計(jì)思想。在Java開發(fā)中,Ioc意味著將你設(shè)計(jì)好的對象交給容器控制,而不是傳統(tǒng)的在你的對象內(nèi)部直接控制。如何理解好Ioc呢?理解好Ioc的關(guān)鍵是要明確“誰控制誰,控制什么,為何是反轉(zhuǎn)(有反轉(zhuǎn)就應(yīng)該有正轉(zhuǎn)了),哪些方面反轉(zhuǎn)了”,那我們來深入分析一下:
- 誰控制誰,控制什么:傳統(tǒng)Java SE程序設(shè)計(jì),我們直接在對象內(nèi)部通過new進(jìn)行創(chuàng)建對象,是程序主動去創(chuàng)建依賴對象;而IoC是有專門一個(gè)容器來創(chuàng)建這些對象,即由Ioc容器來控制對 象的創(chuàng)建;誰控制誰?當(dāng)然是IoC 容器控制了對象;控制什么?那就是主要控制了外部資源獲?。ú恢皇菍ο蟀ū热缥募龋?。
- 為何是反轉(zhuǎn),哪些方面反轉(zhuǎn)了:有反轉(zhuǎn)就有正轉(zhuǎn),傳統(tǒng)應(yīng)用程序是由我們自己在對象中主動控制去直接獲取依賴對象,也就是正轉(zhuǎn);而反轉(zhuǎn)則是由容器來幫忙創(chuàng)建及注入依賴對象;為何是反轉(zhuǎn)?因?yàn)橛扇萜鲙臀覀儾檎壹白⑷胍蕾噷ο?,對象只是被動的接受依賴對象,所以是反轉(zhuǎn);哪些方面反轉(zhuǎn)了?依賴對象的獲取被反轉(zhuǎn)了。
IoC能做什么
IoC 不是一種技術(shù),只是一種思想,一個(gè)重要的面向?qū)ο缶幊痰姆▌t,它能指導(dǎo)我們?nèi)绾卧O(shè)計(jì)出松耦合、更優(yōu)良的程序。傳統(tǒng)應(yīng)用程序都是由我們在類內(nèi)部主動創(chuàng)建依賴對象,從而導(dǎo)致類與類之間高耦合,難于測試;有了IoC容器后,把創(chuàng)建和查找依賴對象的控制權(quán)交給了容器,由容器進(jìn)行注入組合對象,所以對象與對象之間是 松散耦合,這樣也方便測試,利于功能復(fù)用,更重要的是使得程序的整個(gè)體系結(jié)構(gòu)變得非常靈活。
其實(shí)IoC對編程帶來的最大改變不是從代碼上,而是從思想上,發(fā)生了“主從換位”的變化。應(yīng)用程序原本是老大,要獲取什么資源都是主動出擊,但是在IoC/DI思想中,應(yīng)用程序就變成被動的了,被動的等待IoC容器來創(chuàng)建并注入它所需要的資源了。
IoC很好的體現(xiàn)了面向?qū)ο笤O(shè)計(jì)法則之一—— 好萊塢法則:“別找我們,我們找你”;即由IoC容器幫對象找相應(yīng)的依賴對象并注入,而不是由對象主動去找。
那么,IoC容器到底是如何從初始化完成的BeanFactory中對Bean進(jìn)行創(chuàng)建并初始化的呢?接下來我們就一探究竟。
源碼解析
準(zhǔn)備工作
首先寫一個(gè)Spring的配置文件spring.xml,為了方便測試,這里面就只有一個(gè)名為test的bean。
<?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="test" class="org.study.spring.ioc.Test"></bean> </beans>
編寫程序入口代碼,可以直接打斷點(diǎn)進(jìn)行調(diào)試。
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); Test bean = context.getBean("test", Test.class);
開始解析
開始源碼解析,緊接著上一節(jié),首先進(jìn)入AbstractApplicationContext.java的refresh方法,這一節(jié)我們重點(diǎn)來看里面的invokeBeanFactoryPostProcessors方法。
@Override public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // 在這種情況下刷新 prepareRefresh(); // 告訴子類刷新內(nèi)部bean工廠 ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // 在這種情況下,bean工廠準(zhǔn)備使用的. prepareBeanFactory(beanFactory); try { // 允許在上下文bean的后處理工廠子類。 postProcessBeanFactory(beanFactory); //在上下文中調(diào)用factory工廠的時(shí)候注冊bean的 實(shí)例對象 invokeBeanFactoryPostProcessors(beanFactory); // 注冊bean的過程當(dāng)中攔截所以bean的創(chuàng)建 registerBeanPostProcessors(beanFactory); // 初始化上下文消息資源 initMessageSource(); //初始化事物傳播屬性 initApplicationEventMulticaster(); // 在特定上下文初始化其他特殊bean子類。 onRefresh(); // 檢查偵聽器bean并注冊。 registerListeners(); // 實(shí)例化所有剩余(non-lazy-init)單例. finishBeanFactoryInitialization(beanFactory); // 最后一步:發(fā)布對應(yīng)的事件。 finishRefresh(); } catch (BeansException ex) { if (logger.isWarnEnabled()) { logger.warn("Exception encountered during context initialization - " + "cancelling refresh attempt: " + ex); } // 銷毀已經(jīng)創(chuàng)建的單例對象避免浪費(fèi)資源 destroyBeans(); // 重置“活躍”的旗幟。 cancelRefresh(ex); // 異常傳播到調(diào)用者。 throw ex; } finally { // 在spring 核心包里重置了內(nèi)存,因?yàn)槲覀兛喜恍枰獢?shù)據(jù)單例bean對象了 resetCommonCaches(); } } }
進(jìn)入invokeBeanFactoryPostProcessors方法
/** * Instantiate and invoke all registered BeanFactoryPostProcessor beans, * respecting explicit order if given. * <p>Must be called before singleton instantiation. */ protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) { PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors()); // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor) if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); } }
打開PostProcessorRegistrationDelegate類中的invokeBeanFactoryPostProcessors方法,可以看到,這個(gè)方法里有很多內(nèi)容,這里我們只分析最關(guān)鍵的部分。從本質(zhì)上來說,該方法就是去執(zhí)行BeanFactoryPostProcessor這個(gè)接口中的方法去的,上面代碼注釋也清楚的寫到如果想先執(zhí)行BeanFactoryPostProcessor這個(gè)接口的方法,必須先去實(shí)例化實(shí)現(xiàn)這個(gè)接口的Bean,也就是getBean這個(gè)方法。
public static void invokeBeanFactoryPostProcessors( ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) { // Invoke BeanDefinitionRegistryPostProcessors first, if any. Set<String> processedBeans = new HashSet<>(); if (beanFactory instanceof BeanDefinitionRegistry) { BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<>(); List<BeanDefinitionRegistryPostProcessor> registryPostProcessors = new LinkedList<>(); for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) { if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) { BeanDefinitionRegistryPostProcessor registryPostProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor; registryPostProcessor.postProcessBeanDefinitionRegistry(registry); registryPostProcessors.add(registryPostProcessor); } else { regularPostProcessors.add(postProcessor); } } // 不初始化factoryBeans:我們需要把所以沒有初始化的bean讓bean工廠處理他們,單例BeanDefinitionRegistryPostProcessors之間實(shí)現(xiàn)PriorityOrdered接口、序列化接口 String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); //首先,調(diào)用 BeanDefinitionRegistryPostProcessors 并且實(shí)現(xiàn) PriorityOrdered接口 List<BeanDefinitionRegistryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>(); for (String ppName : postProcessorNames) { if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); } } sortPostProcessors(beanFactory, priorityOrderedPostProcessors); registryPostProcessors.addAll(priorityOrderedPostProcessors); invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry); //然后, 調(diào)用 BeanDefinitionRegistryPostProcessors 并且實(shí)現(xiàn)序列化接口 postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); List<BeanDefinitionRegistryPostProcessor> orderedPostProcessors = new ArrayList<>(); for (String ppName : postProcessorNames) { if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) { orderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); } } sortPostProcessors(beanFactory, orderedPostProcessors); registryPostProcessors.addAll(orderedPostProcessors); invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry); // 最后,調(diào)用其他BeanDefinitionRegistryPostProcessors,直到?jīng)]有進(jìn)一步的出現(xiàn)。 boolean reiterate = true; while (reiterate) { reiterate = false; postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); for (String ppName : postProcessorNames) { if (!processedBeans.contains(ppName)) { BeanDefinitionRegistryPostProcessor pp = beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class); registryPostProcessors.add(pp); processedBeans.add(ppName); pp.postProcessBeanDefinitionRegistry(registry); reiterate = true; } } } // 現(xiàn)在,調(diào)用的postProcessBeanFactory回調(diào)處理器處理 invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory); invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory); } else { // 調(diào)用該工廠的時(shí)候 注冊文本的實(shí)例對象 invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory); } //不在這里初始化FactoryBeans,我們需要把所有 未初始化的bean讓工廠后面處理他們 String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false); // 單獨(dú)beanfactorypostprocessor之間實(shí)現(xiàn)PriorityOrdered 接口,下令,休息。 List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>(); List<String> orderedPostProcessorNames = new ArrayList<>(); List<String> nonOrderedPostProcessorNames = new ArrayList<>(); for (String ppName : postProcessorNames) { if (processedBeans.contains(ppName)) { // 跳過已經(jīng)處理完的第一階段 } else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class)); } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) { orderedPostProcessorNames.add(ppName); } else { nonOrderedPostProcessorNames.add(ppName); } } // 首先, 調(diào)用這個(gè) BeanFactoryPostProcessors 并且實(shí)現(xiàn)PriorityOrdered 接口 sortPostProcessors(beanFactory, priorityOrderedPostProcessors); invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory); // 然后,調(diào)用 BeanFactoryPostProcessors 并且實(shí)現(xiàn) 序列化 接口 List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(); for (String postProcessorName : orderedPostProcessorNames) { orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } sortPostProcessors(beanFactory, orderedPostProcessors); invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory); // 最后, 調(diào)用其他所有的 BeanFactoryPostProcessors. List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(); for (String postProcessorName : nonOrderedPostProcessorNames) { nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory); // 清除緩存合并bean定義自后處理器可能會有修改原來的元數(shù)據(jù),例如:替換占位符值. .. beanFactory.clearMetadataCache(); }
接下來進(jìn)入AbstractBeanFactory.java類中的doGetBean方法,這個(gè)方法的具體實(shí)現(xiàn)可以分為三個(gè)部分:
第一部分,首先先去singleton緩存中去找實(shí)例。由于我們例子中沒有把我們的bean手動放入singletonObjects這個(gè)Map里面去,所以這里肯定沒找到。
第二部分,然后是去獲取該BeanFactory父Factory,希望從這些Factory中獲取,如果該Beanfactory有父類,則希望用父類去實(shí)例化該bean,類似于JVM類加載的雙親委派機(jī)制。由于我們例子中的的Beanfactory為null,所以暫不討論這種情況。
第三部分,這一部分是我們關(guān)注的重點(diǎn),這里我們將這一大部分再分為三個(gè)小的部分來進(jìn)行分析:
- 先將目前的bean標(biāo)記為的正在創(chuàng)建
- 再獲取根據(jù)beanName得到對應(yīng)bean在beanfactory中的beanDefinitionMap的BeanDefinition(上一節(jié)初始化beanFactory時(shí)存入的),然后去獲取這個(gè)bean依賴的bean。如果依賴的bean還沒有創(chuàng)建,則先創(chuàng)建依賴的bean,進(jìn)行遞歸調(diào)用(這就是依賴注入Dependence Injection)。如果找不到依賴,則忽略。
- 最后如果是單例(Spring默認(rèn)是單例),則調(diào)用createBean()這個(gè)方法進(jìn)行Bean的創(chuàng)建。
/** * 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) * @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, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException { final String beanName = transformedBeanName(name); Object bean; // 急切地檢查手動注冊單例單緩存 Object sharedInstance = getSingleton(beanName); 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); } else { // 如果我們創(chuàng)建bean 實(shí)例對象失敗了,說明我們在循環(huán)引用該實(shí)例對象 if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } // 在factory這個(gè)工廠里檢查bean 對象是否存在 BeanFactory parentBeanFactory = getParentBeanFactory(); if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { // 當(dāng)沒有發(fā)現(xiàn)時(shí),應(yīng)該檢查父類對象 String nameToLookup = originalBeanName(name); if (args != null) { // 給父類對象提供明確 的參數(shù) return (T) parentBeanFactory.getBean(nameToLookup, args); } else { //沒有參數(shù),代表標(biāo)準(zhǔn)的獲取.getbean()方法 return parentBeanFactory.getBean(nameToLookup, requiredType); } } if (!typeCheckOnly) { markBeanAsCreated(beanName); } try { final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args); // 確保初始化的bean 是當(dāng)前的這個(gè)bean對象 String[] dependsOn = mbd.getDependsOn(); if (dependsOn != null) { for (String dependsOnBean : dependsOn) { if (isDependent(beanName, dependsOnBean)) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Circular depends-on relationship between '" + beanName + "' and '" + dependsOnBean + "'"); } registerDependentBean(dependsOnBean, beanName); getBean(dependsOnBean); } } // 創(chuàng)建一個(gè) bean 的實(shí)例對象 if (mbd.isSingleton()) { sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { try { return createBean(beanName, mbd, args); } catch (BeansException ex) { //從單例明確地刪除實(shí)例的緩存:這可能是熱切的創(chuàng)建過程,允許循環(huán)引用的決議。還刪除任何bean,收到一個(gè)臨時(shí)bean的引用。 destroySingleton(beanName); throw ex; } } }); bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } else if (mbd.isPrototype()) { //這是一個(gè)原型,創(chuàng)建一個(gè)新的實(shí)例 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, new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { 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; } }
進(jìn)入AbstractAutowireCapableBeanFactory.java類的createBean方法,這里面可以分為四個(gè)部分:
第一部分:確保該bean的class是真實(shí)存在的,也就是該bean是可以classload可以找到加載的
第二部分:準(zhǔn)備方法的重寫
第三部分:可以看到,這邊出現(xiàn)了一個(gè)return,也就是說這邊可以返回bean了。但看注釋:Give BeanPostProcessors a chance to return a proxy instead of the target bean instance. 這樣就很清晰了,BeanPostProcessor這個(gè)接口是可以臨時(shí)修改bean的,優(yōu)先級高于正常實(shí)例化bean的,如果beanPostProcessor能返回,則直接返回了。
第四部分:調(diào)用doCreateBean方法開始對bean進(jìn)行創(chuàng)建
/** * Central method of this class: creates a bean instance, * populates the bean instance, applies post-processors, etc. * @see #doCreateBean */ @Override protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException { if (logger.isDebugEnabled()) { logger.debug("Creating instance of bean '" + beanName + "'"); } RootBeanDefinition mbdToUse = mbd; //確保bean類實(shí)際上是解決在這一點(diǎn)上,和克隆bean定義的動態(tài)解析類不能存儲在共享合并bean定義。 Class<?> resolvedClass = resolveBeanClass(mbd, beanName); if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) { mbdToUse = new RootBeanDefinition(mbd); mbdToUse.setBeanClass(resolvedClass); } // 準(zhǔn)備方法覆蓋 try { mbdToUse.prepareMethodOverrides(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(), beanName, "Validation of method overrides failed", ex); } try { // .讓BeanPostProcessors返回一個(gè)代理,而不是目標(biāo)bean實(shí)例 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); } Object beanInstance = doCreateBean(beanName, mbdToUse, args); if (logger.isDebugEnabled()) { logger.debug("Finished creating instance of bean '" + beanName + "'"); } return beanInstance; }
打開doCreateBean方法,在這個(gè)方法里會做兩件事:一是通過createBeanInstance這個(gè)方法創(chuàng)建bean,二是通過initializeBean方法初始化bean。先看看createBeanInstance這個(gè)方法里有什么玄
/**
* Actually create the specified bean. Pre-creation processing has already happened * at this point, e.g. checking {@code postProcessBeforeInstantiation} callbacks. * <p>Differentiates between default bean instantiation, use of a * factory method, and autowiring a constructor. * @param beanName the name of the bean * @param mbd the merged bean definition for the bean * @param args explicit arguments to use for constructor or factory method invocation * @return a new instance of the bean * @throws BeanCreationException if the bean could not be created * @see #instantiateBean * @see #instantiateUsingFactoryMethod * @see #autowireConstructor */ protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) { // Instantiate the bean. BeanWrapper instanceWrapper = null; if (mbd.isSingleton()) { instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) { instanceWrapper = createBeanInstance(beanName, mbd, args); } final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null); Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null); // Allow post-processors to modify the merged bean definition. synchronized (mbd.postProcessingLock) { if (!mbd.postProcessed) { applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); mbd.postProcessed = true; } } /// 急切地緩存單件能夠解決循環(huán)引用 // 即使像BeanFactoryAware由生命周期接口。. boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && isSingletonCurrentlyInCreation(beanName)); if (earlySingletonExposure) { if (logger.isDebugEnabled()) { logger.debug("Eagerly caching bean '" + beanName + "' to allow for resolving potential circular references"); } addSingletonFactory(beanName, new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { return getEarlyBeanReference(beanName, mbd, bean); } }); } // 初始化 bean 的實(shí)例對象 Object exposedObject = bean; try { populateBean(beanName, mbd, instanceWrapper); if (exposedObject != null) { 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) { Object earlySingletonReference = getSingleton(beanName, false); if (earlySingletonReference != null) { 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."); } } } } // 注冊一次性使用的 bean try { registerDisposableBeanIfNecessary(beanName, bean, mbd); } catch (BeanDefinitionValidationException ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); } return exposedObject; }
創(chuàng)建Bean
進(jìn)入createBeanInstance方法,這塊代碼主要是再次對bean做安全檢查并確定該bean有默認(rèn)的構(gòu)造函數(shù)。直接看這個(gè)方法最后一行,調(diào)用instantiateBean方法并返回方法的結(jié)果。
/** * Create a new instance for the specified bean, using an appropriate instantiation strategy: * factory method, constructor autowiring, or simple instantiation. * @param beanName the name of the bean * @param mbd the bean definition for the bean * @param args explicit arguments to use for constructor or factory method invocation * @return BeanWrapper for the new instance * @see #instantiateUsingFactoryMethod * @see #autowireConstructor * @see #instantiateBean */ protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args) { // 這一步是確保bean這個(gè)類在這個(gè)步驟完成解決 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()); } if (mbd.getFactoryMethodName() != null) { return instantiateUsingFactoryMethod(beanName, mbd, args); } // 重新創(chuàng)建相同bean的時(shí)候 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); } } // 這個(gè)時(shí)候需要確定該一下 這個(gè) bean 的構(gòu)造函數(shù). Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { return autowireConstructor(beanName, mbd, ctors, args); } // 不做任何特殊處理:簡單地使用不帶參數(shù)的構(gòu)造函數(shù)。 return instantiateBean(beanName, mbd); }
接著進(jìn)入instantiateBean方法查看
/** * Instantiate the given bean using its default constructor. * @param beanName the name of the bean * @param mbd the bean definition for the bean * @return BeanWrapper for the new instance */ protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) { try { Object beanInstance; final BeanFactory parent = this; if (System.getSecurityManager() != null) { beanInstance = AccessController.doPrivileged(new PrivilegedAction<Object>() { @Override public Object run() { return getInstantiationStrategy().instantiate(mbd, beanName, parent); } }, getAccessControlContext()); } else { beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent); } BeanWrapper bw = new BeanWrapperImpl(beanInstance); initBeanWrapper(bw); return bw; } catch (Throwable ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex); } }
再進(jìn)入SimpleInstantiationStrategy.java的instantiate方法,我們可以看到,在這個(gè)方法里,Spring通過反射的方法根據(jù)BeanDefinition創(chuàng)建出Bean的對象并返回。
@Override public Object instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner) { // Don't override the class with CGLIB if no overrides. if (bd.getMethodOverrides().isEmpty()) { 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(new PrivilegedExceptionAction<Constructor<?>>() { @Override public Constructor<?> run() throws Exception { return clazz.getDeclaredConstructor((Class[]) null); } }); } else { constructorToUse = clazz.getDeclaredConstructor((Class[]) null); } bd.resolvedConstructorOrFactoryMethod = constructorToUse; } catch (Throwable ex) { throw new BeanInstantiationException(clazz, "No default constructor found", ex); } } } return BeanUtils.instantiateClass(constructorToUse); } else { // Must generate CGLIB subclass. return instantiateWithMethodInjection(bd, beanName, owner); } }
以上是Bean的創(chuàng)建,接下來我們看IoC容器是如何對Bean進(jìn)行初始化的。
初始化Bean
讓我們回到AbstractAutowireCapableBeanFactory.java類中的doCreateBean方法中,重點(diǎn)關(guān)注里面的initializeBean方法?,F(xiàn)在bean已經(jīng)被創(chuàng)建了,開始初始化該bean。
/** * Initialize the given bean instance, applying factory callbacks * as well as init methods and bean post processors. * <p>Called from {@link #createBean} for traditionally defined beans, * and from {@link #initializeBean} for existing bean instances. * @param beanName the bean name in the factory (for debugging purposes) * @param bean the new bean instance we may need to initialize * @param mbd the bean definition that the bean was created with * (can also be {@code null}, if given an existing bean instance) * @return the initialized bean instance (potentially wrapped) * @see BeanNameAware * @see BeanClassLoaderAware * @see BeanFactoryAware * @see #applyBeanPostProcessorsBeforeInitialization * @see #invokeInitMethods * @see #applyBeanPostProcessorsAfterInitialization */ protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) { if (System.getSecurityManager() != null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { @Override public Object run() { invokeAwareMethods(beanName, bean); return null; } }, getAccessControlContext()); } else { invokeAwareMethods(beanName, bean); } Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { 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()) { wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; }
在這個(gè)方法中,先調(diào)用invokeAwareMethods方法用于加載相關(guān)資源(比如BeanName、BeanClassLoader、BeanFactory等資源)。
private void invokeAwareMethods(final String beanName, final Object bean) { if (bean instanceof Aware) { if (bean instanceof BeanNameAware) { ((BeanNameAware) bean).setBeanName(beanName); } if (bean instanceof BeanClassLoaderAware) { ((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader()); } if (bean instanceof BeanFactoryAware) { ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this); } } }
再調(diào)用applyBeanPostProcessorsBeforeInitialization方法用于構(gòu)造方法執(zhí)行之前再次修改Bean(BeanPostProcessor接口)。
@Override public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { result = beanProcessor.postProcessBeforeInitialization(result, beanName); if (result == null) { return result; } } return result; }
然后通過invokeInitMethods調(diào)用自定義的初始化方法
/** * Give a bean a chance to react now all its properties are set, * and a chance to know about its owning bean factory (this object). * This means checking whether the bean implements InitializingBean or defines * a custom init method, and invoking the necessary callback(s) if it does. * @param beanName the bean name in the factory (for debugging purposes) * @param bean the new bean instance we may need to initialize * @param mbd the merged bean definition that the bean was created with * (can also be {@code null}, if given an existing bean instance) * @throws Throwable if thrown by init methods or by the invocation process * @see #invokeCustomInitMethod */ protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable { boolean isInitializingBean = (bean instanceof InitializingBean); if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) { if (logger.isDebugEnabled()) { logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'"); } if (System.getSecurityManager() != null) { try { AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { @Override public Object run() throws Exception { ((InitializingBean) bean).afterPropertiesSet(); return null; } }, getAccessControlContext()); } catch (PrivilegedActionException pae) { throw pae.getException(); } } else { ((InitializingBean) bean).afterPropertiesSet(); } } if (mbd != null) { String initMethodName = mbd.getInitMethodName(); if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName)) { invokeCustomInitMethod(beanName, bean, mbd); } } }
再調(diào)用applyBeanPostProcessorsAfterInitialization方法用于構(gòu)造方法執(zhí)行之前再次修改Bean(BeanPostProcessor接口)。
@Override public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { result = beanProcessor.postProcessAfterInitialization(result, beanName); if (result == null) { return result; } } return result; }
以上就完成了創(chuàng)建并初始化Bean的整個(gè)過程。
總結(jié)
通過這次源碼分析,我們應(yīng)該知道bean是怎么被IoC容器所創(chuàng)建的了,也知道IoC容器是如何去初始化spring.xml中的的bean了。
我們來總結(jié)一下,整個(gè)過程最主要的就是AbstractAutowireCapableBeanFactory.java類中兩個(gè)方法,一是createBeanInstance方法,用于創(chuàng)建Bean,二是initializeBean方法,用于初始化Bean。這兩個(gè)方法需要仔細(xì)地分析和思考,如果還有不明白的地方,可以對照著Spring的源碼自己動手理解一下,希望能對大家有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
探討Java 將Markdown文件轉(zhuǎn)換為Word和PDF文檔
這篇文章主要介紹了Java 將Markdown文件轉(zhuǎn)換為Word和PDF文檔,本文通過分步指南及代碼示例展示了如何將 Markdown 文件轉(zhuǎn)換為 Word 文檔和 PDF 文件,需要的朋友可以參考下2024-07-07基于java swing實(shí)現(xiàn)答題系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于java swing實(shí)現(xiàn)答題系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01idea無法打斷點(diǎn),單擊或雙擊代碼行左側(cè)區(qū)域無效的解決
這篇文章主要介紹了idea無法打斷點(diǎn),單擊或雙擊代碼行左側(cè)區(qū)域無效的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09