Spring技术内幕笔记(九)------ApplicationContext和Bean的初始化及销毁

本文探讨了Spring框架中ApplicationContext的初始化和销毁过程,包括在AbstractApplicationContext#prepareBeanFactory阶段的初始化,以及在doClose()方法中的关闭步骤。同时,文章还详细阐述了Bean的初始化通过initializeBean方法实现,以及在容器关闭时Bean的销毁过程,涉及destory()方法及其内部调用的各个阶段。
摘要由CSDN通过智能技术生成

ApplicationContext的初始化及销毁

在Spring中,我们将BeanFactory和ApplicationContext称作它的IOC容器,既然是容器那么他就会存在初始化和销毁的过程,接下来我们就来了解一下容器的初始化和销毁过程。

容器的初始化过程发生在AbstractApplicationContext#prepareBeanFactory中

/**
	 * Configure the factory's standard context characteristics,
	 * such as the context's ClassLoader and post-processors.
	 配置工厂的标准上下文特征,例如上下文的ClassLoader和后置处理器
	 * @param beanFactory the BeanFactory to configure
	 */
	protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
		// Tell the internal bean factory to use the context's class loader etc.
		告诉内部bean工厂使用上下文的类加载器等
		beanFactory.setBeanClassLoader(getClassLoader());
		beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
		beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

		// Configure the bean factory with context callbacks.
		使用上下文回调配置bean工厂
		beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
		beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
		beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
		beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
		beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
		beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
		beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

		// BeanFactory interface not registered as resolvable type in a plain factory.
		// MessageSource registered (and found for autowiring) as a bean.
		beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
		beanFactory.registerResolvableDependency(ResourceLoader.class, this);
		beanFactory.regist
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值