SpringBoot讀取properties中文亂碼解決方案
一、問(wèn)題描述
由于業(yè)務(wù)需求需要在application.properties中配置一個(gè)帶有中文字符串的參數(shù),注入到業(yè)務(wù)類(lèi)中,但是發(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單元測(cè)試 * @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èn)題首先想到網(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è)配置文件用記事本打開(kāi)編輯時(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)容層級(jí)較深時(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); } }
親測(cè)發(fā)現(xiàn)這種方式針對(duì)application.properties是不行的。
但是針對(duì)其他名稱(chēng)的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、重寫(xiě)資源加載類(lèi)(個(gè)人推薦)
1、創(chuàng)建一個(gè)類(lèi)繼承PropertiesPropertySourceLoader,因SpringBoot版本不同PropertiesPropertySourceLoader類(lèi)會(huì)有差別,本文采用的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配置文件中文亂碼的問(wèn)題 * @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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入學(xué)習(xí)Java同步機(jī)制中的底層實(shí)現(xiàn)
在多線程編程中我們會(huì)遇到很多需要使用線程同步機(jī)制去解決的并發(fā)問(wèn)題,這些同步機(jī)制是如何實(shí)現(xiàn)的呢?下面和小編來(lái)一起學(xué)習(xí)吧2019-05-05Java跳臺(tái)階實(shí)現(xiàn)思路和代碼
今天小編就為大家分享一篇關(guān)于Java跳臺(tái)階實(shí)現(xiàn)思路和代碼,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01SpringBoot項(xiàng)目docker容器部署實(shí)現(xiàn)
本文主要介紹了SpringBoot項(xiàng)目docker容器部署實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03Java使用Random類(lèi)生成隨機(jī)數(shù)示例
這篇文章主要介紹了Java使用Random類(lèi)生成隨機(jī)數(shù),結(jié)合實(shí)例形式分析了java基于Random類(lèi)生成隨機(jī)數(shù)與遍歷輸出相關(guān)操作技巧,需要的朋友可以參考下2019-07-07spring Boot查詢數(shù)據(jù)分頁(yè)顯示的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于spring Boot查詢數(shù)據(jù)分頁(yè)顯示的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08MyEclipse2017創(chuàng)建Spring項(xiàng)目的方法
這篇文章主要為大家詳細(xì)介紹了MyEclipse2017創(chuàng)建Spring項(xiàng)目的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03Java內(nèi)部類(lèi)和異常類(lèi)的概念以及使用
這篇文章主要介紹了Java內(nèi)部類(lèi)和異常類(lèi)的概念以及使用,文中有非常詳細(xì)的代碼以及注釋,適合正在學(xué)習(xí)java基礎(chǔ)的同學(xué)們使用,需要的朋友可以參考下2021-04-04