|
| 1 | +package cn.iocoder.springboot.lab67.nettycommondemo.dispacher; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | +import org.springframework.aop.framework.AopProxyUtils; |
| 6 | +import org.springframework.beans.factory.InitializingBean; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.context.ApplicationContext; |
| 9 | + |
| 10 | +import java.lang.reflect.ParameterizedType; |
| 11 | +import java.lang.reflect.Type; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Objects; |
| 15 | + |
| 16 | +public class MessageHandlerContainer implements InitializingBean { |
| 17 | + |
| 18 | + private Logger logger = LoggerFactory.getLogger(getClass()); |
| 19 | + |
| 20 | + /** |
| 21 | + * 消息类型与 MessageHandler 的映射 |
| 22 | + */ |
| 23 | + private final Map<String, MessageHandler> handlers = new HashMap<>(); |
| 24 | + |
| 25 | + @Autowired |
| 26 | + private ApplicationContext applicationContext; |
| 27 | + |
| 28 | + @Override |
| 29 | + public void afterPropertiesSet() throws Exception { |
| 30 | + // 通过 ApplicationContext 获得所有 MessageHandler Bean |
| 31 | + applicationContext.getBeansOfType(MessageHandler.class).values() // 获得所有 MessageHandler Bean |
| 32 | + .forEach(messageHandler -> handlers.put(messageHandler.getType(), messageHandler)); // 添加到 handlers 中 |
| 33 | + logger.info("[afterPropertiesSet][消息处理器数量:{}]", handlers.size()); |
| 34 | + } |
| 35 | + |
| 36 | + protected MessageHandler getMessageHandler(String type) { |
| 37 | + MessageHandler handler = handlers.get(type); |
| 38 | + if (handler == null) { |
| 39 | + throw new IllegalArgumentException(String.format("类型(%s) 找不到匹配的 MessageHandler 处理器", type)); |
| 40 | + } |
| 41 | + return handler; |
| 42 | + } |
| 43 | + |
| 44 | + public static Class<? extends Message> getMessageClass(MessageHandler handler) { |
| 45 | + // 获得 Bean 对应的 Class 类名。因为有可能被 AOP 代理过。 |
| 46 | + Class<?> targetClass = AopProxyUtils.ultimateTargetClass(handler); |
| 47 | + // 获得接口的 Type 数组 |
| 48 | + Type[] interfaces = targetClass.getGenericInterfaces(); |
| 49 | + Class<?> superclass = targetClass.getSuperclass(); |
| 50 | + while ((Objects.isNull(interfaces) || 0 == interfaces.length) && Objects.nonNull(superclass)) { // 此处,是以父类的接口为准 |
| 51 | + interfaces = superclass.getGenericInterfaces(); |
| 52 | + superclass = targetClass.getSuperclass(); |
| 53 | + } |
| 54 | + if (Objects.nonNull(interfaces)) { |
| 55 | + // 遍历 interfaces 数组 |
| 56 | + for (Type type : interfaces) { |
| 57 | + // 要求 type 是泛型参数 |
| 58 | + if (type instanceof ParameterizedType) { |
| 59 | + ParameterizedType parameterizedType = (ParameterizedType) type; |
| 60 | + // 要求是 MessageHandler 接口 |
| 61 | + if (Objects.equals(parameterizedType.getRawType(), MessageHandler.class)) { |
| 62 | + Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); |
| 63 | + // 取首个元素 |
| 64 | + if (Objects.nonNull(actualTypeArguments) && actualTypeArguments.length > 0) { |
| 65 | + return (Class<Message>) actualTypeArguments[0]; |
| 66 | + } else { |
| 67 | + throw new IllegalStateException(String.format("类型(%s) 获得不到消息类型", handler)); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + throw new IllegalStateException(String.format("类型(%s) 获得不到消息类型", handler)); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments