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 map = new ConcurrentHashMap();

  static {
    //反射工具包,指明扫描路径
    Reflections reflections = new Reflections("com.eg.egsc.scp.accesscontrolcomponent.handler");
    //获取带Handler注解的类
    Set> 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 getBean(Class clazz) {
    return getApplicationContext().getBean(clazz);
  }

  // 通过name,以及Clazz返回指定的Bean
  public static  T getBean(String name, Class clazz) {
    return getApplicationContext().getBean(name, clazz);
  }
}

你可能感兴趣的:(spring)