SpringBoot讀取properties中文亂碼解決方案
一、問題描述
由于業(yè)務(wù)需求需要在application.properties中配置一個帶有中文字符串的參數(shù),注入到業(yè)務(wù)類中,但是發(fā)現(xiàn)注入的中文是亂碼的。大概情況如下所示:

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("中文內(nèi)容:" + name);
}
}打印輸出結(jié)果:

二、解決方案
2.1、網(wǎng)絡(luò)上的解決辦法
遇到問題首先想到網(wǎng)絡(luò)上找解決方案,網(wǎng)絡(luò)上的解決辦法基本一致,概括為以下三種。
2.1.1、修改IDEA編碼
在IDEA中將所有的編碼設(shè)置為UTF-8,同時勾上Transparent native-to-ascii conversion的選項,然后重新創(chuàng)建application.properties的文件。

運行結(jié)果:

但是這個配置文件用記事本打開編輯時,發(fā)現(xiàn)內(nèi)容被修改成了unicode編碼,在線上修改時變得很困難,所以此方式我不做推薦。

2.1.2、改為yml配置
就是將application.properties的文件修改為application.yml的結(jié)構(gòu),重啟項目。

運行效果

證明是可行的。這種方式可以根據(jù)自己需求選擇,但是當配置文件的內(nèi)容層級較深時也不推薦,容易看錯行配置。
2.1.3、讀取時設(shè)置編碼
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);
}
}親測發(fā)現(xiàn)這種方式針對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);
}
}

運行結(jié)果:

2.2、重寫資源加載類(個人推薦)
1、創(chuàng)建一個類繼承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目錄下創(chuàng)建目錄META-INF,在META-INF目錄下創(chuàng)建文件spring.factories

內(nèi)容如下:
org.springframework.boot.env.PropertySourceLoader=com.cnstar.utils.SelfPropertySourceLoader
重新運行:

到此這篇關(guān)于SpringBoot讀取properties中文亂碼解決方案的文章就介紹到這了,更多相關(guān)SpringBoot讀取properties中文亂碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot如何為web層添加統(tǒng)一請求前綴
這篇文章主要介紹了springboot如何為web層添加統(tǒng)一請求前綴,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
SpringBoot中統(tǒng)一同步與異步執(zhí)行模型的示例詳解
這篇文章主要為大家詳細介紹了SpringBoot中如何統(tǒng)一同步與異步執(zhí)行模型,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-12-12
Java運算符與流程控制之全類型運算符用法及分支?/?循環(huán)語句實戰(zhàn)指南
這篇文章主要介紹了Java運算符與流程控制之全類型運算符用法及分支/循環(huán)語句實戰(zhàn)的相關(guān)資料,運算符包括算術(shù)、關(guān)系、邏輯、賦值和三元運算符,重點講解了它們的用法、優(yōu)先級和結(jié)合性,以及強制類型轉(zhuǎn)換的注意事項,需要的朋友可以參考下2026-01-01
使用java實現(xiàn)http多線程斷點下載文件(一)
Java 多線程斷點下載文件基本原理:利用URLConnection獲取要下載文件的長度、頭部等相關(guān)信息,并設(shè)置響應(yīng)的頭部信息,本文將詳細介紹,需要了解更多的朋友可以參考下2012-12-12
Java中的instanceof關(guān)鍵字在Android中的用法實例詳解
instanceof是Java的一個二元操作符,和==,>,<是同一類東西。接下來通過本文給大家介紹Java中的instanceof關(guān)鍵字在Android中的用法,非常不錯,具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-07-07
SpringData如何通過@Query注解支持JPA語句和原生SQL語句
這篇文章主要介紹了SpringData如何通過@Query注解支持JPA語句和原生SQL語句,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

