如何将骆驼属性组件添加到骆驼上下文中?

How to add Camel properties component to camel context?

我目前正在尝试将位置设置为属性文件的属性组件添加到我的属性文件中,以便在我的项目中使用属性占位符:

PropertiesComponent pc = new PropertiesComponent();
pc.setLocation("classpath:properties.properties");
context.addComponent("properties", pc);

但是 addComponent() 函数需要 Component 类型参数,而不是 PropertiesComponent,即使 PropertiesComponent 扩展了 DefaultComponent class。我已将此依赖项添加到 pom.xml 以使用它:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-properties</artifactId>
    <version>3.0.0-M4</version>
</dependency>

并且还添加了资源标签:

<build>
    <resources>
        <resource>
            <directory>${project.basedir}/src/main/resources</directory>
        </resource>
    </resources>
    ...
</build>

我收到的错误如下所示:

java: incompatible types: org.apache.camel.component.properties.PropertiesComponent cannot be converted to org.apache.camel.Component

我不知道是什么原因造成的,请帮忙。 谢谢

PropertiesComponent 是一个非常特殊的组件,因此在 Camel 上下文中有像 setPropertiesComponent and getPropertiesComponent() 这样的专用方法来管理它,您应该改用它。

您的代码应该类似于以下代码:

// create the properties component
PropertiesComponent pc = new PropertiesComponent();
pc.setLocation("classpath:properties.properties");

// add properties component to camel context
context.setPropertiesComponent(pc);

或者干脆 context.getPropertiesComponent().setLocation("classpath:properties.properties")