前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring之对象解析及注册(一)

Spring之对象解析及注册(一)

作者头像
OPice
发布2019-10-23 22:33:36
3870
发布2019-10-23 22:33:36
举报
文章被收录于专栏:D·技术专栏D·技术专栏

首先先看一下Spring是如何创建对象的

Spring创建对象

代码语言:javascript
复制
public interface IService {
    public String hello();
}
public class ServiceImpl implements IService {
    private String name;

    @Override
    public String hello() {
        return "hello";
    }
}

引入最小依赖POM&bean配置

代码语言:javascript
复制
 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.1.7.RELEASE</version>
 </dependency>
 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.7.RELEASE</version>
 </dependency>
代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <bean id="service" class="com.wyh.spring.core.ServiceImpl"
          init-method="hello">
    </bean>
</beans>

使用XmlBeanFactory创建对象

代码语言:javascript
复制
public class BeanFactoryDemo {
    public static void main(String[] args) {
        XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
        IService service = (IService) xmlBeanFactory.getBean("service");
        String hello = service.hello();
        System.out.println(hello);
    }
}

Bean解析及注册

首先看下new XmlBeanFactory(new ClassPathResource("beans.xml"))这行代码

1、构造ClassPathResource

获取到了Resource对象也就等于获取到了该资源文件,后面可以根据方法的定义对文件进行相关操作

代码语言:javascript
复制
public ClassPathResource(String path) {
        this(path, (ClassLoader)null);
    }

    public ClassPathResource(String path, @Nullable ClassLoader classLoader) {
        Assert.notNull(path, "Path must not be null");
        String pathToUse = StringUtils.cleanPath(path);
        if (pathToUse.startsWith("/")) {
            pathToUse = pathToUse.substring(1);
        }

        this.path = pathToUse;
        this.classLoader = classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader();
    }

ClassPathResource.png

2、初始化XmlBeanFactory

先放一张UML类图,对整体结构有个了解

构造方法

代码语言:javascript
复制
/**
     * Create a new XmlBeanFactory with the given resource,
     * which must be parsable using DOM.
     * @param resource the XML resource to load bean definitions from
     * @throws BeansException in case of loading or parsing errors
     */
    public XmlBeanFactory(Resource resource) throws BeansException {
        this(resource, null);
    }

    /**
     * Create a new XmlBeanFactory with the given input stream,
     * which must be parsable using DOM.
     * @param resource the XML resource to load bean definitions from
     * @param parentBeanFactory parent bean factory
     * @throws BeansException in case of loading or parsing errors
     */
    public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
        super(parentBeanFactory);
        this.reader.loadBeanDefinitions(resource);
    }

super(parentBeanFactory);跟踪到父类AbstractAutowireCapableBeanFactory

代码语言:javascript
复制
/**
     * Create a new AbstractAutowireCapableBeanFactory.
     */
    public AbstractAutowireCapableBeanFactory() {
        super();
        ignoreDependencyInterface(BeanNameAware.class);
        ignoreDependencyInterface(BeanFactoryAware.class);
        ignoreDependencyInterface(BeanClassLoaderAware.class);
    }

ignoreDependencyInterface方法主要功能是忽略给定接口的自动装配功能。 例如:A有属性B,当Spring获取A对象时,B没有初始化,先初始化B。

下面重要的关键是loadBeanDefinitions方法,借了张时序图

  1. 封装资源文件
  2. 获取输入流
  3. 通过构造InputSource和Resource继续执行doLoadBeanDefinitions() doLoadBeanDefinitions函数里主要做了三件事:
  • 获取对XML文件的验证模式
  • 加载XML文件,获取相应的Document
  • 根据返回Document注册Bean信息
代码语言:javascript
复制
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
            throws BeanDefinitionStoreException {

        try {
            Document doc = doLoadDocument(inputSource, resource);
            int count = registerBeanDefinitions(doc, resource);
            if (logger.isDebugEnabled()) {
                logger.debug("Loaded " + count + " bean definitions from " + resource);
            }
            return count;
        }
        catch (BeanDefinitionStoreException ex) {
            throw ex;
        }
           ......
    }

流程图:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.10.14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Spring创建对象
  • Bean解析及注册
    • 1、构造ClassPathResource
      • 2、初始化XmlBeanFactory
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档