104.5. 将 Spring PropertyPlaceholderConfigurer 与 File 组件一起使用
在 Camel 中,您可以直接从 简单 语言中使用文件语言,这样一来,在 Spring XML 中可以更轻松地使用基于文件扩展的操作,我们可以根据文件扩展进行路由,如下所示: ???
<from uri="file://input/orders"/>
<choice>
<when>
<simple>${file:ext} == 'txt'</simple>
<to uri="bean:orderService?method=handleTextFiles"/>
</when>
<when>
<simple>${file:ext} == 'xml'</simple>
<to uri="bean:orderService?method=handleXmlFiles"/>
</when>
<otherwise>
<to uri="bean:orderService?method=handleOtherFiles"/>
</otherwise>
</choice>
如果您使用 File 端点上的 fileName 选项使用文件语言设置动态文件名,那么请确定您
使用其他语法(从 Camel 2.5 开始提供),以避免使用 Springs PropertyPlaceholderConfigurer 进行冲突。???
bundle-context.xml
<bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:bundle-context.cfg" />
</bean>
<bean id="sampleRoute" class="SampleRoute">
<property name="fromEndpoint" value="${fromEndpoint}" />
<property name="toEndpoint" value="${toEndpoint}" />
</bean>bundle-context.cfg
fromEndpoint=activemq:queue:test
toEndpoint=file://fileRoute/out?fileName=test-$simple{date:now:yyyyMMdd}.txt
请注意,我们在上面的 toEndpoint 中使用 $simple\{ } 语法。
如果您没有这样做,则存在一个冲突,Spring 会抛出异常,如下所示
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'sampleRoute' defined in class path resource [bundle-context.xml]: Could not resolve placeholder 'date:now:yyyyMMdd'