【问题标题】:How to load property file values dynamically in apache camel + spring如何在apache camel + spring中动态加载属性文件值
【发布时间】:2017-12-11 09:09:03
【问题描述】:

我是使用 spring 骆驼 2.17 的新手。我有一个从 Web 服务接收错误代码的处理器,我在属性文件中配置了错误描述,例如

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

目前,我正在使用属性注入读取值 myproject.errorCode 并使用运行正常的描述解析所有 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);

我怎样才能实现这个功能。提前致谢

【问题讨论】:

    标签: java spring properties apache-camel


    【解决方案1】:

    您可以在您的上下文中提供MessageSource,

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

    然后在您的处理器中使用它来获取适当的消息,例如:

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

    【讨论】:

    • 您好,感谢您的快速回复。您的解决方案运行良好。但问题是我需要在类路径中添加属性文件,而在生产中我需要在类路径之外配置我的属性文件。我找不到在我的类路径之外指定文件路径的方法。 ResourceBundleMessageSource 中没有设置文件名的方法。
    • 我认为你可以使用 ReloadableResourceBundleMessageSource,也许你可以找到更多关于它的使用信息here
    • 嗨,@Panayiotis Poularakis 感谢您的重播...运行良好..将基本名称设置为我的属性文件路径+属性名...标记为已接受答案...
    猜你喜欢
    • 1970-01-01
    • 2012-07-08
    • 2023-03-31
    • 1970-01-01
    • 2017-09-17
    • 2012-03-23
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    相关资源
    最近更新 更多