2017-10-20 4 views
0

私は、MQTTブローカを購読するSpringBootApplicationを持っています。 MQTTメッセージはDatabaseに保存する必要がありますが、@ Autouiredサービスにはアクセスできません。Springアクセス@ AbstractMessageHandlerの自動サービス

私が手例外:

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

答えて

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

はい、私は@Component追加しました表記法では、同じエラーが発生します。私は通常RestControllerからサービスにアクセスしています。 – mkdeki

+0

DeviceServiceはどうですか? –

+0

DeviceServiceをRestControllerに入れると、DeviceService(Autowired)にアクセスできます。 問題は、MqttMessageHandlerからAutowired Services/Controllersにアクセスできないことです。 – mkdeki