(如果对于Spring的整体架构和核心技术不了解,建议先看此篇–Spring源码阅读笔记(一):整体架构与核心技术)
基于Spring框架的应用,最主要的亮点就是bean的IoC。而IoC容器的配置、组装由ApplicationContext
完成,那么我们就从这里开始,去探究IoC如何管理到bean对象。
ApplicationContext初始化简要流程
Spring的应用首先需要定义一个ApplicationContext
,此处我们定义的ApplicationContext
为ClassPathXmlApplicationContext
,它的UML图如下:
我们以XML文件配置Spring来进行分析,首先我们的启动类是这样:
public class Application{
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/context.xml");
ctx.start();
}
}
当我们new ClassPathXmlApplicationContext("/context.xml")
的时候,执行ClassPathXmlApplicationContext
构造器,类内部调用:
public ClassPathXmlApplicationContext(String... configLocations) throws BeansException {
this(configLocations, true, null);
}