
org.rx.core.IOC Maven / Gradle / Ivy
package org.rx.core;
import lombok.NonNull;
import lombok.SneakyThrows;
import org.apache.commons.lang3.ClassUtils;
import org.rx.bean.WeakIdentityMap;
import org.rx.exception.InvalidException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static org.rx.core.Constants.NON_UNCHECKED;
@SuppressWarnings(NON_UNCHECKED)
public final class IOC {
static final Map, Object> container = new ConcurrentHashMap<>(8);
//不要放值类型
static final Map WEAK_IDENTITY_MAP = new WeakIdentityMap<>();
public static T get(Class type, Class extends T> defType) {
T bean = innerGet(type);
if (bean == null) {
return get(defType);
}
return bean;
}
public static T get(Class type) {
T bean = innerGet(type);
if (bean == null) {
throw new InvalidException("Bean {} not registered", type.getName());
}
return bean;
}
@SneakyThrows
static synchronized T innerGet(Class type) {
T bean = (T) container.get(type);
if (bean == null) {
Class.forName(type.getName());
bean = (T) container.get(type);
}
return bean;
}
public static void register(@NonNull Class type, @NonNull T bean) {
List> types = ClassUtils.getAllSuperclasses(type);
types.remove(Object.class);
for (Class> t : types) {
container.putIfAbsent(t, bean);
}
container.put(type, bean);
}
public static void unregister(Class type) {
container.remove(type);
}
static Map weakIdentityMap(Object ref) {
return (Map) WEAK_IDENTITY_MAP.computeIfAbsent(ref, k -> new ConcurrentHashMap<>(4));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy