264.25. 使用 @PropertyInject
从 Camel 2.12 开始提供
Camel 允许使用 @PropertyInject
注释来注入 POJO 中的属性占位符,该注释可在字段和 setter 方法上设置。
例如,您可以将该类与 RouteBuilder
类搭配使用,如下所示:
注意我们使用 @PropertyInject
标注了问候字段,并定义它以使用键 "hello
"。然后,Camel 将使用此键查找属性并注入其值,并转换为 String 类型。
您也可以在键中使用多个占位符和文本,例如,我们可以实现:
@PropertyInject("Hello {{name}} how are you?") private String greeting;
@PropertyInject("Hello {{name}} how are you?")
private String greeting;
这将使用键 "name"
查找占位符。
如果键不存在,您还可以添加默认值,例如:
@PropertyInject(value = "myTimeout", defaultValue = "5000") private int timeout;
@PropertyInject(value = "myTimeout", defaultValue = "5000")
private int timeout;