我有一個只需將消息發送到kafka主題的spring啓動應用程序。代碼看起來像這樣。沒有發現依賴關係的[com.comcast.activation.message.interfaces.MessageChannels]類型的合格bean:
@RestController
@RequestMapping("/ActivationQueueService")
public class ActivationQueueController {
private static final Logger LOGGER = LoggerFactory
.getLogger(ActivationQueueController.class);
@Autowired
SpringCloudStreamClient producer;
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new ActivationDataInfoValidator());
}
@RequestMapping(method = RequestMethod.POST, value = "/sendMessage", headers = "Accept=application/json", produces = "application/json")
public void sendMessage(@RequestBody ActivationDataInfo message)
throws JsonProcessingException {
LOGGER.debug("Activation Data Request Recieved : " + message.toString());
if (message != null) {
ObjectMapper mapper = new ObjectMapper();
producer.sendMessagetoKafka(message);
LOGGER.info("Activation Data Request sent to Kafka : " + message);
}
}
}
接口:
public interface MessageChannels {
@Output("activationMsgQueue")
MessageChannel save();
}
監製:
@Service
@EnableBinding(MessageChannels.class)
public class SpringCloudStreamClient {
private static final Logger LOGGER = LoggerFactory
.getLogger(SpringCloudStreamClient.class);
@Autowired MessageChannels msgChannel;
public Object sendMessagetoKafka(ActivationDataInfo msg){
LOGGER.info("Sending Message : " + msg);
msgChannel.save().send(MessageBuilder.withPayload(msg).build());
return new String("Success");
}
}
此應用程序運行正常作爲獨立的應用程序。當我創建一個罐子出來,並將其包含到另一個春季啓動應用程序,以便我可以使用生產者發送消息到主題並運行春季啓動應用程序我得到以下異常:
無法實例化SpringCLoudStreamClient。,reason
:找不到符合條件的[com.comcast.activation.message.interfaces.MessageChannels]類型的符合條件的bean:期望至少1個符合此依賴關係自動裝配候選資格的bean。
我得到了下面的一行代碼
@Autowired SpringCloudStreamClient producer;
我已經使用組件掃描,以確保在JAR包由父春天啓動的應用程序掃描上面的異常。這樣做後,我得到了上述例外。啓用綁定註釋並沒有做它應該做的事情,那就是在這個場景中創建一個消息通道接口的實現。這是一個錯誤還是我錯過了什麼?
你是怎麼製作罐子的?你的依賴是什麼? – Ulises
執行gradle build會生成一個彈簧啓動應用程序的jar。 –