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
重新运行项目后,中文乱码问题应该得到解决。
热门推荐
济南三大夜游打卡地:大明湖、泉城广场、方特东方神画
济南最美夜景打卡地:大明湖&超然楼
首批宁夏著名品牌如何提升市场竞争力?
春节饮食需要注意什么?这几类特殊人群建议这样吃
探秘秘境:泸沽湖至稻城亚丁自驾路途详解与路况指南
流感防治全攻略:从症状识别到疫苗接种
如何应对宠物分离焦虑:有效的方法与建议
猫咪吃塑料袋怎么办?原因分析与科学应对指南
每天舔舔舔,猫猫是怎么处理那么多吃下去的毛毛?处理不完怎么办?
万和燃气热水器E1故障怎么办?这份安全使用指南请收好
广府文化中的饮食方面
双十一剁手后,如何避免陷入债务危机?
地方债:经济高质量发展的助推器与风险挑战
2025西岭雪山大飞水风景区攻略(附门票+地址+简介)
四川十大必去景点:从九寨沟到稻城亚丁,绝美风光让你流连忘返
探秘廿八都古镇:免费5A景区里的“文化飞地”
沪苏湖高铁带你畅游湖州免费景区
惠州西湖:新晋5A级景区必游指南
春节到,快来学学古人拜年的吉祥话!
索罗斯教你如何拥有交易高手的好心态
SWOT分析助你成为顶级交易员
广州到岳阳自驾游,这些景点你不能错过!
天目湖&中华恐龙园:常州必打卡摄影圣地
“光谷光子号”空轨:中国首列“飞行”列车来了
漳州古城和云水谣古镇二日游攻略:省钱又好玩
维生素B2美食地图:探索每日摄取量与食用技巧
富含维生素B族的食物
冬日大明湖摄影指南:从取景到拍摄技巧全攻略
大明湖四季美如画,《还珠格格》打卡地不容错过!
大明湖自驾游船攻略:150元/小时,这些细节你要知道