从ApplicationContext bf = new ClassPathXmlApplicationContext("applicationContextxml");说开来1

本文详细解析了Spring框架中ApplicationContext的初始化过程,包括构造器调用、配置文件加载、beanFactory初始化及刷新等关键步骤。

ApplicationContext bf = new ClassPathXmlApplicationContext(“applicationContext.xml”);
1.首先调用构造器:

public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
		this(new String[] {configLocation}, true, null);
	}

2.再调用构造器:

public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
			throws BeansException {

		super(parent);
		setConfigLocations(configLocations);
		if (refresh) {
			refresh();
		}
	}

3.setConfigLocations(configLocations);方法:

public void setConfigLocations(String... locations) {
		if (locations != null) {
			Assert.noNullElements(locations, "Config locations must not be null");
			this.configLocations = new String[locations.length];
			for (int i = 0; i < locations.length; i++) {
				this.configLocations[i] = resolvePath(locations[i]).trim();
			}
		}
		else {
			this.configLocations = null;
		}
	}

设置applicationContext.xml到configLocations数组中。
4.refresh()方法:

public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.
			//为刷新做上下文的环境准备
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			//初始化beanFactory,读取applicationContext.xml文件,使具有bean操作的所有功能
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			//为beanFactory在上下文使用做准备,也就是扩展bean的功能,支持@autowired等注解
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				//允许子类进行功能扩展的方法
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				//在上下文中以bean的形式激活工厂处理器
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				//注册拦截bean创建的bean处理器
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				//初始化信息资源
				initMessageSource();

				// Initialize event multicaster for this context.
				//初始化事件广播
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				//初始化特殊上下文子类找那个其他特殊的bean,用以子类功能扩展
				onRefresh();

				// Check for listener beans and register them.
				//检查bean监听器并注册
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				//初始化所有的剩下(非懒加载)的单例
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				//发布通知事件
				finishRefresh();
			}

			catch (BeansException ex) {
				logger.warn("Exception encountered during context initialization - cancelling refresh attempt", ex);

				// Destroy already created singletons to avoid dangling resources.
				destroyBeans();

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

				// Propagate exception to caller.
				throw ex;
			}
		}
	}

总结refresh()方法中的主要功能:
1.为刷新做上下文的环境准备
2.初始化beanFactory,读取applicationContext.xml文件,使具有bean操作的所有功能
3.为beanFactory在上下文使用做准备,也就是扩展bean的功能,支持@autowired等注解
4.允许子类进行功能扩展的方法
5.在上下文中以bean的形式激活工厂处理器
6.注册拦截bean创建的bean处理器
7.初始化信息资源
8.初始化事件广播
9.初始化特殊上下文子类找那个其他特殊的bean,用以子类功能扩展
10.检查bean监听器并注册
11.初始化所有的剩下(非懒加载)的单例
12.发布通知事件

`ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");` 这行代码通常用于Spring框架中初始化基于XML配置的应用上下文环境。 让我们逐步解析这句声明: ### 1. `ApplicationContext` 这是Spring框架中的核心接口之一,表示应用程序上下文。它不仅包含了BeanFactory的所有功能,还添加了对国际化、资源加载以及事件传播等的支持。简单来,通过这个接口可以获取到容器管理的各种bean实例,并能访问一些额外的服务特性。 ### 2. `ClassPathXmlApplicationContext` 这是一个实现了`ApplicationContext`接口的具体类。正如其名所示,它可以读取位于classpath下的XML文件并根据其中的内容创建相应的IoC容器。这意味着你可以在项目的resources目录下放置包含所有beans定义信息的XML文档,然后让`ClassPathXmlApplicationContext`去查找它们的位置。 例如,在项目结构中有这样一个路径:`src/main/resources/applicationContext.xml`, 那么构造函数将自动从这里加载所需的配置数据。 ### 3. 初始化过程 当你执行这行命令时: ```java ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ``` 实际上发生了下面几件事: - Spring会先定位名为 `"applicationContext.xml"` 的xml 文件; - 解析该文件内的 beans 定义; - 将所有的 bean 实例化并且注入依赖; - 最终返回给开发者一个完全准备好的 IoC 容器 (`ApplicationContext`) 对象供后续操作使用; 注意这里的参数是一个字符串类型的值,代表的是相对于 classpath 根目录的一个 XML 路径表达式,可以根据实际需要指定不同的配置文件名称或位置。 如果你只写了`new ClassPathXmlApplicationContext()`而没有传递具体的文件路径,则会抛出异常,因为程序不知道去哪里寻找配置信息来构建容器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值