搜索

264.25. 使用 @PropertyInject

download PDF

从 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;
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.