我有一个简单的 MessageProducer
类,它使用 Camel 的 ProducerTemplate
将消息发送到直接 channel
这是代码
@Component
@Slf4j
public class MessageProducer {
@EndpointInject(uri = "direct:app.out")
protected ProducerTemplate template;
public void sendEvent(Object payload, String eventName) {
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("eventName", eventName);
template.sendBodyAndHeaders(payload, headers);
log.debug("Sent message {}", payload);
}
}
当我调试它时,我发现模板在 web 应用程序中运行时为 null,但它在 spring Junit 测试中有效。
我不明白出了什么问题。
最佳答案
是的,Spring bean 后处理器需要启动,让 IoC 运行。 Camel 测试套件自动执行此操作,因此它可以在单元测试中工作。
关于java - Camel 和 spring 忽略 @Produce 和 @EndpointInject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11505121/