Spring中IOC容器初始化过程解析
Posted 踩踩踩从踩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring中IOC容器初始化过程解析相关的知识,希望对你有一定的参考价值。
前言
本篇文章主要解析spring框架ioc容器初始过程,包含启动过程中,涉及到的一些类,ApplicationContext继承体系,以及各个部分涉及到的一部分功能,BeanFactory继承体系 对比着之前设计的ioc上差别部分,功能更加全面;本篇文章主要是解析其源码,而不是单纯的记那是那里,而是理解设计思路才是最重要的。
IOC容器初始化过程
Ioc容器是什么,解决了什么问题,这也是spring中很重要得概念。也就是 ApplicationContext
包括了 用法和工作流程。

包括上面得用法
Spring的用法
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
public class AnnotationConfig
public static void main(String[] args)
ApplicationContext context = new AnnotationConfigApplicationContext(
"edu.dongnao.courseware.spring.bean");
Boy boy = context.getBean(Lad.class);
boy.sayLove();
@Configuration
@ComponentScan("edu.dongnao.courseware.spring.runner")
public class JavaBaseConfig
public MagicGril magicGril()
return new MagicGril("magic gril");
public static void main(String[] args)
ApplicationContext context = new AnnotationConfigApplicationContext(JavaBaseConfig.class);
MagicGril gril = context.getBean(MagicGril.class);
gril.toString();
这是在spring中提供给我们 获取bean得方式。
所有的东西在applicationcontext 做了一个整合 如何加载bean定义。
Applicationcontext是什么
在注释上说了,提供了可配置的应用,他是一个在运行时只读的应用,但是可以实现重新加载的子类。
包括在看 spring中继承的体系中。
- Environment接口
- HierarchicalBeanFactory接口
在ApplicationContext中提供的方法,包含了 提供的方法 ,id 应用名称等等。
ApplicationContext的实现体系
- ConfigurableApplicationContext子接口

- AbstractApplicationContext抽象实现类

利用模板方法的设计模式,

这些都是 抽象类 而且 这个公共抽象类中,并没有bean工厂。利用 getbeanfactory方法 去获取bean工厂。
- AbstractRefreshableApplicationContext


- GenericApplicationContext
它实现了BeanDefinitionRegistry接口,该接口定义了bean定义信息的注册行为。即我们可以直接往GenericApplicationContext中注册bean定义。
GenericApplicationContext的bean定义注册是不是委托给了持有的


ApplicationContext接口一共有6个实现类
BeanFactory继承体系
一定要探求beanfactory到defaultListableBeanfactory.
关键实现类:DefaultListableBeanFactory
@Configuration
@ComponentScan("edu.courseware")
public class Application
public static void main(String[] args)
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
Lad cs = context.getBean("swk", Lad.class);
cs.sayLove();
ApplicationContext context1 = new FileSystemXmlApplicationContext("F:/workspace/idea/vip/spring-source/src/main/resources/application.xml");
cs = context1.getBean("lad", Lad.class);
cs.sayLove();
context1 = new GenericXmlApplicationContext("file:F:/workspace/idea/vip/spring-source/src/main/resources/application.xml");
cs = context1.getBean("swk", Lad.class);
cs.sayLove();
// 注解的方式
ApplicationContext context2 = new AnnotationConfigApplicationContext(Application.class);
Lad cs2 = context2.getBean(Lad.class);
cs2.sayLove();
System.out.println("------------------------------------------------------");
GenericApplicationContext context3 = new GenericApplicationContext();
new XmlBeanDefinitionReader(context3).loadBeanDefinitions("classpath:application.xml");
new ClassPathBeanDefinitionScanner(context3).scan("edu.dongnao.courseware");
// 一定要刷新
context3.refresh();
cs2 = context3.getBean("swk", Lad.class);
cs2.sayLove();
MagicGril ab = context3.getBean(MagicGril.class);
ab.start();
// 用法跟GenericApplicationContext一样
// ApplicationContext context4 = new StaticApplicationContext();
其中最重要的就是Refresh()方法。
其中会做很多 包括 刷新做准备 这在 applicationcontext中会用到的。
以上是关于Spring中IOC容器初始化过程解析的主要内容,如果未能解决你的问题,请参考以下文章