news 2026/5/1 9:58:06

SpringBoot读取properties中文乱码解决方案

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SpringBoot读取properties中文乱码解决方案

目录

一、问题描述

二、解决方案

2.1、网络上的解决办法

2.1.1、修改IDEA编码?

2.1.2、改为yml配置

2.1.3、读取时设置编码

2.2、重写资源加载类(个人推荐)


一、问题描述

由于业务需求需要在application.properties中配置一个带有中文字符串的参数,注入到业务类中,但是发现注入的中文是乱码的。大概情况如下所示:

package com.cnstar.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; /** * cnstar单元测试 * @author cnstar **/ @SpringBootTest(classes = TestApplication.class) @RunWith(SpringRunner.class) public class CnstarTest { @Value("${name}") private String name; @Test public void test1() { System.out.println("中文内容:" + name); } }

打印输出结果:

二、解决方案

2.1、网络上的解决办法

遇到问题首先想到网络上找解决方案,网络上的解决办法基本一致,概括为以下三种。

2.1.1、修改IDEA编码

在IDEA中将所有的编码设置为UTF-8,同时勾上Transparent native-to-ascii conversion的选项,然后重新创建application.properties的文件。

运行结果:

但是这个配置文件用记事本打开编辑时,发现内容被修改成了unicode编码,在线上修改时变得很困难,所以此方式我不做推荐。

2.1.2、改为yml配置

就是将application.properties的文件修改为application.yml的结构,重启项目。

运行效果

证明是可行的。这种方式可以根据自己需求选择,但是当配置文件的内容层级较深时也不推荐,容易看错行配置。

2.1.3、读取时设置编码
package com.cnstar.test.property; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import javax.annotation.PostConstruct; @Configuration @PropertySource(value = "classpath:application.properties", encoding = "utf-8") public class CnstarProperty { @Value("${name}") private String name; @PostConstruct public void init() { System.out.println("name is :" + name); } }

亲测发现这种方式针对application.properties是不行的。

但是针对其他名称的properties文件是可以的

package com.cnstar.test.property; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import javax.annotation.PostConstruct; @Configuration @PropertySource(value = "classpath:test.properties", encoding = "utf-8") public class CnstarProperty { @Value("${name2}") private String name; @PostConstruct public void init() { System.out.println("name is :" + name); } }

运行结果:

2.2、重写资源加载类(个人推荐)

1、创建一个类继承PropertiesPropertySourceLoader,因SpringBoot版本不同PropertiesPropertySourceLoader****类会有差别,本文采用的SpringBoot版本是2.3.12.RELEASE。

package com.cnstar.utils; import org.springframework.core.io.*; import org.springframework.core.env.*; import org.springframework.boot.env.*; import java.util.*; import java.io.*; /** * 解快springBoot读取properties配置文件中文乱码的问题 * @author cnstar **/ public class SelfPropertySourceLoader extends PropertiesPropertySourceLoader { @Override public List<PropertySource<?>> load(String name, Resource resource) throws IOException { Map<String, ?> properties = this.loadUseUtf8(resource); if (properties.isEmpty()) { return Collections.emptyList(); } return Collections.singletonList(new OriginTrackedMapPropertySource(name, Collections.unmodifiableMap((Map<?, ?>)properties), true)); } private Map<String, ?> loadUseUtf8(Resource resource) throws IOException { Properties props = new Properties(); InputStream is = resource.getInputStream(); try { String filename = resource.getFilename(); if (filename != null && filename.endsWith(".xml")) { props.loadFromXML(is); } else { props.load(new InputStreamReader(is, "utf-8")); } } finally { is.close(); } return (Map)props; } }

2.在resource目录下创建目录META-INF,在META-INF目录下创建文件spring.factories

内容如下:

org.springframework.boot.env.PropertySourceLoader=com.cnstar.utils.SelfPropertySourceLoader

重新运行:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/1 8:33:21

深入解析SMUDebugTool:AMD Ryzen处理器调试实战手册

深入解析SMUDebugTool&#xff1a;AMD Ryzen处理器调试实战手册 【免费下载链接】SMUDebugTool A dedicated tool to help write/read various parameters of Ryzen-based systems, such as manual overclock, SMU, PCI, CPUID, MSR and Power Table. 项目地址: https://gitc…

作者头像 李华
网站建设 2026/4/21 22:16:31

工业控制场景下STLink接口引脚图实用解析(图解说明)

工业控制现场的“生命线”&#xff1a;一张STLink接口图&#xff0c;如何撑起整个调试链路&#xff1f;在工厂车间的一角&#xff0c;一台PLC控制器突然停机。维护人员迅速打开外壳&#xff0c;拿出便携式编程器&#xff0c;接入主板边缘一个不起眼的10针接口——几秒后&#x…

作者头像 李华
网站建设 2026/4/17 0:42:50

Cowabunga Lite:无需越狱的iOS深度定制完全指南

Cowabunga Lite&#xff1a;无需越狱的iOS深度定制完全指南 【免费下载链接】CowabungaLite iOS 15 Customization Toolbox 项目地址: https://gitcode.com/gh_mirrors/co/CowabungaLite 还在为iPhone单调的界面感到厌倦吗&#xff1f;想要打造真正属于自己的数字空间&a…

作者头像 李华
网站建设 2026/4/26 2:03:46

PDF-Extract-Kit成本优化:平衡性能与资源消耗的实践

PDF-Extract-Kit成本优化&#xff1a;平衡性能与资源消耗的实践 1. 背景与挑战&#xff1a;PDF智能提取中的资源瓶颈 1.1 PDF-Extract-Kit项目背景 PDF-Extract-Kit是由开发者“科哥”主导开发的一款开源PDF智能内容提取工具箱&#xff0c;旨在解决学术论文、技术文档、扫描…

作者头像 李华
网站建设 2026/4/18 10:01:08

WarcraftHelper:让经典魔兽在现代系统上完美运行

WarcraftHelper&#xff1a;让经典魔兽在现代系统上完美运行 【免费下载链接】WarcraftHelper Warcraft III Helper , support 1.20e, 1.24e, 1.26a, 1.27a, 1.27b 项目地址: https://gitcode.com/gh_mirrors/wa/WarcraftHelper 还在为魔兽争霸III在新系统上频繁闪退而烦…

作者头像 李华