前言
Spring中大部分常用组件存在BeanFactory和ApplicationContext中,我们下面也是从这两类中的属性介绍的。如果我们需要使用这些容器助中的组件,获取到这两个实例就可以了。获取这两个实例的方法,可以使用Aware接口实现。
一、BeanFactory
先来看下XmlBeanFactory类的关系图(Mac快捷键是:command+option+U)
可以看到BeanFactory的具体类只有XmlBeanFactory和DefaultListableBeanFactory,现在XmlBeanFactory已过期,BeanFactory就只剩唯一的实现类DefaultListableBeanFactory了,应该说使用spring的所有bean工厂,都是使用DefaultListableBeanFactory类来创建的。
- Bean后处理器
- 存储位置:AbstractBeanFactory类beanPostProcessors属性
private final List<BeanPostProcessor> beanPostProcessors = new ArrayList<>();
- 注册方法:AbstractBeanFactory的addBeanPostProcessor方法
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
this.beanPostProcessors.remove(beanPostProcessor);
this.beanPostProcessors.add(beanPostProcessor);
if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
this.hasInstantiationAwareBeanPostProcessors = true;
}
if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
this.hasDestructionAwareBeanPostProcessors = true;
}
}
- ApplicationContext注册位置:AbstractApplicationContext类的registerBeanPostProcessors方法
- 单例Bean
- 存储位置:DefaultSingletonBeanRegistry类的singletonObjects属性
/** Cache of singleton objects: bean name --> bean instance */
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
- 注册过的Bean名称
- 存储位置:DefaultSingletonBeanRegistry类的registeredSingletons属性
/** Set of registered singletons, containing the bean names in registration order */
private final Set<String> registeredSingletons = new LinkedHashSet<>(256);
- 手动注册的Bean名称
- 存储位置:DefaultListableBeanFactory类的manualSingletonNames属性
/** List of names of manually registered singletons, in registration order */
private volatile Set<String> manualSingletonNames = new LinkedHashSet<>(16);
- 注册方法:DefaultListableBeanFactory的registerSingleton方法
public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException {
super.registerSingleton(beanName, singletonObject);
if (hasBeanCreationStarted()) {
// Cannot modify startup-time collection elements anymore (for stable iteration)
synchronized (this.beanDefinitionMap) {
if (!this.beanDefinitionMap.containsKey(beanName)) {
Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames.size() + 1);
updatedSingletons.addAll(this.manualSingletonNames);
updatedSingletons.add(beanName);
// 重新赋值
this.manualSingletonNames = updatedSingletons;
}
}
}
else {
// Still in startup registration phase
if (!this.beanDefinitionMap.containsKey(beanName)) {
// 新增beanName
this.manualSingletonNames.add(beanName);
}
}
clearByTypeCache();
}
- Bean定义信息
- 存储位置:DefaultListableBeanFactory类的beanDefinitionMap属性
/** Map of bean definition objects, keyed by bean name */
private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);
- 注册方法:DefaultListableBeanFactory类的registerBeanDefinition方法
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException {
// 省略校验代码...
if (oldBeanDefinition != null) {
// 省略校验代码...
}
else {
if (hasBeanCreationStarted()) {
// Cannot modify startup-time collection elements anymore (for stable iteration)
synchronized (this.beanDefinitionMap) {
this.beanDefinitionMap.put(beanName, beanDefinition);
List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1);
updatedDefinitions.addAll(this.beanDefinitionNames);
updatedDefinitions.add(beanName);
this.beanDefinitionNames = updatedDefinitions;
if (this.manualSingletonNames.contains(beanName)) {
Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames);
updatedSingletons.remove(beanName);
this.manualSingletonNames = updatedSingletons;
}
}
}
else {
// Still in startup registration phase
this.beanDefinitionMap.put(beanName, beanDefinition);
this.beanDefinitionNames.add(beanName);
this.manualSingletonNames.remove(beanName);
}
this.frozenBeanDefinitionNames = null;
}
if (oldBeanDefinition != null || containsSingleton(beanName)) {
resetBeanDefinition(beanName);
}
}
- Bean定义名称
- 存储位置:DefaultListableBeanFactory类的beanDefinitionNames属性
/** List of bean definition names, in registration order */
private volatile List<String> beanDefinitionNames = new ArrayList<>(256);
- 获取方法:DefaultListableBeanFactory类的getBeanDefinitionNames方法
public String[] getBeanDefinitionNames() {
String[] frozenNames = this.frozenBeanDefinitionNames;
if (frozenNames != null) {
return frozenNames.clone();
}
else {
return StringUtils.toStringArray(this.beanDefinitionNames);
}
}
二、ApplicationContext
先来看下ClassPathXmlApplicationContext的类关系图
左上角的红框代表BeanFactory的相关功能,右边的红框代表ApplicationContext在BeanFactory之外扩展的功能。
- Bean工厂
- 存储位置:AbstractRefreshableApplicationContext类的beanFactory属性中
private DefaultListableBeanFactory beanFactory;
- 赋值位置:AbstractRefreshableApplicationContext类的refreshBeanFactory方法
protected final void refreshBeanFactory() throws BeansException {
if (hasBeanFactory()) {
destroyBeans();
closeBeanFactory();
}
try {
// 创建bean工厂
DefaultListableBeanFactory beanFactory = createBeanFactory();
beanFactory.setSerializationId(getId());
customizeBeanFactory(beanFactory);
loadBeanDefinitions(beanFactory);
synchronized (this.beanFactoryMonitor) {
// 赋值
this.beanFactory = beanFactory;
}
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
- Bean工厂后处理器
- 存储位置:AbstractApplicationContext类的beanFactoryPostProcessors属性
/** BeanFactoryPostProcessors to apply on refresh */
private final List<BeanFactoryPostProcessor> beanFactoryPostProcessors = new ArrayList<>();
- 注册位置:AbstractApplicationContext类的addBeanFactoryPostProcessor方法
public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor) {
Assert.notNull(postProcessor, "BeanFactoryPostProcessor must not be null");
this.beanFactoryPostProcessors.add(postProcessor);
}
- 多播器
- 存储位置:AbstractApplicationContext类的applicationEventMulticaster属性
- 监听器
- 存储位置:AbstractApplicationContext#applicationEventMulticaster.defaultRetriever.applicationListeners
这个有点复杂,解释一下:监听器存在多播器applicationEventMulticaster中defaultRetriever属性的applicationListeners属性中。defaultRetriever是AbstractApplicationEventMulticaster的一个内部类,类型为AbstractApplicationEventMulticaster.ListenerRetriever