版權聲明:本文為博主原創文章,轉載請注明出處,歡迎交流學習!
IOC(Inversion of Control),即控制反轉,意思是將對象的創建和依賴關系交給第三方容器處理,我們要用的時候告訴容器我們需要什么然后直接去拿就行了。舉個例子,我們有一個工廠,它生產各種產品,當你需要某個產品,比如你需要一輛汽車,你就告訴工廠你需要一輛汽車,工廠就會直接返回給你一輛汽車,而不需要你自己通過付出勞動來得到這輛汽車,你也不用關心工廠是如何生產這輛汽車。對應到我們的程序中就是,IOC容器會幫我們創建和管理對象,當你告訴容器你需要某個對象時,容器會把這個對象返回給你,而不需要自己去new出一個對象來,對象的創建和管理會由容器自動進行,直接從容器中拿來用就可以了。IOC可以說是Spring最核心的思想,它使我們的開發變得簡單(對象之間的依賴關系可以通過配置文件或者注解來建立),對於這種優秀的設計思想,我們當然有必要研究一下它的底層實現原理。
首先我們來關注一個接口,源碼如下:
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。
從BeanFactory的源碼可以看出,它實現的核心功能就是根據名稱或類型來返回一個bean實例。一個工廠如果要具備這種功能,結合工廠模式的思想,我們可以試想一下它需要具備以下幾個條件:
1、持有各種bean的定義,只有拿到了bean的定義信息,才能根據這些信息進行實例化;
2、持有各種bean之間的依賴關系,如果一個類中持有對另一個類的引用,那么在對該類進行實例化時,必須根據類之間的依賴關系對相關類也進行實例化,因此,工廠必須獲得類之間的依賴關系,否則無法正確實例化;
3、以上兩種信息都依賴於我們的配置信息定義,比如xml配置文件,工廠需要一個工具來讀取配置文件的信息。
以上是我們設想IOC的實現思路,只要滿足以上三個條件,就能構造一個工廠,生產各種bean。但是我們還是有一些疑問,比如在第一個條件中,我們如何持有bean的定義呢?我們先來看另外一個接口:
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實例。
從上面兩個接口,我們大致可以猜出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對象里呢?我們可以試試以下思路:
1、需要一個工具來找到xml配置文件,可以稱之為資源定位;
2、需要一個Reader來讀取xml配置信息,即DOM解析;
3、將讀取出來的信息注冊到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到底做了那些事情呢?下一章節我們來一探究竟。