GenericApplicationContext类解析

GenericApplicationContext类解析

GenericApplicationContext介绍

GenericApplicationContext通用应用程序上下文实现,该实现内部有一个 DefaultListableBeanFactory 实例。可以采用混合方式处理bean的定义,而不是采用特定的bean定义方式来创建bean。

GenericApplicationContext基本就是对DefaultListableBeanFactory 做了个简易的封装,几乎所有方法都是使用了DefaultListableBeanFactory的方法去实现。

GenericApplicationContext更多是作为一个通用的上下文(通用的IOC容器)而存在,BeanFactory本质上也就是一个IOC容器,一个用来生产和获取beans的工厂。

几乎可以把GenericApplicationContext等价于DefaultListableBeanFactory+上下文刷新等其他功能

public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry {
	//内部的beanFactory
	private final DefaultListableBeanFactory beanFactory;

	@Nullable
	private ResourceLoader resourceLoader;

	private boolean customClassLoader = false;

	private final AtomicBoolean refreshed = new AtomicBoolean();


	/**
	 * Create a new GenericApplicationContext.
	 * @see #registerBeanDefinition
	 * @see #refresh
	 */
	public GenericApplicationContext() {
		this.beanFactory = new DefaultListableBeanFactory();
	}
	//......

利用BeanDefinitionReader读取不同资源到上下文中

其实就是通过BeanDefinitionReader将beanDefinition放到DefaultListableBeanFactory中维护的beanDefinitionMap表中

GenericApplicationContext ctx = new GenericApplicationContext();

//其实就是往BeanFactory里面装BeanDefinition
//BeanFactory的实现类一般都维护着一张beanDefinitionMap表
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
xmlReader.loadBeanDefinitions(new ClassPathResource("applicationContext.xml"));

PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(ctx);
propReader.loadBeanDefinitions(new ClassPathResource("applicationContext.properties"));

ctx.refresh();
posted @   鸭梨的药丸哥  阅读(81)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示