1. 程式人生 > 其它 >Spring原始碼 17 IOC refresh方法13

Spring原始碼 17 IOC refresh方法13

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;
}