264.25. 使用 @PropertyInject
从 Camel 2.12 开始提供
Camel 允许使用 @PropertyInject 注释(可以在字段和 setter 方法上设置)注入 POJO 中的属性占位符。
例如,您可以将该类与 RouteBuilder 类一起使用,如下所示:
public class MyRouteBuilder extends RouteBuilder {
@PropertyInject("hello")
private String greeting;
@Override
public void configure() throws Exception {
from("direct:start")
.transform().constant(greeting)
.to("{{result}}");
}
}
请注意,我们使用 @PropertyInject 标注了 greeting 字段,并将它定义为使用键 "hello "。然后,Camel 会使用这个键查找属性并注入其值,并转换为 String 类型。
您还可以在键中使用多个占位符和文本,例如,我们可以执行以下操作:
@PropertyInject("Hello {{name}} how are you?")
private String greeting;
这将使用它们 "名称" 键查找占位符。
如果键不存在,您还可以添加一个默认值,例如:
@PropertyInject(value = "myTimeout", defaultValue = "5000")
private int timeout;