5
 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{}

我想在路由中注入 Shape 实现test(Demo,xxx)

  1. setHeader() 可以帮助在路由中添加一个 Shape 实现。
  2. 除了在骆驼路线中设置标头之外,是否还有其他选择,因为它有其优点和缺点

在 Camel Exchange 中设置 Lot of headers 的优点和缺点

4

1 回答 1

1

这是绕过 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
    }

在您的 Route 配置类中,配置如下:

@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();
    }
}
于 2018-10-25T21:10:37.190 回答