SpringBoot读取properties中文乱码解决方案

06-01 685阅读

目录

一、问题描述

二、解决方案

2.1、网络上的解决办法

2.1.1、修改IDEA编码?

2.1.2、改为yml配置

2.1.3、读取时设置编码

2.2、重写资源加载类(个人推荐)


一、问题描述

由于业务需求需要在application.properties中配置一个带有中文字符串的参数,注入到业务类中,但是发现注入的中文是乱码的。大概情况如下所示:

SpringBoot读取properties中文乱码解决方案

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);
    }
}

打印输出结果:

SpringBoot读取properties中文乱码解决方案

二、解决方案

2.1、网络上的解决办法

遇到问题首先想到网络上找解决方案,网络上的解决办法基本一致,概括为以下三种。

2.1.1、修改IDEA编码

在IDEA中将所有的编码设置为UTF-8,同时勾上Transparent native-to-ascii conversion的选项,然后重新创建application.properties的文件。

SpringBoot读取properties中文乱码解决方案

运行结果:

SpringBoot读取properties中文乱码解决方案

但是这个配置文件用记事本打开编辑时,发现内容被修改成了unicode编码,在线上修改时变得很困难,所以此方式我不做推荐。

SpringBoot读取properties中文乱码解决方案

2.1.2、改为yml配置

就是将application.properties的文件修改为application.yml的结构,重启项目。

SpringBoot读取properties中文乱码解决方案

运行效果

SpringBoot读取properties中文乱码解决方案

证明是可行的。这种方式可以根据自己需求选择,但是当配置文件的内容层级较深时也不推荐,容易看错行配置。

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);
    }
}

亲测发现这种方式针对application.properties是不行的。

SpringBoot读取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);
    }
}

SpringBoot读取properties中文乱码解决方案

运行结果:

SpringBoot读取properties中文乱码解决方案

2.2、重写资源加载类(个人推荐)

1、创建一个类继承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 properties = this.loadUseUtf8(resource);
        if (properties.isEmpty()) {
            return Collections.emptyList();
        }
        return Collections.singletonList(new OriginTrackedMapPropertySource(name, Collections.unmodifiableMap((Map)properties), true));
    }
    private Map 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目录下创建目录META-INF,在META-INF目录下创建文件spring.factories

SpringBoot读取properties中文乱码解决方案

内容如下:

org.springframework.boot.env.PropertySourceLoader=com.cnstar.utils.SelfPropertySourceLoader

重新运行:

SpringBoot读取properties中文乱码解决方案

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码