package com.zllog.factorytest.factory;
import com.google.common.collect.Maps;
import com.zllog.factorytest.annotation.HandlerType;
import com.zllog.factorytest.util.SpringContextUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@SuppressWarnings("unchecked")
public class HandlerProcessor implements BeanFactoryPostProcessor {
private static final String HANDLER_PACKAGE = "com.zllog.factorytest.factory";
/**
* 扫描@HandlerType , 初始化HandlerContext, 将其注册到spring容器
* @param configurableListableBeanFactory
* @throws BeansException
*/
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
Map<String, Class> handlerMap = Maps.newHashMapWithExpectedSize(3);
//TODO 扫描指定的包,获取类上面的注解 根据注解不同,将不同的类放到map中
handlerMap.put(getAnnotation(AceHandler.class), AceHandler.class);
handlerMap.put(getAnnotation(Handler.class), Handler.class);
handlerMap.put(getAnnotation(LuffyHandler.class), LuffyHandler.class);
HandlerContext handlerContext = new HandlerContext(handlerMap);
configurableListableBeanFactory.registerSingleton(HandlerContext.class.getName(), handlerContext);
}
public String getAnnotation(Class clazz) {
HandlerType handlerType = (HandlerType) clazz.getAnnotation(HandlerType.class);
if (handlerType != null) {
return handlerType.value();
} else {
return "";
}
}
}
通过反射,获取类注解上面的值
最新推荐文章于 2023-07-09 22:11:20 发布