spring boot 中 自定义注解,并通过注解反射获取类实例

深碍√TFBOYSˉ_ 2022-06-02 09:58 1300阅读 0赞
  1. package com.eg.egsc.scp.accesscontrolcomponent.mq.iotmq;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.amqp.core.Message;
  5. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  6. import org.springframework.stereotype.Component;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.eg.egsc.scp.accesscontrolcomponent.SpringUtils;
  9. import com.eg.egsc.scp.accesscontrolcomponent.handler.AbstractMessageHandler;
  10. import com.eg.egsc.scp.accesscontrolcomponent.util.AnnoManageUtils;
  11. @Component
  12. public class ReceiverListenerIotbus {
  13. protected final Logger logger = LoggerFactory.getLogger(this.getClass());
  14. @RabbitListener(queues = "MSG_OUTBOUND_APP8075", containerFactory = "iotbusFactory")
  15. public void processMap(Message message) {
  16. String busMsg = new String(message.getBody());
  17. logger.info("recevie form gateway:{}", busMsg);
  18. JSONObject jsonObject = JSONObject.parseObject(busMsg);
  19. // 如果上传的事件类型不包含事件id,则不处理业务逻辑
  20. if (!jsonObject.containsKey("eventTypeID")) {
  21. return;
  22. }
  23. Integer eventTypeId = jsonObject.getInteger("eventTypeID");
  24. String beanName = AnnoManageUtils.getBeanNameByEventType(eventTypeId);
  25. // 接收的事件类型找不到对应的业务处理,不进行业务处理
  26. if (null == beanName) {
  27. return;
  28. }
  29. try {
  30. AbstractMessageHandler messageHandler = (AbstractMessageHandler) SpringUtils.getBean(beanName);
  31. // 注入对应的类进行业务处理
  32. messageHandler.doProcess(message);
  33. } catch (Exception e) {
  34. logger.error("接收mq信息出错{}",e);
  35. }
  36. }
  37. }

自定义注解

  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Documented
  3. public @interface Handler {
  4. String[] value() default "";
  5. }
  6. public final class AnnoManageUtils {
  7. public static Map<Integer, String> map = new ConcurrentHashMap<Integer, String>();
  8. static {
  9. //反射工具包,指明扫描路径
  10. Reflections reflections = new Reflections("com.eg.egsc.scp.accesscontrolcomponent.handler");
  11. //获取带Handler注解的类
  12. Set<Class<?>> classList = reflections.getTypesAnnotatedWith(Handler.class);
  13. for (Class classes : classList) {
  14. Handler t = (Handler) classes.getAnnotation(Handler.class);
  15. String[] valueList = t.value();
  16. //获取注解的值并循环
  17. for (String value : valueList) {
  18. //注解值为key,类名为value
  19. map.put(Integer.valueOf(value), CommonUtils.normalizeFirstWord(classes.getSimpleName()));
  20. }
  21. }
  22. }
  23. //通过eventTypeId,也就是注解的值获取相应处理Handler的类名
  24. public static String getBeanNameByEventType(Integer eventTypeId) {
  25. return map.get(eventTypeId);
  26. }
  27. }

AnnoManageUtils通过Reflections反射工具包,获得所有添加handler注解的类。并存储在ConcurrentHashMap中,key为注解handler的值。这样就可以通过handler的值获取到handler注解的类名。再通过类名获取类注入到容器中的bean。

  1. package com.eg.egsc.scp.accesscontrolcomponent;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.ApplicationContextAware;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. public class SpringUtils implements ApplicationContextAware {
  7. private static ApplicationContext applicationContext = null;
  8. @Override
  9. public void setApplicationContext(ApplicationContext applicationContext) {
  10. if (SpringUtils.applicationContext == null) {
  11. SpringUtils.applicationContext = applicationContext;
  12. }
  13. }
  14. // 获取applicationContext
  15. public static ApplicationContext getApplicationContext() {
  16. return applicationContext;
  17. }
  18. // 通过name获取 Bean.
  19. public static Object getBean(String name) {
  20. return getApplicationContext().getBean(name);
  21. }
  22. // 通过class获取Bean.
  23. public static <T> T getBean(Class<T> clazz) {
  24. return getApplicationContext().getBean(clazz);
  25. }
  26. // 通过name,以及Clazz返回指定的Bean
  27. public static <T> T getBean(String name, Class<T> clazz) {
  28. return getApplicationContext().getBean(name, clazz);
  29. }
  30. }

发表评论

表情:
评论列表 (有 0 条评论,1300人围观)

还没有评论,来说两句吧...

相关阅读