2017-07-07 62 views
3

我是一個新手,在spring中使用駝鹿2.17。我有從web服務接收的錯誤代碼的處理器和我配置在屬性文件中的錯誤描述等如何在apache + spring中動態加載屬性文件的值

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); 

我怎樣才能實現這個設施的錯誤代碼在我的處理器類讀取屬性。在此先感謝

回答

2

你可以做一個可用MessageSource在你的情況下,

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

然後使用這個在您的處理器獲取像適當的消息:

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

您好,感謝您的快速響應。你的解決方案工作正常。但問題是我需要添加屬性文件在類路徑和生產中我需要配置我的屬性文件的類路徑之外。我找不到在我的類路徑之外指定文件路徑的方法。沒有方法在** ResourceBundleMessageSource **中設置文件名。 – Shameer

+0

我想你可以用戶ReloadableResourceBundleMessageSource,也許你可以找到更多的信息在它的用法[這裏](https://stackoverflow.com/questions/9035588/dynamically-load-files-on-classpath-using-reloadableresourcebundlemessagesource) –

+0

嗨, @Panayiotis Poularakis感謝您的回放......運作良好..設置基本名稱作爲我的屬性文件路徑+屬性名...標記爲接受的答案... – Shameer

相關問題