0
我有一個訂閱了MQTT代理的SpringBootApplication。 MQTT消息需要保存到數據庫,但我無法訪問我的@Autowired服務。從AbstractMessageHandler獲取SpringAccess @Autowired服務
例外,我得到:
Field deviceService in com.example.MqttMessageHandler required a bean of type 'com.example.service.DeviceService' that could not be found.
MQTTApiApplication.java
@SpringBootApplication(scanBasePackages = "{com.example}")
public class MQTTApiApplication {
public static void main(String[] args) {
SpringApplicationBuilder(MQTTApiApplication.class)
.web(false).run(args);
}
@Bean
public IntegrationFlow mqttInFlow() {
return IntegrationFlows.from(mqttInbound())
.handle(new MqttMessageHandler())
.get();
}
}
MqttMessageHandler.java
public class MqttMessageHandler extends AbstractMessageHandler {
@Autowired
DeviceService deviceService;
@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
deviceService.saveDevice(new Device());
}
}
是的,我已經添加@Component的符號,但仍然得到相同的錯誤。我通常從RestController訪問服務。 – mkdeki
那麼DeviceService呢? –
當我將DeviceService放入RestController時,我可以訪問DeviceService(這是Autowired)。 問題是我無法從MqttMessageHandler訪問Autowired服務/控制器。 – mkdeki