我有一个 spring boot 应用程序,它只是将消息发送到 kafka 主题。代码看起来像这样。
@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");
}
}
此应用程序作为独立应用程序运行良好。当我从中创建一个 jar 并将其包含到另一个 Spring Boot 应用程序中,以便我可以使用生产者向主题发送消息并运行 Spring Boot 应用程序时,我得到以下异常:
无法实例化 SpringCLoudStreamClient.,原因是
:没有为依赖项找到类型 [com.comcast.activation.message.interfaces.MessageChannels] 的合格 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。
我得到以下代码行的异常
@Autowired SpringCloudStreamClient producer;
我使用了组件扫描来确保 jar 中的包被父 Spring Boot 应用程序扫描。这样做之后,我得到了上述异常。启用绑定注释没有做它应该做的事情,即在这个场景中创建一个消息通道接口的实现。这是一个错误还是我错过了什么?