[已解决] 从Spring Boot项目的application.properties获取属性值乱码

得意时要看淡,失意时要看开。不论得意失意,切莫大意;不论成功失败,切莫止步。志得意满时,需要的是淡然,给自己留一条退路;失意落魄时,需要的是泰然,给自己觅一条出路[已解决] 从Spring Boot项目的application.properties获取属性值乱码,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

项目场景

Spring Boot 2.4


问题描述

Spring Boot项目的代码逻辑中,从application.properties获取到的属性值显示乱码


原因分析

因为spring boot项目默认的加载application.properties配置文件是通过字符集StandardCharsets.ISO_8859_1载入的。

相关代码在 org.springframework.boot.env.OriginTrackedPropertiesLoader类中,具体代码片段如下:

/**
 * Reads characters from the source resource, taking care of skipping comments,
 * handling multi-line values and tracking {@code '\'} escapes.
 */
private static class CharacterReader implements Closeable {
	private static final String[] ESCAPES = { "trnf", "\t\r\n\f" };
	private final LineNumberReader reader;
	private int columnNumber = -1;
	private boolean escaped;
	private int character;
	private boolean lastLineComment;

	CharacterReader(Resource resource) throws IOException {
		this.reader = new LineNumberReader(
				new InputStreamReader(resource.getInputStream(), StandardCharsets.ISO_8859_1));
	}

解决方案

方案一

根据上面的分析,可以使用下面的方法,原理:

创建同包同名的类,可以直接覆盖掉jar包中的类,spring项目会优先加载自定义的类

  1. 在自己的项目里,src/main/java 下创建一个同名的package:org.springframework.boot.env
  2. 定位到 org.springframework.boot.env.OriginTrackedPropertiesLoader类,将这个类copy到上面新建的package里
  3. 在上面步骤复制好的类里,修改上面指定字符集的代码,如下:
/**
 * Reads characters from the source resource, taking care of skipping comments,
 * handling multi-line values and tracking {@code '\'} escapes.
 */
private static class CharacterReader implements Closeable {
	private static final String[] ESCAPES = { "trnf", "\t\r\n\f" };
	private final LineNumberReader reader;
	private int columnNumber = -1;
	private boolean escaped;
	private int character;
	private boolean lastLineComment;

	CharacterReader(Resource resource) throws IOException {
		this.reader = new LineNumberReader(
				new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF-8));
	}
  1. 编译项目,再启动,即可解决。

方案二

将汉字转为Unicode码,再存入配置文件,即可解决。

原理:

几乎 java 中所有汉字乱码问题都可以用 unicode 编码 来解决

工具:菜鸟工具

方案三

将application.properties改为application.yml。
相对于 .properties文件,.yml文件要友好的多,正常使用的话,应该是不存在汉字乱码问题。

方案四

创建一个新的properties配置文件,例如:demo.properties,设置文件编码格式为UTF-8。
引入配置并指定字符集,注意:这种方式针对application.properties是不行的。

添加下面的代码到启动类或者需要的类上:

@PropertySource(value = "classpath:demo.properties",encoding = "UTF-8")

例如:

@Component
@ConfigurationProperties(prefix = "demo")
@PropertySource(value = "classpath:demo.properties",encoding = "UTF-8")
@Getter
@Setter
public class DemoProperties {
...
}

扩展:

Spring 4.3 通过引入 PropertySourceFactory 接口。
PropertySourceFactory 是PropertySource 的工厂类。默认实现是 DefaultPropertySourceFactory,可以构造ResourcePropertySource 实例。
@PropertySource 注解有一个 factory 属性,通过这个属性来注入 PropertySourceFactory。

扩展

如果 通过 nacos 来加载 yml 配置,汉字会出现乱码,甚至导致 nacos 不能启动。使用 unicode 的方式解决。
如果是 nacos 中加载配置文件,只要有汉字就会出错,因此,注释信息也尽量移除。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/155585.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!