半日闲

因过竹院逢僧话,又得浮生半日闲

0%

逻辑解析

  1. 判断SPI类信息是否符合扩展点规范, 判断是否为空, 判断是否为接口, 判断是否被SPI注解修饰
  2. 在扩展点实例缓存中获取类的扩展实例,如果存在,直接返回;如果不存在,则为该SPI接口创建一个扩展点类实例,在返回。
  3. 在创建SPI接口的扩展点类实例的时候,需要判断该SPI是否为ExtensionFactory,如果是,则成员变量ObjectFactory不赋值, 否则,创建一个objectFactory的适配实例 实例化过程

    相关代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    public static <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) {
    if (type == null) {
    throw new IllegalArgumentException("Extension type == null");
    }
    if (!type.isInterface()) {
    throw new IllegalArgumentException("Extension type (" + type + ") is not an interface!");
    }
    if (!withExtensionAnnotation(type)) {
    throw new IllegalArgumentException("Extension type (" + type +
    ") is not an extension, because it is NOT annotated with @" + SPI.class.getSimpleName() + "!");
    }
    //如果存在,直接返回,如果不存在,创建一个再返回
    ExtensionLoader<T> loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
    if (loader == null) {
    EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader<T>(type));
    loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
    }
    return loader;
    }
    //实例化
    private ExtensionLoader(Class<?> type) {
    this.type = type;
    objectFactory =
    (type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension());
    }
阅读全文 »

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//名称分隔器正则
private static final Pattern NAME_SEPARATOR = Pattern.compile("\\s*[,]+\\s*");

//扩展点加载器缓存
private static final ConcurrentMap<Class<?>, ExtensionLoader<?>> EXTENSION_LOADERS = new ConcurrentHashMap<>(64);

//扩展点实例缓存
private static final ConcurrentMap<Class<?>, Object> EXTENSION_INSTANCES = new ConcurrentHashMap<>(64);

//SPI接口
private final Class<?> type;


private final ExtensionFactory objectFactory;

//单个SPI接口缓存到的扩展点名称
private final ConcurrentMap<Class<?>, String> cachedNames = new ConcurrentHashMap<>();

//单个SPI接口实现类缓存
private final Holder<Map<String, Class<?>>> cachedClasses = new Holder<>();

//activate实现类缓存
private final Map<String, Object> cachedActivates = new ConcurrentHashMap<>();
//SPI实现类实例缓存
private final ConcurrentMap<String, Holder<Object>> cachedInstances = new ConcurrentHashMap<>();
//adaptive实例缓存
private final Holder<Object> cachedAdaptiveInstance = new Holder<>();
//adaptive包装类缓存
private volatile Class<?> cachedAdaptiveClass = null;
//默认扩展名缓存
private String cachedDefaultName;
//创建adaptive适配实例异常错误
private volatile Throwable createAdaptiveInstanceError;

//SPI实现类包装类缓存
private Set<Class<?>> cachedWrapperClasses;

//异常集合
private Map<String, IllegalStateException> exceptions = new ConcurrentHashMap<>();


/**
* Record all unacceptable exceptions when using SPI
*/
private Set<String> unacceptableExceptions = new ConcurrentHashSet<>();

//扩展点加载策略 采用传统spi技术加载
private static volatile LoadingStrategy[] strategies = loadLoadingStrategies();