一文了解spring中常用组件的存储位置

前言

  Spring中大部分常用组件存在BeanFactory和ApplicationContext中,我们下面也是从这两类中的属性介绍的。如果我们需要使用这些容器助中的组件,获取到这两个实例就可以了。获取这两个实例的方法,可以使用Aware接口实现。

一、BeanFactory

先来看下XmlBeanFactory类的关系图(Mac快捷键是:command+option+U)
在这里插入图片描述
可以看到BeanFactory的具体类只有XmlBeanFactoryDefaultListableBeanFactory,现在XmlBeanFactory已过期,BeanFactory就只剩唯一的实现类DefaultListableBeanFactory了,应该说使用spring的所有bean工厂,都是使用DefaultListableBeanFactory类来创建的

  1. 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方法
  1. 单例Bean
  • 存储位置:DefaultSingletonBeanRegistry类的singletonObjects属性
/** Cache of singleton objects: bean name --> bean instance */
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
  1. 注册过的Bean名称
  • 存储位置:DefaultSingletonBeanRegistry类的registeredSingletons属性
/** Set of registered singletons, containing the bean names in registration order */
private final Set<String> registeredSingletons = new LinkedHashSet<>(256);
  1. 手动注册的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();
	}
  1. 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);
		}
	}
  1. 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之外扩展的功能。

  1. 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);
		}
	}
  1. 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);
	}
  1. 多播器
  • 存储位置:AbstractApplicationContext类的applicationEventMulticaster属性
  1. 监听器
  • 存储位置:AbstractApplicationContext#applicationEventMulticaster.defaultRetriever.applicationListeners
    这个有点复杂,解释一下:监听器存在多播器applicationEventMulticaster中defaultRetriever属性的applicationListeners属性中。defaultRetriever是AbstractApplicationEventMulticaster的一个内部类,类型为AbstractApplicationEventMulticaster.ListenerRetriever
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值