博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring源码 18 IOC refresh方法13
阅读量:37194 次
发布时间:2020-08-01

本文共 3537 字,大约阅读时间需要 11 分钟。

参考源

《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
, Method[]> declaredMethodsCache = new ConcurrentReferenceHashMap<>(256);private static final Map
, 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
standardRepeatablesCache = new ConcurrentReferenceHashMap<>();private static final Map
noRepeatablesCache = new ConcurrentReferenceHashMap<>();static void clearCache() { standardRepeatablesCache.clear(); noRepeatablesCache.clear();}

3 AnnotationUtils

3-1 清空注解扫描缓存

AnnotationsScanner.clearCache()

5 AnnotationsScanner

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

1 AbstractApplicationContext

1-2 清空并发缓存

ResolvableType.clearCache()

6 ResolvableType

private static final ConcurrentReferenceHashMap
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
acceptedClassLoaders = Collections.newSetFromMap(new ConcurrentHashMap<>(16));static final ConcurrentMap
, CachedIntrospectionResults> strongClassCache = new ConcurrentHashMap<>(64);static final ConcurrentMap
, 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;}

转载地址:http://kypwwy.baihongyu.com/

你可能感兴趣的文章
几个基本的 Sql Plus 命令 和 例子
查看>>
PLSQL单行函数和组函数详解
查看>>
Oracle PL/SQL语言初级教程之异常处理
查看>>
Oracle PL/SQL语言初级教程之游标
查看>>
Oracle PL/SQL语言初级教程之操作和控制语言
查看>>
Oracle PL/SQL语言初级教程之过程和函数
查看>>
Oracle PL/SQL语言初级教程之表和视图
查看>>
Oracle PL/SQL语言初级教程之完整性约束
查看>>
PL/SQL学习笔记
查看>>
如何分析SQL语句
查看>>
结构化查询语言(SQL)原理
查看>>
SQL教程之嵌套SELECT语句
查看>>
几个简单的SQL例子
查看>>
日本語の記号の読み方
查看>>
计算机英语编程中一些单词
查看>>
JavaScript 经典例子
查看>>
判断数据的JS代码
查看>>
js按键事件说明
查看>>
AJAX 初次体验!推荐刚学看这个满好的!
查看>>
AJAX 设计制作 在公司弄的 非得要做出这个养的 真晕!
查看>>