Spring源码 18 IOC refresh方法13

参考源

这是我见过最好的Spring高级底层原理源码教程,大家觉得呢?(2022最新版)_哔哩哔哩_bilibili

这可能是全网讲的最好的Spring源码教程(2021年度B站最佳)_哔哩哔哩_bilibili

《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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天航星

感谢你的鼓励和认可

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值