码迷,mamicode.com
首页 > 编程语言 > 详细

Spring源码学习笔记1

时间:2018-04-30 20:26:17      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:img   mode   new   doc   try   学习   ble   private   部分   

1.Spring中最核心的两个类

1)DefaultListableBeanFactory

  XmlBeanFactory继承自DefaultListableBeanFactory,DefaultListableBeanFactory是整个bean加载的核心部分,是Spring加载及注册bean的默认实现

2)XmlBeanDefinitionReader

2.示例代码

BeanFactory bf=new XmlBeanFactory(new ClassPathResource("beanFactoryTest.xml"));

XmlBeanFactory.class
public
class XmlBeanFactory extends DefaultListableBeanFactory { private final XmlBeanDefinitionReader reader; public XmlBeanFactory(Resource resource) throws BeansException { this(resource, (BeanFactory)null); } public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException { super(parentBeanFactory); this.reader = new XmlBeanDefinitionReader(this); this.reader.loadBeanDefinitions(resource); } }
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
return this.loadBeanDefinitions(new EncodedResource(resource));
}
public int loadBeanDefinitions(EncodedResource encodedResource){
....  

  InputSource inputSource = new InputSource(inputStream);//通过SAX方式解析XML文件
....
  this.doLoadBeanDefinitions(inputSource, encodedResource.getResource());

}
 
 

3.配置文件的封装

Resource implements InputStreamResource

技术分享图片

 

4.AbstractAutowireCapableBeanFactory.ignoreDependencyInterface()

忽略给定接口的自动装配功能

5.加载Bean

protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource) throws BeanDefinitionStoreException {
        try {
            int validationMode = this.getValidationModeForResource(resource);//获取xml文件的验证模式
            Document doc = this.documentLoader.loadDocument(inputSource, this.getEntityResolver(), this.errorHandler, validationMode, this.isNamespaceAware());//加载XML文件
            return this.registerBeanDefinitions(doc, resource);//返回注册bean的信息
        } catch (BeanDefinitionStoreException var5) {
            throw var5;
        } catch (SAXParseException var6) {
            throw new XmlBeanDefinitionStoreException(resource.getDescription(), "Line " + var6.getLineNumber() + " in XML document from " + resource + " is invalid", var6);
        } catch (SAXException var7) {
            throw new XmlBeanDefinitionStoreException(resource.getDescription(), "XML document from " + resource + " is invalid", var7);
        } catch (ParserConfigurationException var8) {
            throw new BeanDefinitionStoreException(resource.getDescription(), "Parser configuration exception parsing XML from " + resource, var8);
        } catch (IOException var9) {
            throw new BeanDefinitionStoreException(resource.getDescription(), "IOException parsing XML document from " + resource, var9);
        } catch (Throwable var10) {
            throw new BeanDefinitionStoreException(resource.getDescription(), "Unexpected exception parsing XML document from " + resource, var10);
        }
    }

1)获取xml文件的验证模式

 

2)加载XML文件

3)返回注册bean的信息

 

Spring源码学习笔记1

标签:img   mode   new   doc   try   学习   ble   private   部分   

原文地址:https://www.cnblogs.com/lvjygogo/p/8974506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!