Spring原理(1)——容器
容器接口

BeanFactory
是ApplicationContext的父接口,所有ApplicationContext的实现都组合了BeanFactory。
BeanFactory才是Spring的核心容器。

从BeanFactory提供的方法来看,主要是从容器中获取Bean。实际上控制反转,依赖注入以及Bean的生命周期管理,都由它的实现类提供。如下展示了BeanFactory其中一个实现类DefaultListableBeanFactory的继承关系。
可以看到,它的继承路线上有一个DefaultSingletonBeanRegistry类,这个类我们打开可以看到如下代码段,其中singletonObjects对象正是容器中所有单例对象被保存的地方。
public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {
/** Cache of singleton objects: bean name to bean instance. */
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256); // 保存了所有的单例对象
/** Cache of singleton factories: bean name to ObjectFactory. */
private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);
...
}
ApplicationContext
ApplicationContext继承自多个接口,因此提供了多种能力,具体提供的能力如下代码所示。
@SpringBootApplication
public class SsmpApplication {
public static void main(String[] args) throws IOException {
final ConfigurableApplicationContext context = SpringApplication.run(SsmpApplication.class, args);
// MessageSource接口提供的功能,实现国际化能力
context.getMessage("hi", null, Locale.CHINA);
context.getMessage("hi", null, Locale.US);
// ResourcePatternResolver接口提供的能力,实现读取资源文件的能力
final Resource[] resources = context.getResources("classpath*:spring.factories");
// EnvironmentCapable接口提供的能力,实现获取环境参数的能力,可以获取环境变量、配置等
final String java_home = context.getEnvironment().getProperty("java_home");
final String port = context.getEnvironment().getProperty("server.port");
// ApplicationEventPublisher提供的能力,可以发送事件,实现解耦
context.publishEvent(new MyEvent(context));
}
public static class MyEvent extends ApplicationEvent {
public MyEvent(Object source) {
super(source);
}
}
}
容器实现
BeanFactory实现
以下代码的注释中展示了BeanFactory是如何注册Bean的,以及注册后如何进行后处理的。
点击查看代码
package com.leo.ssmp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Collection;
@SpringBootApplication
@Slf4j
public class SsmpApplication {
public static void main(String[] args) {
// 将MyConfig类注册到BeanFactory中,由BeanFactory管理Bean的生成,销毁
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
final BeanDefinition configDefinition = BeanDefinitionBuilder.genericBeanDefinition(MyConfig.class)
.setScope("singleton")
.getBeanDefinition();
beanFactory.registerBeanDefinition("config", configDefinition);
// 向BeanFactory上注册一些后处理器,这些后处理器可以解析@Configuration和@Bean这些注解
AnnotationConfigUtils.registerAnnotationConfigProcessors(beanFactory);
// BeanFactory后处理器,执行后处理操作,处理@Configuration注解,将bean1和bean2生成出来
final Collection<BeanFactoryPostProcessor> beanFactoryPostProcessors = beanFactory.getBeansOfType(
BeanFactoryPostProcessor.class).values();
log.info("---------------BeanFactory后处理器------------------");
beanFactoryPostProcessors.forEach(beanFactoryPostProcessor -> log.info(beanFactoryPostProcessor.toString()));
beanFactoryPostProcessors.forEach(beanFactoryPostProcessor -> {
beanFactoryPostProcessor.postProcessBeanFactory(beanFactory);
});
log.info("---------------BeanFactory后处理器------------------");
// Bean后处理器,针对Bean的生命周期的各个阶段做扩展,例如@Autowired @Resource,
// 这样才能在生成bean1后,向bean1中注入bean2
final Collection<BeanPostProcessor> beanPostProcessors = beanFactory.getBeansOfType(BeanPostProcessor.class)
.values();
log.info("----------------Bean后处理器-----------------");
beanPostProcessors.forEach(beanPostProcessor -> log.info(beanPostProcessor.toString()));
beanPostProcessors.forEach(beanFactory::addBeanPostProcessor);
log.info("----------------Bean后处理器-----------------");
log.info("------------------注册的所有Bean定义信息------------------");
final String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
log.info(beanDefinitionName);
}
log.info("------------------注册的所有Bean定义信息------------------");
final Bean1 bean1 = beanFactory.getBean(Bean1.class);
log.info(bean1.getBean2().toString());
}
@Configuration
static class MyConfig {
@Bean
public Bean1 bean1() {
return new Bean1();
}
@Bean
public Bean2 bean2() {
return new Bean2();
}
}
static class Bean1 {
@Autowired
private Bean2 bean2;
public Bean1() {
log.info("Bean1 construct...");
}
public Bean2 getBean2() {
return bean2;
}
}
static class Bean2 {
public Bean2() {
log.info("Bean2 construct");
}
}
}
以上代码的运行结果如下:
点击查看代码
10:56:43.379 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
10:56:43.395 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
10:56:43.398 [main] INFO com.leo.ssmp.SsmpApplication - ---------------BeanFactory后处理器------------------
10:56:43.398 [main] INFO com.leo.ssmp.SsmpApplication - org.springframework.context.annotation.ConfigurationClassPostProcessor@724af044
10:56:43.398 [main] INFO com.leo.ssmp.SsmpApplication - org.springframework.context.event.EventListenerMethodProcessor@4678c730
10:56:43.512 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
10:56:43.513 [main] INFO com.leo.ssmp.SsmpApplication - ---------------BeanFactory后处理器------------------
10:56:43.513 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
10:56:43.513 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
10:56:43.516 [main] INFO com.leo.ssmp.SsmpApplication - ----------------Bean后处理器-----------------
10:56:43.516 [main] INFO com.leo.ssmp.SsmpApplication - org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor@6385cb26
10:56:43.516 [main] INFO com.leo.ssmp.SsmpApplication - org.springframework.context.annotation.CommonAnnotationBeanPostProcessor@38364841
10:56:43.517 [main] INFO com.leo.ssmp.SsmpApplication - ----------------Bean后处理器-----------------
10:56:43.517 [main] INFO com.leo.ssmp.SsmpApplication - ------------------注册的所有Bean定义信息------------------
10:56:43.517 [main] INFO com.leo.ssmp.SsmpApplication - config
10:56:43.517 [main] INFO com.leo.ssmp.SsmpApplication - org.springframework.context.annotation.internalConfigurationAnnotationProcessor
10:56:43.517 [main] INFO com.leo.ssmp.SsmpApplication - org.springframework.context.annotation.internalAutowiredAnnotationProcessor
10:56:43.517 [main] INFO com.leo.ssmp.SsmpApplication - org.springframework.context.annotation.internalCommonAnnotationProcessor
10:56:43.517 [main] INFO com.leo.ssmp.SsmpApplication - org.springframework.context.event.internalEventListenerProcessor
10:56:43.517 [main] INFO com.leo.ssmp.SsmpApplication - org.springframework.context.event.internalEventListenerFactory
10:56:43.517 [main] INFO com.leo.ssmp.SsmpApplication - bean1
10:56:43.517 [main] INFO com.leo.ssmp.SsmpApplication - bean2
10:56:43.517 [main] INFO com.leo.ssmp.SsmpApplication - ------------------注册的所有Bean定义信息------------------
10:56:43.517 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'bean1'
10:56:43.518 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'config'
10:56:43.529 [main] INFO com.leo.ssmp.SsmpApplication - Bean1 construct...
10:56:43.535 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'bean2'
10:56:43.536 [main] INFO com.leo.ssmp.SsmpApplication - Bean2 construct
10:56:43.537 [main] INFO com.leo.ssmp.SsmpApplication - com.leo.ssmp.SsmpApplication$Bean2@55040f2f
Process finished with exit code 0
后处理器的执行顺序跟它们之间的排序策略有关,先执行的后处理器会生效。
ApplicationContext实现
ClassPathXmlApplicationContext
首先我们定义一个实体类
package com.leo.domain;
import lombok.Data;
@Data
public class Book {
private Integer id;
private String type;
private String name;
private String description;
}
在resources目录下创建b01.xml文件作为Spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="bean1" class="com.leo.domain.Book"/>
</beans>
通常我们会使用如下方式来启动和加载Spring容器
package com.leo;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
@SpringBootApplication
public class SpringApplication {
public static void main(String[] args) {
final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"b01.xml");
}
}
其内部实现如果简单的拆开来,可以用如下步骤表示,BeanFactory是真正的容器,我们使用XmlBeanDefinitionReader将xml中定义的bean转化成BeanDefinition并注册到BeanFactory中。
package com.leo;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;
@SpringBootApplication
public class SpringApplication {
public static void main(String[] args) {
testClasspathXmlApplicationContext();
}
private static void testClasspathXmlApplicationContext() {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
System.out.println("=======================>before");
for (String beanDefinitionName : beanFactory.getBeanDefinitionNames()) {
System.out.println(beanDefinitionName);
}
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
reader.loadBeanDefinitions(new ClassPathResource("b01.xml"));
System.out.println("=======================>after");
for (String beanDefinitionName : beanFactory.getBeanDefinitionNames()) {
System.out.println(beanDefinitionName);
}
}
}
以上代码的运行结果如下:
=======================>before
14:14:13.483 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 6 bean definitions from class path resource [b01.xml]
=======================>after
bean1
我们会发现这是容器里只有一个Bean,但是当我们用springboot自动配置来启动容器时,会发现即使我们不定义Bean,默认也会加载一些内置的bean。那么Spring是如何加载这些内置的Bean的呢。我们可以在xml文件中加入如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="bean1" class="com.leo.ssmp.domain.Book"/>
<!--这里开启了各种注解,例如Autowired-->
<context:annotation-config/>
</beans>
这样我们运行上面的代码时打印出来的结果如下:
=======================>before
14:14:13.483 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 6 bean definitions from class path resource [b01.xml]
=======================>after
bean1
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
可以看到这时已经加载了很多处理注解和事件监听的Bean。
FileSystemXmlApplicationContext
基本原理与ClassPathXmlApplicationContext相似,只不过这里改为从文件系统直接读取xml配置。
AnnotationConfigApplicationContext
直接通过@Configuration注解的类来加载Bean,这种方法加载Bean时,会默认加载注解处理器这些内置的Bean。同时还会将config类也加载为Bean。
Spring原理(1)——容器的更多相关文章
- Spring MVC 原理探秘 - 容器的创建过程
1.简介 在上一篇文章中,我向大家介绍了 Spring MVC 是如何处理 HTTP 请求的.Spring MVC 可对外提供服务时,说明其已经处于了就绪状态.再次之前,Spring MVC 需要进行 ...
- spring 原理1:java 模拟springIOC容器
本篇博客主要是使用java代码模拟spring的IOC容器,实现依赖注入:当然只是模拟spring容器中简单的一点实现原理而已,加深一些自己对spring框架的底层原理的理解: 使用的技术:dom4j ...
- Spring框架IOC容器和AOP解析
主要分析点: 一.Spring开源框架的简介 二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置 一.S ...
- 03 Spring的父子容器
1.概念理解和知识铺垫 在Spring整体框架的核心概念中,容器是核心思想,就是用来管理Bean的整个生命周期的,而在一个项目中,容器不一定只有一个,Spring中可以包括多个容器,而且容器有上下层关 ...
- spring 原理
1.spring原理 内部最核心的就是IOC了,动态注入,让一个对象的创建不用new了,可以自动的生产,这其实就是利用java里的反射,反射其实就是在运行时动态的去创建.调用对象,Spring就是在运 ...
- 学习认识Spring原理
学习认识Spring原理 Spring 是一种业务层框架.搭建Spring框架需要Spring开发包和commons-logging包.Spring的核心思想是控制反转也称依赖注入(创建者--(实例) ...
- Spring框架IOC容器和AOP解析 非常 有用
Spring框架IOC容器和AOP解析 主要分析点: 一.Spring开源框架的简介 二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面 ...
- 7.1 Spring原理
7.1 Spring原理 一.spring是什么?(IOC.AOP.MVC) Spring是一个基于IOC和AOP的结构J2EE系统的框架 , 1.1 IOC 控制反转 是Spring的基础,Inve ...
- Spring——原理解析-利用反射和注解模拟IoC的自动装配
解析Spring的IoC容器基于注解实现的自动装配(自动注入依赖)的原理 1.本文案例 使用注解和反射机制来模拟Spring中IoC的自动装配功能 定义两个注解:@Component,用来标注组件:@ ...
- Spring原理系列一:Spring Bean的生命周期
一.前言 在日常开发中,spring极大地简化了我们日常的开发工作.spring为我们管理好bean, 我们拿来就用.但是我们不应该只停留在使用层面,深究spring内部的原理,才能在使用时融汇贯通. ...
随机推荐
- 使用ELRepo升级CentOS内核
在腾讯云中部署了一些服务器,操作系统使用的是CentOS 7.6,但是其默认内核版本较低,现使用ELRepo对CentOS的内核进行升级. 操作环境 服务器:腾讯云轻量应用服务器 操作系统:CentO ...
- Activiti7开发(四)-我的待办
目录 1. 查询登录用户的待办任务 2.审批 1. 查询登录用户的待办任务 private List<Task> queryMyTasks(){ String username = Sec ...
- 微软出品自动化神器【Playwright+Java】系列(十二)测试框架的设计与开发
一.前言 大家好,我是六哥! 又有好长一段时间没更文了,不是我懒,而是确实在更文上,没有以前积极了,这里是该自我检讨的. 其实不是我不积极,而是相对更文学习来说,优先级不是最高. 对我而言,目前最重要 ...
- 声网Agora 教育 aPaaS 灵动课堂升级:UI与业务逻辑分离,界面、功能自定义更灵活
声网Agora 教育 aPaaS 产品灵动课堂现已升级至 v1.1.0 版本.声网Agora 灵动课堂可以帮助教育机构和开发者最快 15 分钟上线自有品牌.全功能的在线互动教学平台,节省 90% 开发 ...
- 近期调研和使用 zeromq 与 cppzmq 的一些问题
关于message 消息分片 消息分片的发送 消息分片允许将多个消息封装成一条消息.在发送自定义协议数据时,我们经常需要在消息前"填充"一个包头.如下代码,在发送的时候加上 zmq ...
- 写书写到一半,强迫症发作跑去给HotChocolate修bug
前言 这是写作<C#与.NET6 开发从入门到实践>时的小故事,作为本书正式上市的宣传,在此分享给大家. 正文 .NET目前有两个比较成熟的GraphQL框架,其中一个是HotChocol ...
- 算法总结--ST表
声明(叠甲):鄙人水平有限,本文为作者的学习总结,仅供参考. 1. RMQ 介绍 在开始介绍 ST 表前,我们先了解以下它以用的场景RMQ问题.RMQ (Range Minimum/Maximum Q ...
- 电商平台趋势妙手采集类API接口
电商平台趋势,平台化.大家可以看到大的电商都开始有自己的平台,其实这个道理很清楚,就是因为这是充分利用自己的流量.自己的商品和服务大效益化的一个过程,因为有平台,可以利用全社会的资源弥补自己商品的丰富 ...
- vue指令之事件指令
目录 什么是事件指令 示例 什么是事件指令 事件指的是:点击事件,双击事件,划动事件,焦点事件... 语法 v-on:事件名='函数' # 注意:函数必须写在 methods配置项中 示例 # 点击按 ...
- python入门教程之八列表,字典,字符串,集合常用操作
一列表常用方法 Python包含以下函数: 序号 函数 1 cmp(list1, list2)比较两个列表的元素 2 len(list)列表元素个数 3 max(list)返回列表元素最大值 4 mi ...