版權聲明:本文爲博主原創文章,轉載請註明出處,歡迎交流學習!java
IOC(Inversion of Control),即控制反轉,意思是將對象的建立和依賴關係交給第三方容器處理,咱們要用的時候告訴容器咱們須要什麼而後直接去拿就好了。舉個例子,咱們有一個工廠,它生產各類產品,當你須要某個產品,好比你須要一輛汽車,你就告訴工廠你須要一輛汽車,工廠就會直接返回給你一輛汽車,而不須要你本身經過付出勞動來獲得這輛汽車,你也不用關心工廠是如何生產這輛汽車。對應到咱們的程序中就是,IOC容器會幫咱們建立和管理對象,當你告訴容器你須要某個對象時,容器會把這個對象返回給你,而不須要本身去new出一個對象來,對象的建立和管理會由容器自動進行,直接從容器中拿來用就能夠了。IOC能夠說是Spring最核心的思想,它使咱們的開發變得簡單(對象之間的依賴關係能夠經過配置文件或者註解來創建),對於這種優秀的設計思想,咱們固然有必要研究一下它的底層實現原理。spring
首先咱們來關注一個接口,源碼以下: app
package org.springframework.beans.factory; import org.springframework.beans.BeansException; import org.springframework.core.ResolvableType; import org.springframework.lang.Nullable; /** * * @author Rod Johnson * @author Juergen Hoeller * @author Chris Beams * @since 13 April 2001 * */ public interface BeanFactory { String FACTORY_BEAN_PREFIX = "&"; Object getBean(String name) throws BeansException; <T> T getBean(String name, @Nullable Class<T> requiredType) throws BeansException; Object getBean(String name, Object... args) throws BeansException; <T> T getBean(Class<T> requiredType) throws BeansException; <T> T getBean(Class<T> requiredType, Object... args) throws BeansException; boolean containsBean(String name); boolean isSingleton(String name) throws NoSuchBeanDefinitionException; boolean isPrototype(String name) throws NoSuchBeanDefinitionException; boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException; boolean isTypeMatch(String name, @Nullable Class<?> typeToMatch) throws NoSuchBeanDefinitionException; @Nullable Class<?> getType(String name) throws NoSuchBeanDefinitionException; String[] getAliases(String name); }
這個接口即是spring核心的bean工廠定義,它是IOC容器的頂層接口,spring中全部bean工廠都直接或間接的繼承或實現了這個接口。咱們平時使用的最多的ApplicationContext接口也繼承了BeanFactory接口,所以它具備BeanFactory接口的全部功能,這裏順便提一下,從BeanFactory獲取bean時,實例化BeanFactory容器並不會實例化所配置的bean,只有當使用某個bean(getBean)時,纔會實時的實例化該bean;從ApplicationContext獲取bean時,實例化ApplicationContext容器時會一併實例化容器中的全部的bean。ide
從BeanFactory的源碼能夠看出,它實現的核心功能就是根據名稱或類型來返回一個bean實例。一個工廠若是要具有這種功能,結合工廠模式的思想,咱們能夠試想一下它須要具有如下幾個條件:函數
一、持有各類bean的定義,只有拿到了bean的定義信息,才能根據這些信息進行實例化;工具
二、持有各類bean之間的依賴關係,若是一個類中持有對另外一個類的引用,那麼在對該類進行實例化時,必須根據類之間的依賴關係對相關類也進行實例化,所以,工廠必須得到類之間的依賴關係,不然沒法正確實例化;學習
三、以上兩種信息都依賴於咱們的配置信息定義,好比xml配置文件,工廠須要一個工具來讀取配置文件的信息。測試
以上是咱們設想IOC的實現思路,只要知足以上三個條件,就能構造一個工廠,生產各類bean。可是咱們仍是有一些疑問,好比在第一個條件中,咱們如何持有bean的定義呢?咱們先來看另一個接口: ui
package org.springframework.beans.factory.config; import org.springframework.beans.BeanMetadataElement; import org.springframework.beans.MutablePropertyValues; import org.springframework.core.AttributeAccessor; import org.springframework.lang.Nullable; /** * 一個BeanDefinition描述一個bean實例具備的屬性值,構造函數參數值,以及具體實現的進一步信息。 * * A BeanDefinition describes a bean instance, which has property values, * constructor argument values, and further information supplied by * concrete implementations. * * @author Juergen Hoeller * @author Rob Harrop * @since 19.03.2004 */ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON; String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE; int ROLE_APPLICATION = 0; int ROLE_SUPPORT = 1; int ROLE_INFRASTRUCTURE = 2; void setParentName(@Nullable String parentName); @Nullable String getParentName(); void setBeanClassName(@Nullable String beanClassName); @Nullable String getBeanClassName(); void setScope(@Nullable String scope); @Nullable String getScope(); void setLazyInit(boolean lazyInit); boolean isLazyInit(); /** * Set the names of the beans that this bean depends on being initialized. * The bean factory will guarantee that these beans get initialized first. */ void setDependsOn(String... dependsOn); /** * Return the bean names that this bean depends on. */ @Nullable String[] getDependsOn(); void setAutowireCandidate(boolean autowireCandidate); boolean isAutowireCandidate(); void setPrimary(boolean primary); boolean isPrimary(); void setFactoryBeanName(@Nullable String factoryBeanName); @Nullable String getFactoryBeanName(); void setFactoryMethodName(@Nullable String factoryMethodName); @Nullable String getFactoryMethodName(); ConstructorArgumentValues getConstructorArgumentValues(); MutablePropertyValues getPropertyValues(); boolean isSingleton(); boolean isPrototype(); boolean isAbstract(); int getRole(); @Nullable String getDescription(); @Nullable String getResourceDescription(); @Nullable BeanDefinition getOriginatingBeanDefinition(); }
BeanDefinition,顧名思義即是spring中的bean定義接口,spring的工廠裏持有的就是此接口定義的內容。從源碼能夠看出,這個接口繼承了兩個另外兩個接口,一個是AttributeAccessor接口,繼承這個接口就意味着BeanDefinition接口擁有了處理屬性的能力,另一個接口是BeanMetedataElement,它能夠得到bean的配置定義的元素,對於xml文件來講就是會持有bean的標籤。從源碼中咱們能夠看出,BeanDefinition接口定義了兩個方法,分別是void setDependsOn(String... dependsOn)和String[] getDependsOn(),從方法的說明能夠看出,這兩個方法就是設置依賴的bean的名稱和獲取依賴的bean的名稱,這就意味着只要咱們有一個BeanDefinition,就能獲得獲得bean的定義信息和bean之間的依賴關係,從而能夠生產一個完整的bean實例。this
從上面兩個接口,咱們大體能夠猜出spring是如何持有bean的定義信息及依賴關係了,沒錯,就是讓bean工廠持有一個Map<String,BeanDefinition>,String型的beanName做爲key,BeanDefinition型的bean定義做爲value,這樣就能生產一個bean實例。BeanFactory接口固然不能持有這個map對象,那麼必定是在它的某個實現類裏所持有的,咱們找到了這個實現類,來看看源碼:
package org.springframework.beans.factory.support; import java.io.IOException; import java.io.NotSerializableException; import java.io.ObjectInputStream; import java.io.ObjectStreamException; import java.io.Serializable; import java.lang.annotation.Annotation; import java.lang.ref.Reference; import java.lang.ref.WeakReference; import java.lang.reflect.Method; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.IdentityHashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import javax.inject.Provider; import org.springframework.beans.BeanUtils; import org.springframework.beans.BeansException; import org.springframework.beans.TypeConverter; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCurrentlyInCreationException; import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanNotOfRequiredTypeException; import org.springframework.beans.factory.CannotLoadBeanClassException; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InjectionPoint; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoUniqueBeanDefinitionException; import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.SmartFactoryBean; import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinitionHolder; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.DependencyDescriptor; import org.springframework.beans.factory.config.NamedBeanHolder; import org.springframework.core.OrderComparator; import org.springframework.core.ResolvableType; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.CompositeIterator; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; /** * 基於bean definition對象的完整bean工廠 * * Default implementation of the * {@link org.springframework.beans.factory.ListableBeanFactory} and * {@link BeanDefinitionRegistry} interfaces: a full-fledged bean factory * based on bean definition objects. * * @author Rod Johnson * @author Juergen Hoeller * @author Sam Brannen * @author Costin Leau * @author Chris Beams * @author Phillip Webb * @author Stephane Nicoll * @since 16 April 2001 * @see StaticListableBeanFactory * @see PropertiesBeanDefinitionReader * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader */ @SuppressWarnings("serial") public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable { @Nullable private static Class<?> javaxInjectProviderClass; static { try { javaxInjectProviderClass = ClassUtils.forName("javax.inject.Provider", DefaultListableBeanFactory.class.getClassLoader()); } catch (ClassNotFoundException ex) { // JSR-330 API not available - Provider interface simply not supported then. javaxInjectProviderClass = null; } } /** Map from serialized id to factory instance */ private static final Map<String, Reference<DefaultListableBeanFactory>> serializableFactories = new ConcurrentHashMap<>(8); /** Optional id for this factory, for serialization purposes */ @Nullable private String serializationId; /** Whether to allow re-registration of a different definition with the same name */ private boolean allowBeanDefinitionOverriding = true; /** Whether to allow eager class loading even for lazy-init beans */ private boolean allowEagerClassLoading = true; /** Optional OrderComparator for dependency Lists and arrays */ @Nullable private Comparator<Object> dependencyComparator; /** Resolver to use for checking if a bean definition is an autowire candidate */ private AutowireCandidateResolver autowireCandidateResolver = new SimpleAutowireCandidateResolver(); /** Map from dependency type to corresponding autowired value */ private final Map<Class<?>, Object> resolvableDependencies = new ConcurrentHashMap<>(16); /** Map of bean definition objects, keyed by bean name */ //beanFactory持有此map,這樣就能夠在任什麼時候候獲取bean的BeanDefinition來建立一個bean實例 private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256); /** Map of singleton and non-singleton bean names, keyed by dependency type */ private final Map<Class<?>, String[]> allBeanNamesByType = new ConcurrentHashMap<>(64); /** Map of singleton-only bean names, keyed by dependency type */ private final Map<Class<?>, String[]> singletonBeanNamesByType = new ConcurrentHashMap<>(64); /** List of bean definition names, in registration order */ private volatile List<String> beanDefinitionNames = new ArrayList<>(256); /** List of names of manually registered singletons, in registration order */ private volatile Set<String> manualSingletonNames = new LinkedHashSet<>(16); /** Cached array of bean definition names in case of frozen configuration */ @Nullable private volatile String[] frozenBeanDefinitionNames; /** Whether bean definition metadata may be cached for all beans */ private volatile boolean configurationFrozen = false; }
DefaultListableBeanFactory類,這個類是默認的bean工廠實現類,這裏只貼出了部分源碼,完整的代碼太長。咱們來看其中的一行代碼:
/** Map of bean definition objects, keyed by bean name */ //beanFactory持有此map,這樣就能夠在任什麼時候候獲取bean的BeanDefinition來建立一個bean實例 private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);
這行代碼證實了咱們的猜想,從方法是說明就能夠看出這是bean定義的map對象,以bean的名稱做爲key。到這裏思路就明確了,bean工廠的初始化就是往這個map對象里加東西,把咱們xml文件裏定義的bean填充到這個對象裏,bean工廠就能夠工做了。
那麼怎樣將xml文件配置的bean註冊到這個map對象裏呢?咱們能夠試試如下思路:
一、須要一個工具來找到xml配置文件,能夠稱之爲資源定位;
二、須要一個Reader來讀取xml配置信息,即DOM解析;
三、將讀取出來的信息註冊到map對象裏。
以代碼來驗證一下,寫一個Person類做爲bean:
public class Person { public void work(){ System.out.println("I am working..."); } }
建立一個applicationContext.xml配置文件,配置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="person" class="com.springframework.bean.test.Person"></bean> </beans>
接下來寫個測試類:
public class Client { public static void main(String[] args) { ClassPathResource classPathResource = new ClassPathResource("applicationContext.xml"); DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(defaultListableBeanFactory); beanDefinitionReader.loadBeanDefinitions(classPathResource); System.out.println(defaultListableBeanFactory.getBeanDefinitionCount()); Person person = (Person)defaultListableBeanFactory.getBean("person"); person.work(); } }
執行結果以下:
七月 06, 2017 9:41:48 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] 1 I am working...
從結果能夠看出,咱們成功解析了xml文件,並註冊了一個bean定義,經過getBean()方法成功返回了一個實例。上面的測試類用4行代碼實現了bean工廠的初始化:
第一行,完成了資源定位;
第二行,建立了一個默認的bean工廠;
第三行,建立了一個Reader,這個Reader用來讀取xml文件,將建立的defaultListableBeanFactory 做爲參數傳遞給Reader,表示爲此工廠建立Reader;
第四行,用Reader讀取配置信息,並將解析的bean定義註冊到defaultListableBeanFactory 中。
執行完以上四個步驟,bean工廠酒杯正確初始化了,接下來咱們能夠調用工廠的方法,以及得到bean實例。
可是在實際開發中不會這麼複雜,spring能夠更簡單的一步到位,它是這麼作的:
public class TestSpringBeanFactory { public static void main(String[] args) { ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml"); System.out.println(ctx.getBeanDefinitionCount()); Person person = (Person) ctx.getBean("person"); person.work(); } }
咱們看看執行結果:
七月 06, 2017 9:42:55 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@20ad9418: startup date [Thu Jul 06 21:42:55 CST 2017]; root of context hierarchy 七月 06, 2017 9:42:55 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from file [C:\Users\fangfuhai\workspace\spring-code-learning\src\applicationContext.xml] 1 I am working...
從結果能夠看出,spring用一行代碼就完成了咱們四個步驟,仔細看看日誌信息就能夠發現,spring也是用XmlBeanDefinitionReader 來讀取、解析並註冊,同時在日誌信息裏還多了兩行,這說明在這一行代碼裏,spring還作了更多的事情。
咱們在new一個FileSystemXmlApplicationContext對象的時候,spring到底作了那些事情呢?下一章節咱們來一探究竟。