SpringBoot读取properties中文乱码解决方案
创作时间:
作者:
@小白创作中心
SpringBoot读取properties中文乱码解决方案
引用
CSDN
1.
https://blog.csdn.net/m0_74824845/article/details/146431756
一、问题描述
在使用SpringBoot开发过程中,如果需要在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;
@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.*;
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
重新运行项目后,中文乱码问题应该得到解决。
热门推荐
重大资产重组对股票的影响及购买资产计算方法
走跑跳投 “薰衣草园”里谁主沉浮——巴黎奥运会项目盘点之田径
自制花椒油(麻油)
“诺奖风向标”指向DNA与免疫关系研究 华人科学家斩获拉斯克基础医学研究奖
宁夏银川发生4.8级地震:专家解读地震成因及防范要点
全屋智能装修不翻车!一文讲透零线预留、双控改造等20个关键细节
苹果手机屏幕更换全攻略:判断、选择、流程及注意事项解析
内网穿透:实现远程访问和测试内部网络的关键技术
选购空调时需关注的关键参数与注意事项
最容易考的10所211大学,录取分数线低,每年学生都能捡漏!
寒冬下的电动汽车续航、充电、空调升温挑战,车企技术创新全览
中国县市旅行第294篇——河北省.廊坊市.安次区
射击世界杯破纪录夺冠 18岁的黄雨婷实现世锦赛、奥运会、世界杯金牌大满贯
7个强化外展肌的练习
姜夔暗香:宋词中的独特韵味与情感表达
如何利用AI技术创造美丽的女性肖像绘画
宇宙会有尽头吗?宇宙的尽头之外又有什么
44年前唐山大地震留下的城市抗震启示
地震的精确预报:现状与未来发展趋势
精选烘焙食谱:打造香甜可口的美味盛宴,唤醒你的味蕾
NFT技术开发音乐版权系统的技术原理
母女共有房母亲售房,女儿起诉合同无效被驳回的法律分析
王者荣耀受欢迎的原因是什么?王者荣耀的发展趋势如何?
癌症治疗的主要方法
防灾减灾:解读地震破坏
2024年1-3月全球地震活动述评
5G手机对电池续航能力的影响
从零开始学养花:如何控制光照让花卉茁壮成长
CPU散热风扇几伏特性最好?深入解析电压选择与性能优化
解讀人生秘密!紫微斗數怎麼看?12命宮、14主星全面解析