SpringBoot讀取properties中文亂碼解決方案
一、問題描述
由于業(yè)務(wù)需求需要在application.properties中配置一個(gè)帶有中文字符串的參數(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,同時(shí)勾上Transparent native-to-ascii conversion的選項(xiàng),然后重新創(chuàng)建application.properties的文件。

運(yùn)行結(jié)果:

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

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

運(yùn)行效果

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

運(yùn)行結(jié)果:

2.2、重寫資源加載類(個(gè)人推薦)
1、創(chuàng)建一個(gè)類繼承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
重新運(yùn)行:

到此這篇關(guān)于SpringBoot讀取properties中文亂碼解決方案的文章就介紹到這了,更多相關(guān)SpringBoot讀取properties中文亂碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入學(xué)習(xí)Java同步機(jī)制中的底層實(shí)現(xiàn)
在多線程編程中我們會遇到很多需要使用線程同步機(jī)制去解決的并發(fā)問題,這些同步機(jī)制是如何實(shí)現(xiàn)的呢?下面和小編來一起學(xué)習(xí)吧2019-05-05
SpringBoot項(xiàng)目docker容器部署實(shí)現(xiàn)
本文主要介紹了SpringBoot項(xiàng)目docker容器部署實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
spring Boot查詢數(shù)據(jù)分頁顯示的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于spring Boot查詢數(shù)據(jù)分頁顯示的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
MyEclipse2017創(chuàng)建Spring項(xiàng)目的方法
這篇文章主要為大家詳細(xì)介紹了MyEclipse2017創(chuàng)建Spring項(xiàng)目的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03

