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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2017-10-25 一脸懵逼学习HBase的搭建(注意HBase的版本)
2017-10-25 一脸懵逼学习HBase---基于HDFS实现的。(Hadoop的数据库,分布式的,大数据量的,随机的,实时的,非关系型数据库)