package com.xjrsoft.common.mqtt;

import org.eclipse.paho.mqttv5.client.IMqttMessageListener;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 获取所有实现了IMqttMessageListener接口的类
 */
@Component
public class MqttServiceLocator implements ApplicationContextAware {

    public Map<String, IMqttMessageListener> getMap() {
        return map;
    }

    /**
     * 用于保存接口实现类名及对应的类
     */
    private Map<String, IMqttMessageListener> map;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //根据接口类型返回相应的所有bean
        map = applicationContext.getBeansOfType(IMqttMessageListener.class);
    }

    /**
     * 获取对应服务
     * @param key
     * @return
     */
    public IMqttMessageListener getService(String key) {
        return map.get(key);
    }
}
