您可以将自定义配置全局注册到组件,然后将其应用到所有freemarker端点。或者使用
configuration
URI参数。
将配置注册到组件:
FreemarkerComponent freemarkerComponent = getContext().getComponent("freemarker", FreemarkerComponent.class);
freemarkerComponent.setConfiguration(myConfiguration);
将配置注册到单个端点:
.to("freemarker:dummy?configuration=#freemarkerConfiguration") // freemarkerConfiguration is custom bean in registry
我创建了用于演示的单元测试:
public class CamelCustomFreemarkerLoader extends CamelTestSupport {
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
Configuration myConfiguration = new Configuration();
StringTemplateLoader dummyLoader = new StringTemplateLoader();
dummyLoader.putTemplate("dummy", "Hello from dummy template");
myConfiguration.setTemplateLoader(dummyLoader);
FreemarkerComponent freemarkerComponent = getContext().getComponent("freemarker", FreemarkerComponent.class);
freemarkerComponent.setConfiguration(myConfiguration);
from("direct:test")
.to("freemarker:dummy")
.to("mock:result");
}
};
}
@Test
public void testCustomTemplateLoader() throws Exception{
MockEndpoint mockEndpoint = getMockEndpoint("mock:result");
template.sendBody("direct:test",null);
mockEndpoint.setExpectedCount(1);
mockEndpoint.assertIsSatisfied();
Assert.assertEquals("Hello from dummy template", mockEndpoint.getExchanges().get(0).getIn().getBody());
}
}