五毛猿 潜水
  • 1发帖数
  • 1主题数
  • 0关注数
  • 0粉丝
开启左侧

绝对干货!Spring框架源码剖析附步调

[复制链接]
五毛猿 发表于 2021-10-14 20:53:28 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
Spring框架源码剖析01-(xml设置剖析)

环境: spring5.X + idea
Spring 是一个工厂,是一个负责对象的创建和维护的工厂。它给我提供了一个功能齐备而且方便我们使用的ApplicationContext子接口,它最底层的接口是BeanFactory。在这个BeanFactory下面衍生了各种功能的子接口。
容器管理HierarchicalBeanFatory
主动注入AutowireCapableBeanFactory
读取设置信息ListableBeanFactory
可以自行找一下BeanFactory类关系图,它有一个子实现类XmlBeanFactory,先说一下XML设置文件的读取
ListableBeanFactory 是读取设置信息的,它的子实现类XmlBeanFactory就是读取xml文件的具体实现。而ApplicationContext继承了ListableBeanFactory并对xml剖析做了进一步的封装所以再我们使用ApplicationContext时直接给他一个对应位置的资源文件名它就会帮我们读取到设置信息。ApplicationContext ctx = new ClassPathXmlApplicatiionContext("applicationContext.xml");User user = ctx.getBean("user"); //我们直接用最底层的接口 BeanFactory 获取xml信息BeanFactory bf = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));User user = bf.getBean("user");说明:


  • beanFactory 底层获取xml文件信息的实现类 XmlBeanFactory 需要传入一个 ClassPathResource 对象。这个对象的父接口就是InputStreamSource 他就是提供了一个获取输入流的方法
InputStream getInputStream() throws IOException;而ClassPathResource 对这个方法的实现就是通过类大概类加载器实现的
if (this.clazz != null) {        is = this.clazz.getResourceAsStream(this.path);}else if (this.classLoader != null) {        is = this.classLoader.getResourceAsStream(this.path);}获取了输入流那就自然可以获取文件的内容了。
2.Spring获取xml内容后通过XmlBeanDefinitionReader剖析设置文件的内容封装成BeanDefinition方便后续使用。
/前边说了是XmlBeanFactory具体实现获取xml信息的功能1. public class XmlBeanFactory extends DefaultListableBeanFactory {    //xmlBeanFactory 中直接实例化 xmlBeanDefinitionReader        private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);.....        public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {                super(parentBeanFactory);                this.reader.loadBeanDefinitions(resource);        }  }2. public class XmlBeandefinitionReader extends AbstractBeanDefinitionReader{        public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {                ......                try (                        //获取输入流                        InputStream inputStream = encodedResource.getResource().getInputStream()) {                        //xml 剖析工具类                          InputSource inputSource = new InputSource(inputStream);                        if (encodedResource.getEncoding() != null) {                                inputSource.setEncoding(encodedResource.getEncoding());                        }                        //开始具体剖析                        return doLoadBeanDefinitions(inputSource, encodedResource.getResource());                }                ......        };                        protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)                throws BeanDefinitionStoreException {                try {                        // xml 剖析为 document                         Document doc = doLoadDocument(inputSource, resource);                        // document 转 beanDefinition                        int count = registerBeanDefinitions(doc, resource);                        if (logger.isDebugEnabled()) {                                logger.debug("Loaded " + count + " bean definitions from " + resource);                        }                        return count;                }        }   }3. public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocumentReader {           protected void doRegisterBeanDefinitions(Element root){                   ...                   preProcessXml(root);                   parseBeanDefinitions(root,this,dalegate);                   postProcessXml(root);           };        protected void parseBeanDefinitions(Element root,BeanDefinitionParseDelegate delegate){                if (delegate.isDefaultNamespace(root)) {                        NodeList nl = root.getChildNodes();                        for (int i = 0; i < nl.getLength(); i++) {                                Node node = nl.item(i);                                if (node instanceof Element) {                                        Element ele = (Element) node;                                        if (delegate.isDefaultNamespace(ele)) {                                                //剖析根本标签                                                parseDefaultElement(ele, delegate);                                        }                                        else {                                                //剖析自定义标签                                                delegate.parseCustomElement(ele);                                        }                                }                        }                }        }           }解释说明:

  • 通过 XmlBeanDefinitionReader 的 loadBeanDefinitions 方法得到输入流和xml剖析工具类在 doLoadBeanDefinitions方法中把输入流也就是获得的xml文件信息转化为 Document 再通过 registerBeanDefinitions 方法封装成 beanDefinition
  • 封装beanDefinition是在 DefaultBeanDefinitionDocumentReader 类中的doRegisterBeanDefinitions . parseBeanDefinitions方法做了具体功能的实现,也就是剖析文件中的标签并和beanDefinition的属性做映射。例如: 根标签(beans、prefile等) 子标签 (根本标签 bean、import、alias等,自定义标签 aop、mvc:annotation-driven、tx:annotation-driven、context:等)
  • 用 BeanDefinitionParserDelegate 把剖析标签得到的值映射成beanDefinition方便后续使用。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

猜你喜欢
在线客服邮箱
wxcy#wkgb.net

邮箱地址#换为@

Powered by 创意电子 ©2018-现在 专注资源实战分享源码下载站联盟商城