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]1I 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]1I am working...
从结果可以看出,spring用一行代码就完成了我们四个步骤,仔细看看日志信息就可以发现,spring也是用XmlBeanDefinitionReader 来读取、解析并注册,同时在日志信息里还多了两行,这说明在这一行代码里,spring还做了更多的事情。
我们在new一个FileSystemXmlApplicationContext对象的时候,spring到底做了那些事情呢?
FileSystemXmlApplicationContext类的内容主要是定义了若干重载的构造方法,核心构造方法如下:
/** * Create a new FileSystemXmlApplicationContext with the given parent, * loading the definitions from the given XML files. * * loading all bean definitions and creating all singletons. * Alternatively, call refresh manually after further configuring the context. * */ public FileSystemXmlApplicationContext( String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException {
super(parent); setConfigLocations(configLocations); if (refresh) { refresh(); } }
从方法说明可以看出,在这个构造方法里加载所有bean定义并创建bean单例实例。其中的refresh()方法就是IOC容器初始化的入口,refresh()方法位AbstractApplicationContext类中,这是一个抽象类,它实现了ApplicationContext的基础功能,这里使用了模版方法模式,给实现它的子类提供了统一的模板:
@Override public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // Prepare this context for refreshing. prepareRefresh();
// Tell the subclass to refresh the internal bean factory.告诉子类刷新内部bean工厂 ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context. prepareBeanFactory(beanFactory);
try { // Allows post-processing of the bean factory in context subclasses. postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation. registerBeanPostProcessors(beanFactory);
// Initialize message source for this context. initMessageSource();
// Initialize event multicaster for this context. initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses. onRefresh();
// Check for listener beans and register them. registerListeners();
// Instantiate all remaining (non-lazy-init) singletons. finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event. finishRefresh(); }
catch (BeansException ex) { if (logger.isWarnEnabled()) { logger.warn("Exception encountered during context initialization - " + "cancelling refresh attempt: " + ex); }
// Destroy already created singletons to avoid dangling resources. destroyBeans();
// Reset 'active' flag. cancelRefresh(ex);
// Propagate exception to caller. throw ex; }
finally { // Reset common introspection caches in Spring's core, since we // might not ever need metadata for singleton beans anymore... resetCommonCaches(); } } }
refresh()方法里列出了IOC容器初始化的步骤,第一个方法是初始化准备,这里只是设置启动日期和活动标识以及执行属性源的初始化。我们重点看第二个方法obtainFreshBeanFactory(),它告诉子类刷新内部bean工厂,返回了一个ConfigurableListableBeanFactory,跟踪这个方法:
/** * Tell the subclass to refresh the internal bean factory. * @return the fresh BeanFactory instance * @see #refreshBeanFactory() * @see #getBeanFactory() */ protected ConfigurableListableBeanFactory obtainFreshBeanFactory() { refreshBeanFactory(); ConfigurableListableBeanFactory beanFactory = getBeanFactory(); if (logger.isDebugEnabled()) { logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory); } return beanFactory; }复制代码
protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;
/** * Return the internal bean factory of this application context. * Can be used to access specific functionality of the underlying factory. * */ ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;
obtainFreshBeanFactory()方法的第一行调用了refreshBeanFactory()方法,这是一个抽象方法,由它的子类来实现,方法的第二行调用了getBeanFactory(),这是在其父接口中定义的一个空方法。抽象方法refreshBeanFactory()在其子类子类AbstractRefreshableApplicationContext中实现:
/** * This implementation performs an actual refresh of this context's underlying * bean factory, shutting down the previous bean factory (if any) and * initializing a fresh bean factory for the next phase of the context's lifecycle. * * 此实现执行该上下文的底层bean工厂的实际刷新,关闭以前的bean工厂(如果有的话), * 并为上下文生命周期的下一阶段初始化一个新的bean工厂 */ @Override protected final void refreshBeanFactory() throws BeansException { if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { DefaultListableBeanFactory beanFactory = createBeanFactory(); beanFactory.setSerializationId(getId()); customizeBeanFactory(beanFactory); loadBeanDefinitions(beanFactory); synchronized (this.beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } }
这个方法被final关键字修饰,也就是说不可以被重写,IOC容器的初始化就是在这个方法中完成的。第一步先判断有没有现有的工厂,有的话就销毁掉,然后创建一个默认的工厂,也就是DefaultListableBeanFactory ,接下来两行代码是设置bean工厂的一些属性,注意看loadBeanDefinitions(beanFactory)这行,当创建了一个默认的bean工厂后,加载bean定义,这跟我们上一章节使用原始方式初始化bean工厂类似。从这里不难看出,FileSystemXmlApplicationContext的构造方法中其实已经包含了我们上一章节中原始的初始化过程。接下来我们跟踪一下loadBeanDefinitions(beanFactory)的实现,这个方法是由AbstractXmlApplicationContext抽象类实现的:
/** * Loads the bean definitions via an XmlBeanDefinitionReader.装载bean定义通过XmlBeanDefinitionReader * */ @Override protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException { // Create a new XmlBeanDefinitionReader for the given BeanFactory. XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
// Configure the bean definition reader with this context's // resource loading environment. 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. initBeanDefinitionReader(beanDefinitionReader); loadBeanDefinitions(beanDefinitionReader); }
方法的第一行首先定义了一个Reader,这个Reader就是用来读取xml配置文件的,最后一行就是真正载入bean定义的实现过程,代码如下:
/** * Load the bean definitions with the given XmlBeanDefinitionReader. * */ protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException { Resource[] configResources = getConfigResources(); if (configResources != null) { reader.loadBeanDefinitions(configResources); } String[] configLocations = getConfigLocations(); if (configLocations != null) { reader.loadBeanDefinitions(configLocations); } }
上面的方法调用了XmlBeanDefinitionReader类的loadBeanDefinitions(EncodedResource encodedResource)方法:
/** * Load bean definitions from the specified XML file. * rows BeanDefinitionStoreException in case of loading or parsing errors */ 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 { InputStream inputStream = encodedResource.getResource().getInputStream(); try { InputSource inputSource = new InputSource(inputStream); if (encodedResource.getEncoding() != null) { inputSource.setEncoding(encodedResource.getEncoding()); } 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(); } } }
从方法说明可以看出,这个方法是从指定的xml文件中加载bean定义,try块中的代码才是载入bean定义的过程。spring将资源返回的输入流包装以后传给了doLoadBeanDefinitions()方法,我们进入这个方法,代码如下:
/** * Actually load bean definitions from the specified XML file. * */ protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource) throws BeanDefinitionStoreException { try { Document doc = doLoadDocument(inputSource, resource); 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); } }
/** * Actually load the specified document using the configured DocumentLoader. * */ protected Document doLoadDocument(InputSource inputSource, Resource resource) throws Exception { return this.documentLoader.loadDocument(inputSource, getEntityResolver(), this.errorHandler, getValidationModeForResource(resource), isNamespaceAware()); }
从try块中的代码可以看出,spring使用documentLoader将资源转换成了Document资源,spring使用的documentLoader为DefaultDocumentLoader,loadDocument方法定义在此类中:
/** * Load the {@link Document} at the supplied {@link InputSource} using the standard JAXP-configured * XML parser. */ @Override public Document loadDocument(InputSource inputSource, EntityResolver entityResolver, ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {
DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware); if (logger.isDebugEnabled()) { logger.debug("Using JAXP provider [" + factory.getClass().getName() + "]"); } DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler); return builder.parse(inputSource); }
从这里不难看出,这就是我们非常熟悉的DOM解析xml了,可以想象spring是根据XSD文件规定的格式解析了xml文件的各节点及属性。我们再来回头看看registerBeanDefinitions(doc, resource)方法,
/** * Register the bean definitions contained in the given DOM document. * Called by {@code loadBeanDefinitions}. * */ public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException { BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader(); int countBefore = getRegistry().getBeanDefinitionCount(); documentReader.registerBeanDefinitions(doc, createReaderContext(resource)); return getRegistry().getBeanDefinitionCount() - countBefore; }
方法说明很明确的告诉我们,这个方法是注册给定的DOM文档中包含的bean定义。到这里思路就很明确了,spring将包装的输入流解析成DOM文档,然后将DOM中包含的bean定义信息注册到IOC容器持有的Map<String,BeanDefinition>对象中。只要我们的IOC容器持有了bean定义,就能正确的生产bean实例。
通过阅读源码,我们分析了Spring IOC的实现原理。有些实现细节并没有去深究,更重要的是去理解它的核心思想和实现思路。
--END--