spring(2) spring容器的实现过程

spring 容器的实现过程

编程式Ioc容器

  • 资源位置
  • ClassPathResource res = new ClassPathResource("beans.xml")
  • 读入器的实现,
  • DefaultListableBeanFactory factory = new DefaultListableBeanFactory() 这个是默认的容器,即读入器将bean存储的map。
  • XmlBeanDefinitionReader reader= new XmlBeanDefinitionReader(factory) 这里是创建读入器的地方。
  • 读入器读入resource
  • reader.loadBeanDefinition(res) 这里将res注入到beanFacoty里面。

FileSystemXmlApplicationContext容器

继承体系

1、FileSystemXmlApplicationContext从构造方法,我们可以看到,进行了两个操作,第一个,设置资源的文件路径setConfigLocation(),第二个refresh为启动容器。该类作用就是将 configLocation转化为Resource。

2、继承自AbstractXmlApplicationContext 该类作用就是创建读入器XmlBeanDefinitionReader,该读入器作用就是将resource注入到map容器中。

3、AbstractRefreshableConfigApplicationContextAbstractXmlApplicationContext 父类,该类作用提供 configLocation的维护。

4、AbstractRefreshableApplicationContextAbstractRefreshableConfigApplicationContext 的父类,作用是创建注册器,即默认的DefaultListabelBeanFactory,该注册器为了后面创建读入器。

5、AbstractApplicationContextApplicationContext 的子类,实现了ApplicationContext 容器的大部分功能,包括消息,国际化,监听器,维护bean的声明周期等,最重要的该类提供了
Resource解析器ResourcePatternResolver ,将configLocation转化为Resource,即PathMatchingResourcePatternResolver ,而该类的实现方法以ResourceLoader 为参数,该参数提供了Resource的转换功能。另外该类提供了容器的模板方法,而具体的方法实现在继承类里面实现。

6、ResourceLoaderAbstractApplicationContext 的父类,他的功能是将configLocation转化为Resource,而模板方法getResourceByPath(location)FileSystemXmlApplicationContext 里面实现。

容器实现过程

1、DefaultResourceReader —转化为路径解析器 —> ResourcePatternResolver

2、configLocation —通过ResourcePatternResolver转化为Recource —>Resource[]

3、构建注册器DefaultListableBeanFacotry

4、构建读入器 Reader
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader (DefaultListableBeanFacotry)

5、Resource –通过reader–》转化为Document树。

6、构建 XmlReaderContext,以resource和reader为参数构建。

7、构建BeanDefinitionDocumentReader 默认的为DefaultBeanDefinitionDocumentReader ,通过
该类提供了element 语法解析器BeanDefinitionParserDelegate

8、构建 BeanDefinitionParserDelegatedelegate= new BeanDefinitionParserDelegate(readerContext);

9、doc –通过delegate –》beanDefinition,

10、beanDefinition – 》beanDefinitionHolder

11、beanDefinitionHolder – 注册到–》容器中。beanDefinitionMap

注入依赖关系。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值