- 系列文章目录
- 附录
二者的关系
说到spring容器,有的同学可能知道指的是BeanFactory,有的可能说是ApplicationContext,其实这二者都是容器类,只不过BeanFactory是底层基础类,位于spring-beans模块中,而ApplicationContext位于spring-context模块,是对BeanFactory的装饰,即包含一个BeanFactory实例,容器相关的操作还是通过调用BeanFactory相关方法来实现,并拓展了许多别的功能,见源码
/**
* <p>An ApplicationContext provides:
* <ul>
* <li>Bean factory methods for accessing application components.
* Inherited from {@link org.springframework.beans.factory.ListableBeanFactory}.
* <li>The ability to load file resources in a generic fashion.
* Inherited from the {@link org.springframework.core.io.ResourceLoader} interface.
* <li>The ability to publish events to registered listeners.
* Inherited from the {@link ApplicationEventPublisher} interface.
* <li>The ability to resolve messages, supporting internationalization.
* Inherited from the {@link MessageSource} interface.
* <li>Inheritance from a parent context. Definitions in a descendant context
* will always take priority. This means, for example, that a single parent
* context can be used by an entire web application, while each servlet has
* its own child context that is independent of that of any other servlet.
* </ul>
*/
/**
* ApplicationContext提供:
* 1.用来访问应用组件(即bean)的Bean factory方法
* 2.以通用方式加载文件资源(即xml配置文件或注解)的能力
* 3.向注册的事件监听器发布事件的能力
* 4.解析消息的能力,支持国际化
* 5.父ApplicationContext
*/
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
MessageSource, ApplicationEventPublisher, ResourcePatternResolver {
//省略其他接口方法
AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;
}
可以看到ApplicationContext持有的是一个AutowireCapableBeanFactory对象。为什么不是BeanFactory呢?先看看BeanFactory定义了哪些接口方法。
BeanFactory
public interface BeanFactory {
//主要就是这些方法
Object getBean(String name) throws BeansException;
<T> T getBean(String name, @Nullable Class<T> requiredType) throws BeansException;
Object getBean(String name, Object... args) throws BeansException;
<T> T getBean(Class<T> requiredType) throws BeansException;
<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
}
BeanFactory定义的接口方法太少了,基本都是获取bean的方法,所以在ApplicationContext中获取的是AutowireCapableBeanFactory对象,它的接口方法更多。
AutowireCapableBeanFactory
public interface AutowireCapableBeanFactory extends BeanFactory {
<T> T createBean(Class<T> beanClass) throws BeansException;
void autowireBean(Object existingBean) throws BeansException;
Object createBean(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException;
//@Autowire注解的实现
Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException;
Object initializeBean(Object existingBean, String beanName) throws BeansException;
void destroyBean(Object existingBean);
//省略其他接口方法
}
AutowireCapableBeanFactory继承自BeanFactory,还是个interface,看名字就知道新增了一些自动装配相关的方法(如创建、初始化、销毁bean,根据类型注入bean等),没有任何实际实现,而想要看最终用的是哪个class,需要看ApplicationContext的子类,子类会实现getAutowireCapableBeanFactory方法。
(小提示:点击接口方法左边栏的绿色图标可展示该接口方法的实现类)

我们常用的如ClassPathXmlApplicationContext(通过xml文件读取配置),AnnotationConfigWebApplicationContext(通过标有@Configuration,@Component等注解的类读取配置)其实都是AbstractApplicationContext的子类,所以点击AbstractApplicationContext查看该类的实现:
@Override
public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException {
return getBeanFactory();
}
@Override
public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;
这种写法说明AutowireCapableBeanFactory的接口方法较少,会需要使用更具体的子类ConfigurableListableBeanFactory里的方法
ConfigurableListableBeanFactory
public interface ConfigurableListableBeanFactory
extends ListableBeanFactory, AutowireCapableBeanFactory, ConfigurableBeanFactory {
//省略其他接口方法
//重点!!!在ApplicationContext读取完配置后,会调用该方法实例化所有非懒加载(lazy-init配置为false,默认都是false)的bean
void preInstantiateSingletons() throws BeansException;
}
ConfigurableListableBeanFactory继承了AutowireCapableBeanFactory,还继承了
- ListableBeanFactory:看名字就知道该接口提供了很多数组相关的方法,如根据Class获取所有BeanNames(String[]),根据Class获取所有Bean(Map<String, T>,k为beanName,v为bean实例)
- ConfigurableBeanFactory:提供了配置BeanFactory的方法,如允许给指定Class类的bean注册自定义属性编辑器,设置TypeConverter,设置作用域SCOPE,添加BeanPostProcessor(在bean的生命周期过程中,我们想要实现对bean的自定义操作就是通过BeanPostProcessor来实现,下一篇会讲到);继承了HierarchicalBeanFactory和SingletonBeanRegistry,HierarchicalBeanFactory很简单,在BeanFactory的基础上新增了获取父容器的方法,而SingletonBeanRegistry就是单例bean注册器,因为bean默认都是单例的,而bean容器肯定会有注册、获取单例bean的方法,这些方法不在BeanFactory家族中,被放在了SingletonBeanRegistry
再次查看AbstractApplicationContext类getBeanFactory方法的实现,发现在子类AbstractRefreshableApplicationContext中:
@Override
public final ConfigurableListableBeanFactory getBeanFactory() {
DefaultListableBeanFactory beanFactory = this.beanFactory;
if (beanFactory == null) {
throw new IllegalStateException("BeanFactory not initialized or already closed - " +
"call 'refresh' before accessing beans via the ApplicationContext");
}
return beanFactory;
}
终于看到完整的实现逻辑了,可以发现最终持有的是一个DefaultListableBeanFactory对象
DefaultListableBeanFactory
可以先看看这个类的继承关系

1.首先继承关系确实很复杂,所以像上面一样列出该类的一些主要方法的实现也不现实。
2.其次是这个类终于是个class了,既不是interface也不是抽象类,没有未实现的接口方法了,说明这个类就是最基本的BeanFactory实现类了。
关于DefaultListableBeanFactory如何实现继承的interface方法的部分,在下一篇介绍ApplicationContext时,看看具体调用了哪些方法,这些方法肯定都是核心方法,所以只需要弄明白这些即可,否则漫无目的的看DefaultListableBeanFactory类的实现,看的枯燥且没有记忆点
本文围绕Spring框架展开,深入解析了BeanFactory和ApplicationContext的关系,指出BeanFactory是底层基础类,ApplicationContext是对其的装饰。还详细介绍了BeanFactory、AutowireCapableBeanFactory、ConfigurableListableBeanFactory和DefaultListableBeanFactory的特点及继承关系,为理解Spring容器提供了清晰思路。


被折叠的 条评论
为什么被折叠?



