引入其他类定义的静态变量
1. 引入方式
import static 类路径.*;
2. 示例
2.1 需要被导入的类
package com.streamax.s17.mcs.business.message;import javax.annotation.Resource; import java.util.Date; import java.util.Map; /** * @Describe: * @Author: chenfan * @Date: 2019/4/29 13:26 */ @Slf4j @Component public class MqttMessageListener implements IMqttMessageListener { public static final String TRANSFER_BEGIN = "s17/media/handout/op_session_client_pull_begin"; public static final String TRANSFER_END = "s17/media/handout/op_session_client_pull_end"; public static final String TRANSFER_CONTROL = "s17/media/op_session_control"; }
2.2 需要导入的类
package com.streamax.s17.mcs.business.message; import lombok.extern.slf4j.Slf4j; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import static com.streamax.s17.mcs.business.message.MqttMessageListener.*; /** * @Describe: * @Author: chenfan * @Date: 2019/4/26 15:17 */ @Slf4j @Component public class MessageHandler { private MqttMessageListener listener; private MqttClient mqttClient; public static final String filter = "/#"; @Autowired public MessageHandler(MqttClient mqttClient,MqttMessageListener listener ) { this.mqttClient = mqttClient; this.listener = listener; init(); } /** * 订阅关心事件 */ public void init(){ try {
// 导入后即可通过变量名直接使用,不需要通过类名引用变量 mqttClient.subscribe(TRANSFER_BEGIN + filter, 1, listener); mqttClient.subscribe(TRANSFER_END + filter, 1, listener); mqttClient.subscribe(TRANSFER_CONTROL + filter, 1, listener); log.info("subscribe topics success"); } catch (MqttException e) { log.error("subscribe topics occur a exception:{ }",e); } } }
如果文章对您有所帮助,可以点一下推荐