我是使用 spring 的 camel 2.17 的新手。我有一个从网络服务接收错误代码的处理器,我在属性文件中配置了错误描述,如
myproject.errorCode=1001:error1 description, 1002:error2 description, 1003:....
目前我正在使用属性注入(inject)读取值 myproject.errorCode 并解析所有带有描述的错误代码,它工作正常。但是错误代码列表非常庞大,很难在单个属性中维护它。所以我想像这样拆分属性
myproject.errorCode.1001=error1 description
myproject.errorCode.1002=error2 description
myproject.errorCode.1003=error3 description
.....
我想根据从 Web 服务收到的错误代码读取处理器类中的属性,例如
String errorCodeRecieved = myWebService.getErrorCode();
String errorString = "myproject.errorCode.";
String errorDescription = something.getProperty(errorString + errorCodeRecieved);
我怎样才能实现这个功能。提前致谢
最佳答案
你可以制作一个MessageSource在您的上下文中可用,
@Bean
MessageSource myMessageSource() {
ResourceBundleMessageSource r = new ResourceBundleMessageSource();
r.setBasenames("/messages/sample");
r.setDefaultEncoding("UTF-8");
return r;
}
然后在您的处理器中使用它来获取适当的消息,例如:
messageSource.getMessage(code, null, null, locale)
关于java - 如何在 apache camel + spring 中动态加载属性文件值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44964497/