from("direct:myRoute1")
.bean(new DemoRoute(), "test(Demo,xxx)")
.end();
from("direct:myRoute2")
.bean(new DemoRoute(), "test(Demo,xxx)")
.end();
public interface Shape
@Component
class Circle implements Shape{
}
@Component
class Square implements Shape{}
我想在路由 test(Demo,xxx)
中注入(inject) Shape 实现
- setHeader() 能否帮助在路由中添加 Shape 实现。
- 除了在 Camel route 设置 header 之外,还有其他选择吗,因为它有其优点和缺点
最佳答案
这是一个绕过 Camel 的解决方案:
由于您是自己实例化 bean 而不是依赖 spring 来管理它,因此您可以通过构造函数传递 Shape 实现。
在您的 DemoRoute 类中添加一个 Shape 字段:
public class DemoRoute {
private final Shape shape;
public DemoRoute(Shape shape) {
this.shape = shape;
}
// test method that uses shape
}
然后在您的路由配置类中,配置如下:
@Component
public class CustomRoute extends RouteBuilder {
private final Square square;
private final Circle circle;
CustomRoute(Square square, Circle circle){
this.square = square;
this.circle = circle;
}
@Override
public void configure() throws Exception {
from("direct:myRoute1")
.bean(new DemoRoute(circle), "test(Demo,xxx)")
.end();
from("direct:myRoute2")
.bean(new DemoRoute(square), "test(Demo,xxx)")
.end();
}
}
关于java - 使用 bean ref 在 Camel route 设置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52909179/