简体   繁体   English

如何在apache camel + spring中动态加载属性文件值

[英]How to load property file values dynamically in apache camel + spring

I am a newbie using camel 2.17 with spring. 我是春天使用骆驼2.17的新手。 I have a processor which receives an error code from a web service and I configured the error description in the property file like 我有一个处理器从Web服务接收错误代码,我在属性文件中配置了错误描述,如

  myproject.errorCode=1001:error1 description, 1002:error2 description, 1003:....

Currently I am reading the value myproject.errorCode using property injection and parsing all the errorCode with description which is working fine. 目前我正在使用属性注入读取值myproject.errorCode并解析所有带有描述的errorCode工作正常。 But the error code list is very huge and maintaining it in a single property is very difficult. 但是错误代码列表非常庞大并且将其保存在单个属性中非常困难。 So I want to split the properties like 所以我想分割像这样的属性

   myproject.errorCode.1001=error1 description
   myproject.errorCode.1002=error2 description
   myproject.errorCode.1003=error3 description
   .....

and I want to read the property in my processor class according to the error code received from the web service like 我想根据从Web服务收到的错误代码读取我的处理器类中的属性

  String errorCodeRecieved  = myWebService.getErrorCode();
  String errorString = "myproject.errorCode.";
  String errorDescription = something.getProperty(errorString + errorCodeRecieved);

How can I achieve this facility. 我怎样才能实现这一目标。 Thanks in advance 提前致谢

you can make a MessageSource available in your context, 您可以在上下文中使用MessageSource

@Bean
MessageSource myMessageSource() {
    ResourceBundleMessageSource r = new ResourceBundleMessageSource();
    r.setBasenames("/messages/sample");
    r.setDefaultEncoding("UTF-8");
    return r;
}

And then use this in your processor to fetch the appropriate message like: 然后在处理器中使用它来获取相应的消息,如:

messageSource.getMessage(code, null, null, locale)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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