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

package com.eg.egsc.scp.accesscontrolcomponent.mq.iotmq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.eg.egsc.scp.accesscontrolcomponent.SpringUtils;
import com.eg.egsc.scp.accesscontrolcomponent.handler.AbstractMessageHandler;
import com.eg.egsc.scp.accesscontrolcomponent.util.AnnoManageUtils;

@Component
public class ReceiverListenerIotbus {
  protected final Logger logger = LoggerFactory.getLogger(this.getClass());

  @RabbitListener(queues = "MSG_OUTBOUND_APP8075", containerFactory = "iotbusFactory")
  public void processMap(Message message) {
    String busMsg = new String(message.getBody());
    logger.info("recevie form gateway:{}", busMsg);
    JSONObject jsonObject = JSONObject.parseObject(busMsg);
    // 如果上传的事件类型不包含事件id,则不处理业务逻辑
    if (!jsonObject.containsKey("eventTypeID")) {
      return;
    }
    Integer eventTypeId = jsonObject.getInteger("eventTypeID");
    String beanName = AnnoManageUtils.getBeanNameByEventType(eventTypeId);
    // 接收的事件类型找不到对应的业务处理,不进行业务处理
    if (null == beanName) {
      return;
    }
    try {
      AbstractMessageHandler messageHandler = (AbstractMessageHandler) SpringUtils.getBean(beanName);
      // 注入对应的类进行业务处理
      messageHandler.doProcess(message);
    } catch (Exception e) {
      logger.error("接收mq信息出错{}",e);
    }
  }

}


自定义注解

@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Handler {
  String[] value() default "";
}
public final class AnnoManageUtils {
  public static Map<Integer, String> map = new ConcurrentHashMap<Integer, String>();

  static {
    //反射工具包,指明扫描路径
    Reflections reflections = new Reflections("com.eg.egsc.scp.accesscontrolcomponent.handler");
    //获取带Handler注解的类
    Set<Class<?>> classList = reflections.getTypesAnnotatedWith(Handler.class);
    for (Class classes : classList) {
      Handler t = (Handler) classes.getAnnotation(Handler.class);
      String[] valueList = t.value();
      //获取注解的值并循环
      for (String value : valueList) {
        //注解值为key,类名为value
        map.put(Integer.valueOf(value), CommonUtils.normalizeFirstWord(classes.getSimpleName()));
      }
    }
  }

  //通过eventTypeId,也就是注解的值获取相应处理Handler的类名
  public static String getBeanNameByEventType(Integer eventTypeId) {
    return map.get(eventTypeId);
  }
}

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

package com.eg.egsc.scp.accesscontrolcomponent;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtils implements ApplicationContextAware {
  private static ApplicationContext applicationContext = null;

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) {
    if (SpringUtils.applicationContext == null) {
      SpringUtils.applicationContext = applicationContext;
    }
  }

  // 获取applicationContext
  public static ApplicationContext getApplicationContext() {
    return applicationContext;
  }

  // 通过name获取 Bean.
  public static Object getBean(String name) {
    return getApplicationContext().getBean(name);

  }

  // 通过class获取Bean.
  public static <T> T getBean(Class<T> clazz) {
    return getApplicationContext().getBean(clazz);
  }

  // 通过name,以及Clazz返回指定的Bean
  public static <T> T getBean(String name, Class<T> clazz) {
    return getApplicationContext().getBean(name, clazz);
  }
}
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值