【发布时间】:2016-06-15 21:56:59
【问题描述】:
我有一个简单的向 kafka 主题发送消息的 Spring Boot 应用程序。代码如下所示。
@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 应用程序扫描。这样做之后,我得到了上述异常。启用绑定注释并没有做它应该做的事情,即在这种情况下创建消息通道接口的实现。这是一个错误还是我错过了什么?
【问题讨论】:
-
你是如何创建罐子的?你的依赖是什么?
-
进行 gradle 构建会生成一个 spring boot 应用程序的 jar。
标签: java spring apache-kafka spring-cloud-stream