欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot讀取properties中文亂碼解決方案

 更新時間:2024年12月24日 09:40:12   作者:大餅酥  
本文主要介紹了在Spring?Boot中讀取帶有中文字符串的application.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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 小米推送Java代碼

    小米推送Java代碼

    今天小編就為大家分享一篇關于小米推送Java代碼,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 深入學習Java同步機制中的底層實現(xiàn)

    深入學習Java同步機制中的底層實現(xiàn)

    在多線程編程中我們會遇到很多需要使用線程同步機制去解決的并發(fā)問題,這些同步機制是如何實現(xiàn)的呢?下面和小編來一起學習吧
    2019-05-05
  • SpringBoot中使用Knife4J的解決方案

    SpringBoot中使用Knife4J的解決方案

    knife4j是為Java?MVC框架集成Swagger生成Api文檔的增強解決方案,這篇文章主要介紹了SpringBoot中使用Knife4J,需要的朋友可以參考下
    2022-10-10
  • Java跳臺階實現(xiàn)思路和代碼

    Java跳臺階實現(xiàn)思路和代碼

    今天小編就為大家分享一篇關于Java跳臺階實現(xiàn)思路和代碼,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • SpringBoot項目docker容器部署實現(xiàn)

    SpringBoot項目docker容器部署實現(xiàn)

    本文主要介紹了SpringBoot項目docker容器部署實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-03-03
  • Java使用Random類生成隨機數(shù)示例

    Java使用Random類生成隨機數(shù)示例

    這篇文章主要介紹了Java使用Random類生成隨機數(shù),結合實例形式分析了java基于Random類生成隨機數(shù)與遍歷輸出相關操作技巧,需要的朋友可以參考下
    2019-07-07
  • spring Boot查詢數(shù)據(jù)分頁顯示的方法實例

    spring Boot查詢數(shù)據(jù)分頁顯示的方法實例

    這篇文章主要給大家介紹了關于spring Boot查詢數(shù)據(jù)分頁顯示的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2020-08-08
  • Java多線程之中斷線程(Interrupt)的使用詳解

    Java多線程之中斷線程(Interrupt)的使用詳解

    interrupt字面上是中斷的意思,但在Java里Thread.interrupt()方法實際上通過某種方式通知線程,并不會直接中止該線程
    2013-05-05
  • MyEclipse2017創(chuàng)建Spring項目的方法

    MyEclipse2017創(chuàng)建Spring項目的方法

    這篇文章主要為大家詳細介紹了MyEclipse2017創(chuàng)建Spring項目的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Java內部類和異常類的概念以及使用

    Java內部類和異常類的概念以及使用

    這篇文章主要介紹了Java內部類和異常類的概念以及使用,文中有非常詳細的代碼以及注釋,適合正在學習java基礎的同學們使用,需要的朋友可以參考下
    2021-04-04

最新評論