【死磕 Spring】----- IOC 之 IOC 初始化总结

前面 13 篇博文从源码层次分析了 IOC 整个初始化过程,这篇就这些内容做一个总结将其连贯起来。

在前文提过,IOC 容器的初始化过程分为三步骤:Resource 定位、BeanDefinition 的载入和解析,BeanDefinition 注册。

9b310e3ef4668d96169e1fe276f19333b44e9d9e
  • Resource 定位。我们一般用外部资源来描述 Bean 对象,所以在初始化 IOC 容器的第一步就是需要定位这个外部资源。

  • BeanDefinition 的载入和解析。装载就是 BeanDefinition 的载入。BeanDefinitionReader 读取、解析 Resource 资源,也就是将用户定义的 Bean 表示成 IOC 容器的内部数据结构:BeanDefinition。在 IOC 容器内部维护着一个 BeanDefinition Map 的数据结构,在配置文件中每一个都对应着一个BeanDefinition对象。

  • BeanDefinition 注册。向IOC容器注册在第二步解析好的 BeanDefinition,这个过程是通过 BeanDefinitionRegistery 接口来实现的。在 IOC 容器内部其实是将第二个过程解析得到的 BeanDefinition 注入到一个 HashMap 容器中,IOC 容器就是通过这个 HashMap 来维护这些 BeanDefinition 的。在这里需要注意的一点是这个过程并没有完成依赖注入,依赖注册是发生在应用第一次调用 getBean() 向容器索要 Bean 时。当然我们可以通过设置预处理,即对某个 Bean 设置 lazyinit 属性,那么这个 Bean 的依赖注入就会在容器初始化的时候完成。

还记得在博客【死磕 Spring】----- IOC 之 加载 Bean 中提供的一段代码吗?这里我们同样也以这段代码作为我们研究 IOC 初始化过程的开端,如下:

 
 
  1. ClassPathResource resource = new ClassPathResource("bean.xml");

  2. DefaultListableBeanFactory factory = new DefaultListableBeanFactory();

  3. XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);

  4. reader.loadBeanDefinitions(resource);

刚刚开始的时候可能对上面这几行代码不知道什么意思,现在应该就一目了然了。

  • ClassPathResourceresource=newClassPathResource("bean.xml");: 根据 Xml 配置文件创建 Resource 资源对象。ClassPathResource 是 Resource 接口的子类,bean.xml 文件中的内容是我们定义的 Bean 信息。

  • DefaultListableBeanFactoryfactory=newDefaultListableBeanFactory(); 创建一个 BeanFactory。DefaultListableBeanFactory 是 BeanFactory 的一个子类,BeanFactory 作为一个接口,其实它本身是不具有独立使用的功能的,而 DefaultListableBeanFactory 则是真正可以独立使用的 IOC 容器,它是整个 Spring IOC 的始祖,在后续会有专门的文章来分析它。

  • XmlBeanDefinitionReaderreader=newXmlBeanDefinitionReader(factory);:创建 XmlBeanDefinitionReader 读取器,用于载入 BeanDefinition 。

  • reader.loadBeanDefinitions(resource);:开启 Bean 的载入和注册进程,完成后的 Bean 放置在 IOC 容器中。

Resource 定位

Spring 为了解决资源定位的问题,提供了两个接口:Resource、ResourceLoader,其中 Resource 接口是 Spring 统一资源的抽象接口,ResourceLoader 则是 Spring 资源加载的统一抽象。关于Resource、ResourceLoader 的更多知识请关注【死磕 Spring】----- IOC 之 Spring 统一资源加载策略

Resource 资源的定位需要 Resource 和 ResourceLoader 两个接口互相配合,在上面那段代码中 newClassPathResource("bean.xml") 为我们定义了资源,那么 ResourceLoader 则是在什么时候初始化的呢?看 XmlBeanDefinitionReader 构造方法:

 
 
  1. public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) {

  2. super(registry);

  3. }

直接调用父类 AbstractBeanDefinitionReader :

 
 
  1. protected AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) {

  2. Assert.notNull(registry, "BeanDefinitionRegistry must not be null");

  3. this.registry = registry;


  4. // Determine ResourceLoader to use.

  5. if (this.registry instanceof ResourceLoader) {

  6. this.resourceLoader = (ResourceLoader) this.registry;

  7. }

  8. else {

  9. this.resourceLoader = new PathMatchingResourcePatternResolver();

  10. }


  11. // Inherit Environment if possible

  12. if (this.registry instanceof EnvironmentCapable) {

  13. this.environment = ((EnvironmentCapable) this.registry).getEnvironment();

  14. }

  15. else {

  16. this.environment = new StandardEnvironment();

  17. }

  18. }

核心在于设置 resourceLoader 这段,如果设置了 ResourceLoader 则用设置的,否则使用 PathMatchingResourcePatternResolver ,该类是一个集大成者的 ResourceLoader。

BeanDefinition 的载入和解析

reader.loadBeanDefinitions(resource); 开启 BeanDefinition 的解析过程。如下:

 
 
  1. public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {

  2. return loadBeanDefinitions(new EncodedResource(resource));

  3. }

在这个方法会将资源 resource 包装成一个 EncodedResource 实例对象,然后调用 loadBeanDefinitions() 方法,而将 Resource 封装成 EncodedResource 主要是为了对 Resource 进行编码,保证内容读取的正确性。

 
 
  1. public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {

  2. // 省略一些代码

  3. try {

  4. // 将资源文件转为 InputStream 的 IO 流

  5. InputStream inputStream = encodedResource.getResource().getInputStream();

  6. try {

  7. // 从 InputStream 中得到 XML 的解析源

  8. InputSource inputSource = new InputSource(inputStream);

  9. if (encodedResource.getEncoding() != null) {

  10. inputSource.setEncoding(encodedResource.getEncoding());

  11. }


  12. // 具体的读取过程

  13. return doLoadBeanDefinitions(inputSource, encodedResource.getResource());

  14. }

  15. finally {

  16. inputStream.close();

  17. }

  18. }

  19. // 省略一些代码

  20. }

从 encodedResource 源中获取 xml 的解析源,调用 doLoadBeanDefinitions() 执行具体的解析过程。

 
 
  1. protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)

  2. throws BeanDefinitionStoreException {

  3. try {

  4. Document doc = doLoadDocument(inputSource, resource);

  5. return registerBeanDefinitions(doc, resource);

  6. }

  7. // 省略很多catch代码

在该方法中主要做两件事:1、根据 xml 解析源获取相应的 Document 对象,2、调用 registerBeanDefinitions() 开启 BeanDefinition 的解析注册过程。

转换为 Document 对象

调用 doLoadDocument() 会将 Bean 定义的资源转换为 Document 对象。

 
 
  1. protected Document doLoadDocument(InputSource inputSource, Resource resource) throws Exception {

  2. return this.documentLoader.loadDocument(inputSource, getEntityResolver(), this.errorHandler,

  3. getValidationModeForResource(resource), isNamespaceAware());

  4. }

loadDocument() 方法接受五个参数:

  • inputSource:加载 Document 的 Resource 源

  • entityResolver:解析文件的解析器

  • errorHandler:处理加载 Document 对象的过程的错误

  • validationMode:验证模式

  • namespaceAware:命名空间支持。如果要提供对 XML 名称空间的支持,则为true

对于这五个参数,有两个参数需要重点关注下:entityResolver、validationMode。这两个参数分别在 【死磕Spring】----- IOC 之 获取 Document 对象【死磕Spring】----- IOC 之 获取验证模型 中有详细的讲述。

loadDocument() 在类 DefaultDocumentLoader 中提供了实现,如下:

 
 
  1. public Document loadDocument(InputSource inputSource, EntityResolver entityResolver,

  2. ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {

  3. // 创建文件解析工厂

  4. DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);

  5. if (logger.isDebugEnabled()) {

  6. logger.debug("Using JAXP provider [" + factory.getClass().getName() + "]");

  7. }

  8. // 创建文档解析器

  9. DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);

  10. // 解析 Spring 的 Bean 定义资源

  11. return builder.parse(inputSource);

  12. }

这到这里,就已经将定义的 Bean 资源文件,载入并转换为 Document 对象了,那么下一步就是如何将其解析为 Spring IOC 管理的 Bean 对象并将其注册到容器中。这个过程有方法 registerBeanDefinitions() 实现。如下:

 
 
  1. public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {

  2. // 创建 BeanDefinitionDocumentReader 来对 xml 格式的BeanDefinition 解析

  3. BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();

  4. // 获得容器中注册的Bean数量

  5. int countBefore = getRegistry().getBeanDefinitionCount();


  6. // 解析过程入口,这里使用了委派模式,BeanDefinitionDocumentReader只是个接口,

  7. // 具体的解析实现过程有实现类DefaultBeanDefinitionDocumentReader完成

  8. documentReader.registerBeanDefinitions(doc, createReaderContext(resource));

  9. return getRegistry().getBeanDefinitionCount() - countBefore;

  10. }

首先创建 BeanDefinition 的解析器 BeanDefinitionDocumentReader,然后调用 documentReader.registerBeanDefinitions() 开启解析过程,这里使用的是委派模式,具体的实现由子类 DefaultBeanDefinitionDocumentReader 完成。

 
 
  1. public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {

  2. // 获得XML描述符

  3. this.readerContext = readerContext;

  4. logger.debug("Loading bean definitions");


  5. // 获得Document的根元素

  6. Element root = doc.getDocumentElement();


  7. // 解析根元素

  8. doRegisterBeanDefinitions(root);

  9. }

对 Document 对象的解析

从 Document 对象中获取根元素 root,然后调用 doRegisterBeanDefinitions() 开启真正的解析过程。

 
 
  1. protected void doRegisterBeanDefinitions(Element root) {

  2. BeanDefinitionParserDelegate parent = this.delegate;

  3. this.delegate = createDelegate(getReaderContext(), root, parent);


  4. // 省略部分代码


  5. preProcessXml(root);

  6. parseBeanDefinitions(root, this.delegate);

  7. postProcessXml(root);


  8. this.delegate = parent;

  9. }

preProcessXml()postProcessXml() 为前置、后置增强处理,目前 Spring 中都是空实现, parseBeanDefinitions() 是对根元素 root 的解析注册过程。

 
 
  1. protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {

  2. // Bean定义的Document对象使用了Spring默认的XML命名空间

  3. if (delegate.isDefaultNamespace(root)) {

  4. // 获取Bean定义的Document对象根元素的所有子节点

  5. NodeList nl = root.getChildNodes();

  6. for (int i = 0; i < nl.getLength(); i++) {

  7. Node node = nl.item(i);

  8. // 获得Document节点是XML元素节点

  9. if (node instanceof Element) {

  10. Element ele = (Element) node;

  11. // Bean定义的Document的元素节点使用的是Spring默认的XML命名空间

  12. if (delegate.isDefaultNamespace(ele)) {

  13. // 使用Spring的Bean规则解析元素节点(默认解析规则)

  14. parseDefaultElement(ele, delegate);

  15. }

  16. else {

  17. // 没有使用Spring默认的XML命名空间,则使用用户自定义的解析规则解析元素节点

  18. delegate.parseCustomElement(ele);

  19. }

  20. }

  21. }

  22. }

  23. else {

  24. // Document 的根节点没有使用Spring默认的命名空间,则使用用户自定义的解析规则解析

  25. delegate.parseCustomElement(root);

  26. }

  27. }

迭代 root 元素的所有子节点,对其进行判断,若节点为默认命名空间,则ID调用 parseDefaultElement() 开启默认标签的解析注册过程,否则调用 parseCustomElement() 开启自定义标签的解析注册过程。

标签解析

若定义的元素节点使用的是 Spring 默认命名空间,则调用 parseDefaultElement() 进行默认标签解析,如下:

 
 
  1. private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {

  2. // 如果元素节点是<Import>导入元素,进行导入解析

  3. if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {

  4. importBeanDefinitionResource(ele);

  5. }

  6. // 如果元素节点是<Alias>别名元素,进行别名解析

  7. else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {

  8. processAliasRegistration(ele);

  9. }

  10. // 如果元素节点<Bean>元素,则进行Bean解析注册

  11. else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {

  12. processBeanDefinition(ele, delegate);

  13. }


  14. // // 如果元素节点<Beans>元素,则进行Beans解析

  15. else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {

  16. // recurse

  17. doRegisterBeanDefinitions(ele);

  18. }

  19. }

对四大标签:import、alias、bean、beans 进行解析,其中 bean 标签的解析为核心工作。关于各个标签的解析过程见如下文章:

对于默认标签则由 parseCustomElement() 负责解析。

 
 
  1. public BeanDefinition parseCustomElement(Element ele) {

  2. return parseCustomElement(ele, null);

  3. }


  4. public BeanDefinition parseCustomElement(Element ele, @Nullable BeanDefinition containingBd) {

  5. String namespaceUri = getNamespaceURI(ele);

  6. if (namespaceUri == null) {

  7. return null;

  8. }

  9. NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);

  10. if (handler == null) {

  11. error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele);

  12. return null;

  13. }

  14. return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));

  15. }

获取节点的 namespaceUri,然后根据该 namespaceuri 获取相对应的 Handler,调用 Handler 的 parse() 方法即完成自定义标签的解析和注入。想了解更多参考:【死磕Spring】----- IOC 之解析自定义标签

注册 BeanDefinition

经过上面的解析,则将 Document 对象里面的 Bean 标签解析成了一个个的 BeanDefinition ,下一步则是将这些 BeanDefinition 注册到 IOC 容器中。动作的触发是在解析 Bean 标签完成后,如下:

 
 
  1. protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {

  2. BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);

  3. if (bdHolder != null) {

  4. bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);

  5. try {

  6. // Register the final decorated instance.

  7. BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());

  8. }

  9. catch (BeanDefinitionStoreException ex) {

  10. getReaderContext().error("Failed to register bean definition with name '" +

  11. bdHolder.getBeanName() + "'", ele, ex);

  12. }

  13. // Send registration event.

  14. getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));

  15. }

  16. }

调用 BeanDefinitionReaderUtils.registerBeanDefinition() 注册,其实这里面也是调用 BeanDefinitionRegistry 的 registerBeanDefinition()来注册 BeanDefinition ,不过最终的实现是在 DefaultListableBeanFactory 中实现,如下:

 
 
  1. @Override

  2. public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)

  3. throws BeanDefinitionStoreException {


  4. // 省略一堆校验


  5. BeanDefinition oldBeanDefinition;


  6. oldBeanDefinition = this.beanDefinitionMap.get(beanName);

  7. // 省略一堆 if

  8. this.beanDefinitionMap.put(beanName, beanDefinition);

  9. }

  10. else {

  11. if (hasBeanCreationStarted()) {

  12. // Cannot modify startup-time collection elements anymore (for stable iteration)

  13. synchronized (this.beanDefinitionMap) {

  14. this.beanDefinitionMap.put(beanName, beanDefinition);

  15. List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1);

  16. updatedDefinitions.addAll(this.beanDefinitionNames);

  17. updatedDefinitions.add(beanName);

  18. this.beanDefinitionNames = updatedDefinitions;

  19. if (this.manualSingletonNames.contains(beanName)) {

  20. Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames);

  21. updatedSingletons.remove(beanName);

  22. this.manualSingletonNames = updatedSingletons;

  23. }

  24. }

  25. }

  26. else {

  27. // Still in startup registration phase

  28. this.beanDefinitionMap.put(beanName, beanDefinition);

  29. this.beanDefinitionNames.add(beanName);

  30. this.manualSingletonNames.remove(beanName);

  31. }

  32. this.frozenBeanDefinitionNames = null;

  33. }


  34. if (oldBeanDefinition != null || containsSingleton(beanName)) {

  35. resetBeanDefinition(beanName);

  36. }

  37. }

这段代码最核心的部分是这句 this.beanDefinitionMap.put(beanName,beanDefinition) ,所以注册过程也不是那么的高大上,就是利用一个 Map 的集合对象来存放,key 是 beanName,value 是 BeanDefinition。

至此,整个 IOC 的初始化过程就已经完成了,从 Bean 资源的定位,转换为 Document 对象,接着对其进行解析,最后注册到 IOC 容器中,都已经完美地完成了。现在 IOC 容器中已经建立了整个 Bean 的配置信息,这些 Bean 可以被检索、使用、维护,他们是控制反转的基础,是后面注入 Bean 的依赖。最后用一张流程图来结束这篇总结之文。

10891c75f8da709f4972c72807e81bfc4924dd41

更多阅读:



原文发布时间为:2018-09-9
本文作者: Java技术驿站
本文来自云栖社区合作伙伴“ Java技术驿站”,了解相关信息可以关注“ Java技术驿站”。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值