Springboot使用MessageSource讀取資源文件


1、項目開發過程中的提示文字信息可以在資源文件中進行定義,而且資源文件是實現國際化技術的主要手段。如果想在SpringBoot里面進行資源文件的配置,只需要做一些簡單的application.yml配置即可,而且所有注入的資源文件都可以像最初的Spring處理那樣,直接使用MessageSource進行讀取。

首先,在src/main/resources源文件夾下創建一個i18n的子目錄(包),然后創建src/main/resources/i18n/Messages.properties文件,然后輸入自己的提示信息。

1 springboot.url=www.bie.com
2 springboot.msg=歡迎{0}光臨!

然后,修改application.yml配置文件,追加資源文件配置,如下所示:

1 server.port=8081
2 
3 # 定義資源文件,多個資源文件使用逗號進行分割
4 spring.messages.basename=i18n/Messages 

項目結構,如下所示:

編寫測試程序,如下所示:

 1 package org.springboot.tentent.controller;
 2 
 3 import java.util.HashMap;
 4 import java.util.Locale;
 5 import java.util.Map;
 6 
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.context.MessageSource;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RestController;
11 
12 @RestController
13 public class SampleController {
14 
15     // 利用該對象實現資源文件的讀取
16     @Autowired
17     private MessageSource messageSource;
18 
19     @RequestMapping(value = "/message")
20     public Map<String, String> message() {
21         Map<String, String> map = new HashMap<String, String>();
22         // 當程序中配置了資源文件之后,就可以通過MessageSource接口中提供的getMessage()方法進行資源的讀取
23         map.put("springboot.url:", this.messageSource.getMessage("springboot.url", null, Locale.getDefault()));
24         map.put("springboot.msg:",
25                 this.messageSource.getMessage("springboot.msg", new Object[] { "哈哈哈" }, Locale.getDefault()));
26         return map;
27     }
28 
29 }

運行效果,如下所示:

 

2、可以使用此機制實現國際化開發,當程序可以實現資源文件讀取的時候,就意味着可以實現國際化開發處理了。可以發現,MessageSource接口中的getMessage()方法里面需要接收一個Locale類的對象,此時就可以通過Locale類的設置來獲取不同的資源文件。當然,也需要在項目中配置好不同語言的資源文件。例如,本程序在src/main/resources/i18n目錄中又創建了Messages_zh_CN.properties和Messages_en_US.properties(注意baseName的名稱相同)。

1 springboot.url=www.bie.com
2 springboot.msg=歡迎{0}光臨!
1 springboot.url=www.bie.com
2 springboot.msg=welcome to {0} here!

項目結構,如下所示:

測試案例,如下所示:

 1 package org.springboot.tentent.controller;
 2 
 3 import java.util.HashMap;
 4 import java.util.Locale;
 5 import java.util.Map;
 6 
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.context.MessageSource;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RestController;
11 
12 @RestController
13 public class SampleController {
14 
15     // 利用該對象實現資源文件的讀取
16     @Autowired
17     private MessageSource messageSource;
18 
19     @RequestMapping(value = "/message")
20     public Map<String, String> message() {
21         Map<String, String> map = new HashMap<String, String>();
22         // 當程序中配置了資源文件之后,就可以通過MessageSource接口中提供的getMessage()方法進行資源的讀取
23         map.put("springboot.url", this.messageSource.getMessage("springboot.url", null, Locale.getDefault()));
24         map.put("springboot.msg",
25                 this.messageSource.getMessage("springboot.msg", new Object[] { "哈哈哈" }, new Locale("en", "US")));
26         
27         System.out.println(map.get("springboot.msg"));
28         // 采用不同的Locale對象實現指定語言的資源讀取
29         map.put("springboot.msg",
30                 this.messageSource.getMessage("springboot.msg", new Object[] { "哈哈哈" }, new Locale("zh", "CN")));
31         
32         System.out.println(map.get("springboot.msg"));
33         return map;
34     }
35 
36 }

注意:即使提供了不同語言的資源文件,在SpringBoot中也依然需要提供Messages.properties配置文件,否則將無法實現資源文件的讀取。

1 server.port=8081
2 
3 # 定義資源文件,多個資源文件使用逗號進行分割
4 spring.messages.basename=i18n/Messages,i18n/Messages_en_US,i18n/Messages_zh_CN

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM