spring源码解析之(DefaultListableBeanFactory)

首先咱们来看下继承图 java

在3.1以前有一个XmlBeanFactory类,在3.1以后被标记为Deprecated状态,官方文档建议直接使用DefaultListableBeanFactory和XmlBeanDefinitionReader类来实现XmlBeanFactory的功能。具体使用以下: this

DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
		new XmlBeanDefinitionReader(factory).loadBeanDefinitions(new ClassPathResource(format("factoryBean.xml", class));//class类
		Object result = factory.getBean("factoryBean");

XmlBeanDefinitionReader.loadBeanDefinitions(Resource resource)方法为从指定的XML文件加载bean定义。 spa

其中它实现了BeanDefinitionReader接口,其中也能够使用属性文件的方式读取,PropertiesBeanDefinitionReader具体使用方法看以下官方实例 prototype


employee.(class)=MyClass       // bean is of class MyClass
employee.(abstract)=true       // this bean can't be instantiated directly
employee.group=Insurance       // real property
employee.usesDialUp=false      // real property (potentially overridden)
  
salesrep.(parent)=employee     // derives from "employee" bean definition
salesrep.(lazy-init)=true      // lazily initialize this singleton bean
salesrep.manager(ref)=tony     // reference to another bean
salesrep.department=Sales      // real property
  
techie.(parent)=employee       // derives from "employee" bean definition
techie.(scope)=prototype       // bean is a prototype (not a shared instance)
techie.manager(ref)=jeff       // reference to another bean
techie.department=Engineering  // real property
techie.usesDialUp=true         // real property (overriding parent value)
  
ceo.$0(ref)=secretary          // inject 'secretary' bean as 0th constructor arg
ceo.$1=1000000                 // inject value '1000000' at 1st constructor arg



还有一个GroovyBeanDefinitionReader类也就不说了,如今Groovy毕竟用的比较少 code

AbstractAutowireCapableBeanFactory是一个抽象类,实现了ConfigurableListableBeanFactory, BeanDefinitionRegistry两个接口而且这个抽象类是能够序列号的实现了Serializable orm

BeanDefinitionRegistry这个是BeanDefinition的注册
xml

至于往上走能够看出BeanFactory是鼻祖。 继承

DefaultListableBeanFactory分红两个分支
接口

1.实现ConfigurableListableBeanFactory接口来达到配置bean的功能,其主要完成自动装配,操做列表bean,配置bean。 文档

其中它又继承了三个接口ListableBeanFactory, AutowireCapableBeanFactory, ConfigurableBeanFactory

ListableBeanFactory:bean集合操做接口

AutowireCapableBeanFactory:自动装配接口

ConfigurableBeanFactory:配置bean的接口

其中ConfigurableBeanFactory配置接口又继承自HierarchicalBeanFactory, SingletonBeanRegistry

HierarchicalBeanFactory是一个父bean加强器,增长了

获得父BeanFactory
BeanFactory getParentBeanFactory();
获得当前BeanFactory中的bean(忽略父工厂中的bean)
boolean containsLocalBean(String name);



SingletonBeanRegistry单例注册器,包含操做单例的一些方法




AbstractAutowireCapableBeanFactory主要实现了AutowireCapableBeanFactory方法,即自动装配。同时继承了AbstractBeanFactory类。

AbstractBeanFactory实现了ConfigurableBeanFactory,配置bean,同时继承了FactoryBeanRegistrySupport

FactoryBeanRegistrySupport至于这里就不在往上查找了,往上走主要是为了bean的注册,不管是单例仍是普通注册,总之这里就是注册。

至此DefaultListableBeanFactory的剖析就结束了

相关文章
相关标签/搜索