2017-10-20 26 views
0

J'ai un SpringBootApplication qui est abonné au courtier MQTT. Les messages MQTT doivent être enregistrés dans la base de données, mais je ne peux pas accéder à mon service @Autowired.Spring - Accès au service @Autowired de AbstractMessageHandler

Exception je reçois:

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()); 
    } 
} 

Répondre

0

Application.java

@SpringBootApplication 
public class MQTTApiApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(MQTTApiApplication.class, args); 
    } 

    @Bean 
    public IntegrationFlow mqttinFlow(MqttMessageHandler handler) { 
     return IntegrationFlows 
     .from(mqttInbound()) 
     .handle(handler).get(); 
    } 
} 

MqqtMessageHandler.java

@Component 
public class MqttMessageHandler extends AbstractMessageHandler{ 

    @Autowired 
    private DeviceService deviceService; 

    @Override 
    protected void handleMessageInternal(String message) { 
     //... 
    } 

} 

DeviceService.java

@Service 
public class DeviceService { 

    @Autowired 
    private DeviceRepository repository; 

    //... 
} 

DeviceController.java

@RestController 
@RequestMapping("/") 
public class DeviceController { 

    @Autowired 
    private IntegrationFlow flow; 

    //... 
} 

DeviceRepository.java

@Repository 
public class DeviceRepository { 
    public void save() { 
     //... 
    } 
} 
+0

Oui, je l'ai ajouté un @component notation, mais obtenez toujours la même erreur. J'accède au service depuis RestController normalement. – mkdeki

+0

Et DeviceService? –

+0

Lorsque je mets DeviceService dans RestController, je peux accéder au DeviceService (qui est Autowired). Le problème est que je ne peux pas accéder aux Services/Contrôleurs Autowired à partir de MqttMessageHandler. – mkdeki