我的用例是我有一个路由,我想通过一个可选属性配置autoStartup。我以为我可以使用@PropertyInject批注来获取此属性,如果尚未设置该属性,则使用默认值。例如:
@PropertyInject(value = "message.import.item.autoStartup", defaultValue = "true")
private String autoStartup;
public void configure() throws Exception {
from("{{message.import.item}}?clientId=2&durableSubscriptionName=ItemSubscriber")
.to("{{message.import.source}}")
.autoStartup(autoStartup);
}
我在我的camel上下文中创建了一个属性占位符,其中包含两个属性文件,一个在类路径中,它始终存在于打包的jar文件中,第二个是我想要成为可选的外部文件:
<camelContext streamCache="true" xmlns="http://camel.apache.org/schema/spring">
<propertyPlaceholder id="properties" location="file:${camel.subscribers}/subscriber.properties,classpath:integration.properties" ignoreMissingLocation="true"/>
<contextScan/>
</camelContext>
只要我将camel.subscribers的值作为JVM属性提供,就可以按预期工作。如果subscriber.properties文件不存在,则通过@PropertyInject批注加载默认值,如果它存在且包含相应的键,则从属性文件加载该值。
但是我有一个问题:请记住,我希望这个属性文件是可选的。文件本身的存在确实是可选的,但是这个设置要求我为camel.subscribers提供一个值。除非我这样做,否则我会收到IllegalArgumentException:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'camel.subscribers' in string value "file:${camel.subscribers}/subscriber.properties,classpath:integration.properties"
这种方法违背了将属性作为可选属性的全部目的,因为它迫使我为camel.subscribers提供一个值,只是为了能够发现文件subscriber.properties不存在。
我接近这个错误的方式吗?关于如何解决这个问题的其他想法?
答案 0 :(得分:-1)
你应该只能设置ignoreMissingLocation =&#34; true&#34;在属性占位符中。
<camelContext streamCache="true" xmlns="http://camel.apache.org/schema/spring">
<propertyPlaceholder id="properties" ignoreMissingLocation="true" location="file:${camel.subscribers}/subscriber.properties,classpath:integration.properties" ignoreMissingLocation="true"/>
<contextScan/>
</camelContext>