配置类

属性类

1
2
3
4
5
6
7
8
@ConfigurationProperties(prefix = "init.admin")
@Data
public class AccountProperties {
private Boolean enable = false;
private String account;
private String username;
private String password;
}

主启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@EnableConfigurationProperties({AccountProperties.class})
@SpringBootApplication
@ComponentScan(basePackages = {"cc.yybk"})
@MapperScan(basePackages = {"cc.yybk.dao.mapper"})
public class WebStarter {
public static void main(String[] args) {
SpringApplication.run(WebStarter.class, args);
}

@InitBinder
public void initBinder(final WebDataBinder webdataBinder) {
webdataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(new Date(Long.valueOf(text)));
}
});
}
}

实现bean开关

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
   @Configuration
public class MqttConfig{

@Autowired
private MqttProperties mqttProperties;

@Autowired
private MqttService service;

@Bean
@ConditionalOnProperty(name = "cc.yybk.mqtt.enable", havingValue = "true", matchIfMissing = false)
public MqttPahoClientFactory mqttClientFactory() {
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
MqttConnectOptions options = factory.getConnectionOptions();
options.setServerURIs(new String[]{mqttProperties.getConnectUrl()});
options.setUserName(mqttProperties.getAccount());
options.setPassword(mqttProperties.getPassword().toCharArray());
options.setConnectionTimeout(10);
factory.setConnectionOptions(options);
return factory;
}

@Bean
@ConditionalOnProperty(name = "cc.yybk.mqtt.enable", havingValue = "true", matchIfMissing = false)
public MessageChannel mqttInputChannel() {
return new DirectChannel();
}

/**
* 入站消息处理器
* @return
*/
@Bean
@ServiceActivator(inputChannel = "mqttInputChannel")
@ConditionalOnProperty(name = "cc.yybk.mqtt.enable", havingValue = "true", matchIfMissing = false)
public MessageHandler handler() throws Exception {
return message -> {
String payload = (String) message.getPayload();
service.processAlarmMessage(payload);
};
}

/**
* 订阅主题
* @return
*/
@Bean
@ConditionalOnProperty(name = "cc.yybk.mqtt.enable", havingValue = "true", matchIfMissing = false)
public MessageProducer inbound() {
MqttPahoMessageDrivenChannelAdapter adapter =
new MqttPahoMessageDrivenChannelAdapter(mqttProperties.getClientId(), mqttClientFactory(), mqttProperties.getAlarmTopic());
adapter.setCompletionTimeout(5000);
adapter.setConverter(new DefaultPahoMessageConverter());
adapter.setQos(1);
adapter.setOutputChannel(mqttInputChannel());
return adapter;
}

/**
* 发布消息 如果你需要发布消息到MQTT broker,你可以创建一个出站消息处理器和相应的通道
* @return
*/
//
@Bean
@ConditionalOnProperty(name = "cc.yybk.mqtt.enable", havingValue = "true", matchIfMissing = false)
public MessageChannel mqttOutboundChannel() {
return new DirectChannel();
}

/**
* 出站消息处理器
* @return
*/
@Bean
@ServiceActivator(inputChannel = "mqttOutboundChannel")
@ConditionalOnProperty(name = "cc.yybk.mqtt.enable", havingValue = "true", matchIfMissing = false)
public MessageHandler mqttOutbound() {
MqttPahoMessageHandler messageHandler =
new MqttPahoMessageHandler("clientId", mqttClientFactory());
messageHandler.setAsync(true);
messageHandler.setDefaultTopic("Hello World!");
messageHandler.setDefaultQos(1);
return messageHandler;
}
}