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

 

posted on 2020-10-25 16:38  别先生  阅读(6664)  评论(0编辑  收藏  举报