参考源

https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click

https://www.bilibili.com/video/BV12Z4y197MU?spm_id_from=333.999.0.0

《Spring源码深度解析(第2版)》

版本

本文章基于 Spring 5.3.15


Spring IOC 的核心是 AbstractApplicationContextrefresh 方法。

其中一共有 13 个主要方法,这里分析第 13 个:resetCommonCaches

1 AbstractApplicationContext

1-1 清空缓存

resetCommonCaches()
protected void resetCommonCaches() {
// 清空反射缓存
ReflectionUtils.clearCache();
// 清空注解缓存
AnnotationUtils.clearCache();
// 清空并发缓存
ResolvableType.clearCache();
// 清空类加载器
CachedIntrospectionResults.clearClassLoader(getClassLoader());
}

1-2 清空反射缓存

ReflectionUtils.clearCache()

2 ReflectionUtils

private static final Map<Class<?>, Method[]> declaredMethodsCache = new ConcurrentReferenceHashMap<>(256);
private static final Map<Class<?>, Field[]> declaredFieldsCache = new ConcurrentReferenceHashMap<>(256); public static void clearCache() {
declaredMethodsCache.clear();
declaredFieldsCache.clear();
}

1 AbstractApplicationContext

1-2 清空注解缓存

AnnotationUtils.clearCache()
public static void clearCache() {
// 清空类型映射缓存
AnnotationTypeMappings.clearCache();
// 清空注解扫描缓存
AnnotationsScanner.clearCache();
}

3-1 清空类型映射缓存

AnnotationTypeMappings.clearCache()

4 AnnotationTypeMappings

private static final Map<AnnotationFilter, Cache> standardRepeatablesCache = new ConcurrentReferenceHashMap<>();
private static final Map<AnnotationFilter, Cache> noRepeatablesCache = new ConcurrentReferenceHashMap<>(); static void clearCache() {
standardRepeatablesCache.clear();
noRepeatablesCache.clear();
}

3 AnnotationUtils

3-1 清空注解扫描缓存

AnnotationsScanner.clearCache()

5 AnnotationsScanner

private static final Map<AnnotatedElement, Annotation[]> declaredAnnotationCache = new ConcurrentReferenceHashMap<>(256);
private static final Map<Class<?>, Method[]> baseTypeMethodsCache = new ConcurrentReferenceHashMap<>(256); static void clearCache() {
declaredAnnotationCache.clear();
baseTypeMethodsCache.clear();
}

1 AbstractApplicationContext

1-2 清空并发缓存

ResolvableType.clearCache()

6 ResolvableType

private static final ConcurrentReferenceHashMap<ResolvableType, ResolvableType> cache = new ConcurrentReferenceHashMap<>(256);

public static void clearCache() {
cache.clear();
SerializableTypeWrapper.cache.clear();
}

1 AbstractApplicationContext

1-2 清空类加载器

CachedIntrospectionResults.clearClassLoader(getClassLoader())

7 CachedIntrospectionResults

static final Set<ClassLoader> acceptedClassLoaders = Collections.newSetFromMap(new ConcurrentHashMap<>(16));
static final ConcurrentMap<Class<?>, CachedIntrospectionResults> strongClassCache = new ConcurrentHashMap<>(64);
static final ConcurrentMap<Class<?>, CachedIntrospectionResults> softClassCache = new ConcurrentReferenceHashMap<>(64); public static void clearClassLoader(@Nullable ClassLoader classLoader) {
// 判断是否在类加载器下面
acceptedClassLoaders.removeIf(registeredLoader -> isUnderneathClassLoader(registeredLoader, classLoader));
strongClassCache.keySet().removeIf(beanClass -> isUnderneathClassLoader(beanClass.getClassLoader(), classLoader));
softClassCache.keySet().removeIf(beanClass -> isUnderneathClassLoader(beanClass.getClassLoader(), classLoader));
}

7-1 判断是否在类加载器下面

isUnderneathClassLoader(registeredLoader, classLoader)
private static boolean isUnderneathClassLoader(@Nullable ClassLoader candidate, @Nullable ClassLoader parent) {
if (candidate == parent) {
return true;
}
if (candidate == null) {
return false;
}
ClassLoader classLoaderToCheck = candidate;
while (classLoaderToCheck != null) {
classLoaderToCheck = classLoaderToCheck.getParent();
if (classLoaderToCheck == parent) {
return true;
}
}
return false;
}

Spring源码 18 IOC refresh方法13的更多相关文章

  1. Spring源码 07 IOC refresh方法2

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

  2. Spring源码 16 IOC refresh方法11

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

  3. Spring源码 17 IOC refresh方法12

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

  4. Spring源码 15 IOC refresh方法10

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

  5. Spring源码 14 IOC refresh方法9

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

  6. Spring源码 11 IOC refresh方法6

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

  7. Spring源码 09 IOC refresh方法4

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

  8. Spring源码 08 IOC refresh方法3

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

  9. Spring源码 06 IOC refresh方法1

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

随机推荐

  1. 创建NuGet本地包源

    NuGet 是免费.开源的包管理开发工具,专注于在 .NET 应用开发过程中,简单地合并第三方的组件库.使用Visual Studio 可以很方便地将类库等项目打包发布,最简单的办法是上传到Nuget ...

  2. C++编码规范(本人自定义)

    C++编码规范 1.变量名用camelCase命名法(即lowerCamelCase,小驼峰拼写法)命名. 即小写字母开头,如果变量名是复合词,第二个单词的首字母大写. 举例: int digitsC ...

  3. 渗透开源工具之sqlmap安装配置环境变量教程

    由于计算机安全牵涉到很多方面,建议自己在服务器上搭建自己的靶场,如何搭建靶场请订阅并查看作者上期教程,这里作者先为大家推荐一个免费开源升级靶场:https://hack.zkaq.cn/   在封神台 ...

  4. CabloyJS V3.2.0支持Socket IO

    CabloyJS v3.2.0引入了Socket IO,并且实现了统一的在线推送和离线推送机制 效果演示 1. IM 用户向系统发送一条消息,系统通过websocket在线通道向用户推送一条回复 2. ...

  5. 十二张图:从0开始理解对称/非对称加密、CA认证、以及K8S各组件颁发证书原由

    目录 一.对称加密 二.对称加密-不安全 三.非对称加密 四.非对称加密-不安全 五.对称加密和非对称加密结合 六.对称加密和非对称加密结合-不安全 七.Https的做法-引入CA机构 八.乘胜追击理 ...

  6. Improved Security for a Ring-Based Fully Homomorphic Encryption Scheme-2013:解读

    本文记录阅读此论文的笔记 摘要 (1)1996年,HPS三人提出一个格上的高效加密方案,叫做NTRUEncrypt,但是没有安全性证明:之后2011年,SS等人修改此方案,将其安全规约到标准格上的困难 ...

  7. go-zero微服务实战系列(四、CRUD热热身)

    上一篇文章我们把整个项目的架子搭建完成,服务在本地也已经能运行起来了,顺利成章的接下来我们就应该开始写业务逻辑代码了,但是单纯的写业务逻辑代码是比较枯燥的,业务逻辑的代码我会不断地补充到 lerbon ...

  8. C++对象间通信组件,让C++对象“无障碍交流”

    介绍 这是很久之前的一个项目了,最近刚好有些时间,就来总结一下吧! 推荐初步熟悉项目后阅读本文: https://gitee.com/smalldyy/easy-msg-cpp 从何而来 这要从我从事 ...

  9. hadoop集群搭建——单节点(伪分布式)

    1. 准备工作: 前提:需要电脑安装VM,且VM上安装一个Linux系统 注意:本人是在学习完尚学堂视频后,结合自己的理解,在这里做的总结.学习的视频是:大数据. 为了区分是在哪一台机器做的操作,eg ...

  10. 一次 Keepalived 高可用的事故,让我重学了一遍它!

    原文首发: 你好,我是悟空. 前言 上次我们遇到了一个 MySQL 故障的事故,这次我又遇到了另外一个奇葩的问题: Keepalived 高可用组件的虚拟 IP 持续漂移,导致 MySQL 主从不断切 ...