Spring IoC容器构建过程分析(一)

本文深入剖析了Spring IoC容器的构建过程,特别是ClassPathXmlApplicationContext的构造方法调用AbstractApplicationContext.refresh()方法的12个步骤。从准备刷新、创建BeanFactory到解析配置文件、注册Bean处理器,详细解释了每个步骤的功能,帮助读者理解Spring的核心原理和扩展点。
摘要由CSDN通过智能技术生成

注:本文分析的内容,针对的是Spring 2.5.6的版本
ApplicationContext是spring IoC容器的顶级接口,其类结构图如下:

    从上面的类图中可以看出, ApplicationContext继承了ResourceLoader接口,便于获取外部资源;也间接继承了 BeanFactory接口,这样可以在Spring容器中创建Bean对象;同时也继承了ApplicationEventPublisher接口,用于发送一些事件消息。
    通常我们使用这样的一行代码来创建并启动Spring容器:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
 虽然是简单的一行代码,但背后却做了很多事情,先看一下ClassPathXmlApplicationContext的构造方法:
	public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
		this(new String[] {configLocation}, true, null);
	}
	public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
			throws BeansException {

		super(parent);
		setConfigLocations(configLocations);
		if (refresh) {
			refresh();
		}
	}
在该构造方法中,最后调用了AbstractApplicationContext.refresh()方法,该方法的实现代码如下:
public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			//1、 Prepare this context for refreshing.
			prepareRefresh();

			//2、 Tell the subclass to refresh the internal bean factory.
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			//3、 Prepare the bean factory for use in this context.
			prepareBeanFactory(beanFactory);

			try {
				//4、 Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);

				//5、 Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);

				//6、 Register bean processors that intercept bean creation.
				registerBeanPostProcessors(beanFactory);

				//7、 Initialize message source for this context.
				initMessageSource();

				//8、 Initialize event multicaster for this context.
				initApplicationEventMulticaster();

				//9、 Initialize other special beans in specific context subclasses.
				onRefresh();

				//10、 Check for listener beans and register them.
				registerListeners();

				//11、 Instantiate all remaining (non-lazy-init) singletons.
				finishBeanFactoryInitialization(beanFactory);

				//12、 Last step: publish corresponding event.
				finishRefresh();
			}

			catch (BeansException ex) {
				// Destroy already created singletons to avoid dangling resources.
				beanFactory.destroySingletons();

				// Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}
		}
	}
这个方法非常关键,通过调用12个其他的方法,完成了整个IoC容器的构建。详细了解这个方法里面的每一行代码,基本上就掌握了Spring大部分的原理、功能和扩展点。下面分析每一个方法的所做的事情。

1、prepareRefresh()
该方法所做的事情相对比较简单:记录容器启动的时间,并设置容器处于活跃状态。

2、obtainFreshBeanFactory()
该方法的作用:创建BeanFactory实例,并解析Spring的xml配置文件。beanFactory的实现类是:ConfigurableListableBeanFactory。方法的实现如下:
	protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
		refreshBeanFactory();
		ConfigurableListableBeanFactory beanFactory = getBeanFactory();

		if (logger.isInfoEnabled()) {
			logger.info("Bean factory for application context [" &#
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
当一个Spring应用程序启动时,它会先通过Spring IOC容器构建所有的应用程序组件。以下是Spring IOC容器构建流程的详细步骤: 1. 加载配置文件:Spring IOC容器首先会读取配置文件(如XML文件),并根据配置文件中的信息创建一个或多个BeanDefinition对象,其中BeanDefinition对象描述了Bean的属性和依赖关系。 2. 创建Bean实例:容器根据BeanDefinition对象中的信息创建Bean实例。包括以下几个步骤: - 实例化Bean:容器根据BeanDefinition中的类信息创建Bean实例,可以使用Java反射机制实现。 - 设置Bean属性:容器将BeanDefinition中的属性值设置到Bean实例中。 - 解析依赖关系:容器解析Bean之间的依赖关系,为每个依赖注入合适的Bean实例。 3. 注册Bean实例:容器将创建好的Bean实例注册到BeanFactory中。 4. 预处理Bean实例:容器对Bean实例进行预处理,包括Bean实例的初始化和Bean实例的后置处理。 5. 容器启动:容器启动时,会触发所有已注册Bean实例的初始化方法。 6. 容器关闭:当应用程序关闭时,容器会销毁所有已创建的Bean实例,释放资源。 总之,Spring IOC容器构建流程的核心是BeanDefinition对象的创建和Bean实例的创建。容器会根据BeanDefinition中的信息创建Bean实例,并根据Bean之间的依赖关系将Bean实例注入到其他Bean中。最终,容器启动时会初始化所有已注册的Bean实例,应用程序结束时容器会销毁所有已创建的Bean实例。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值