简体   繁体   中英

Programmatically accessing camel property from camel context

The project I am working at the moment uses camel as the routing framework. When configuring camel context in spring we pass a property file that contains a bunch of global properties needed when configuring camel routes or for controlling run time behavior:

<camel:camelContext xmlns="http://camel.apache.org/schema/spring" id="my-id">
    <camel:propertyPlaceholder location="my-system.properties" id="global-properties"/>
    ...
</camel:camelContext>

and say my-system.properties has an entry like below:

my-system.properties
# Global properties that control my-system configuration and run time
...
foo={{bar}} 
...

When configuring the routes I can access foo property using the {{foo}} notation. It is also available to other beans using @PropertyInject annotation. However there is one use case in my design when a plain POJO not created by spring (an enum instead but this is not relevant) needs to access my foo property. Because this POJO it is passed the CamelContext as a method argument I find it natural to think I should be able to get the value of foo from there. However I spent a bit of time and could not figure out by myself how.

I know I can load the properties file again or even get the system property System.getProperty("bar") and everything will work but it looks like cheating to me.

There is an api on CamelContext to resolve property placeholders - its the resolvePropertyPlaceholders method:

If your POJO is not being managed by the SpringContext I don't see any way you can automatically inject the property. Although your approach may not seem the most fancy or elegant, it has the advantage of not giving you any overhead you could have by using another injection tool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM