Skip to content
Advertisement

How to add Camel properties component to camel context?

I’m currently trying to add properties component with location set to my properties file to use properties placeholders in my project:

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

But addComponent() function expects Component type argument, not PropertiesComponent even though PropertiesComponent extends the DefaultComponent class. I’ve added this dependency to pom.xml to use it:

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

and also added the resources tag:

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

The error I get looks like this:

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

I have no idea what causes it, please help. Thanks.

Advertisement

Answer

The PropertiesComponent is a very special component thus there are dedicated methods like setPropertiesComponent and getPropertiesComponent() in the Camel context to manage it that you should use instead.

Your code should rather be something like the following code:

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

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

Or simply context.getPropertiesComponent().setLocation("classpath:properties.properties")

Advertisement