SpringBoot讀取properties中文亂碼解決方案
一、問題描述
由于業(yè)務需求需要在application.properties中配置一個帶有中文字符串的參數(shù),注入到業(yè)務類中,但是發(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("中文內容:" + name); } }
打印輸出結果:
二、解決方案
2.1、網(wǎng)絡上的解決辦法
遇到問題首先想到網(wǎng)絡上找解決方案,網(wǎng)絡上的解決辦法基本一致,概括為以下三種。
2.1.1、修改IDEA編碼
在IDEA中將所有的編碼設置為UTF-8,同時勾上Transparent native-to-ascii conversion的選項,然后重新創(chuàng)建application.properties的文件。
運行結果:
但是這個配置文件用記事本打開編輯時,發(fā)現(xiàn)內容被修改成了unicode編碼,在線上修改時變得很困難,所以此方式我不做推薦。
2.1.2、改為yml配置
就是將application.properties的文件修改為application.yml的結構,重啟項目。
運行效果
證明是可行的。這種方式可以根據(jù)自己需求選擇,但是當配置文件的內容層級較深時也不推薦,容易看錯行配置。
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); } }
親測發(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); } }
運行結果:
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
內容如下:
org.springframework.boot.env.PropertySourceLoader=com.cnstar.utils.SelfPropertySourceLoader
重新運行:
到此這篇關于SpringBoot讀取properties中文亂碼解決方案的文章就介紹到這了,更多相關SpringBoot讀取properties中文亂碼內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot項目docker容器部署實現(xiàn)
本文主要介紹了SpringBoot項目docker容器部署實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-03-03spring Boot查詢數(shù)據(jù)分頁顯示的方法實例
這篇文章主要給大家介紹了關于spring Boot查詢數(shù)據(jù)分頁顯示的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2020-08-08MyEclipse2017創(chuàng)建Spring項目的方法
這篇文章主要為大家詳細介紹了MyEclipse2017創(chuàng)建Spring項目的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03