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



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

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

但是addComponent((函数需要Component类型参数,而不是PropertiesComponent,即使PropertiesComponents扩展了DefaultComponent类。我已经将这个依赖项添加到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:不兼容的类型:org.apache.camel.component.properties。properties组件无法转换为org.apache.camil.component

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

PropertiesComponent是一个非常特殊的组件,因此在Camel上下文中有setPropertiesComponentgetPropertiesComponent()等专用方法来管理它,您应该使用它们。

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

// 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")

最新更新