spring使用@Value标签读取.properties文件的中文乱码问题的解决

最近测试某个老系统的时候,启动的时候发@Value注入的中文是乱码,文件使用GBK/UTF-8的时候均会出现乱码问题,但是spring配置文件里面注入的占位符并没有这个问题,bean文件设置了file-encoding="UTF-8"亦如此。

经查,可通过如下方式解决:

@Component
@PropertySource(value = "classpath:conf/spider.properties",encoding = "utf-8")
@Getter
public class SpiderConfig {
    @Value("${a}")
    private String a;
    @Value("${b}")
    private String b;
}

亦或是

不设置编码格式,编写文件时将中文转化为unicode编码

时间: 2024-08-29 02:24:37

spring使用@Value标签读取.properties文件的中文乱码问题的解决的相关文章

springBoot使用@Value标签读取*.properties文件的中文乱码问题

上次我碰到获取properties文件中的中文出现乱码问题. 查了下资料,原来properties默认的字符编码格式为asci码,所以我们要对字符编码进行转换成UTF-8格式 原先代码:@PropertySource("classpath:fu.properties") 改后代码:@PropertySource(value="classpath:fu.properties",encoding="utf-8") 然后就不会出现@Value标签读取*

读取Properties文件以及中文乱码问题

在java类中常见的读取Properties文件方式,是使用Properties.load(inputStream);的方式但是常常出现中文乱码问题,这就很尴尬了 public synchronized void load(InputStream inStream) throws IOException { load0(new LineReader(inStream)); } 看了很久才发现,还有一个重载的方法, 它的参数是Reader,如下: public synchronized void

spring使用@Value注解读取.properties文件时出现中文乱码问题的解决

解决办法 在spring中我们常常使用.properties对一些属性进行一个提前配置, spring 在读取*.properties文件时, 默认使用的是asci码, 这时 我们需要对其编码进行转换. 下面列举两种常见的方法. 方法一:在配置spring.xml文件时,声明所需的∗.properties文件时直接使用"utf−8"编码 <context:property-placeholder location="classpath:conf/*.properties

Spring用代码来读取properties文件

我们都知道,Spring可以@Value的方式读取properties中的值,只需要在配置文件中配置org.springframework.beans.factory.config.PropertyPlaceholderConfigurer <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

Spring 中 用 ${xxx} 读取properties文件的说明

properties 如果在 spring 中通过 PropertyPlaceholderConfigurer 加载,当spring 中需要 用到 properties 中的一些 key 和value 值时可以 利用 PorpertyPlaceholderConfiger 提供的$ 直接 取得. PorpertyPlaceholderConfiger 有一些常用的属性,在一些高级应用中,可能会用到 locations fileEncoding 属性文件的编码格式 order 文件中定义了多个Pr

使用spring最简单地读取properties文件中的内容

相比传统的读取propertis文件内容,使用spring框架会更加简单快捷 1. 第一步,在spring的配置文件中,将propertis文件加载到spring容器 2. 加载了配置文件后,只需要在需要使用的地方,使用spring注入即可 原文地址:https://www.cnblogs.com/mayiaction/p/10869758.html

Nio 读取UTF-8文件出现中文乱码

错误代码 String filePath = "viewflow.html" ; StringBuilder sb = new StringBuilder(1024*10); try { BufferedReader reader = new BufferedReader(new FileReader(new File(filePath))); CharBuffer charBuff = CharBuffer.allocate(1024); int length = -1 ; whil

linux下文件解压缩中文乱码问题的解决

将带中文文件名的压缩文件上传到服务器,使用unzip解压后,文件名乱码: 临时解决方法: 通过unzip行命令解压,指定字符集unzip -O CP936 xxx.zip (用GBK, GB18030也可以),可以通过man unzip查看该选项的说明. 永久生效方法: 在环境变量中,指定unzip参数,总是以指定的字符集显示和解压文件/etc/environment中加入2行 UNZIP="-O CP936" ZIPINFO="-O CP936" 原文地址:htt

【Spring源码分析】.properties文件读取及占位符${...}替换源码解析

前言 我们在开发中常遇到一种场景,Bean里面有一些参数是比较固定的,这种时候通常会采用配置的方式,将这些参数配置在.properties文件中,然后在Bean实例化的时候通过Spring将这些.properties文件中配置的参数使用占位符"${}"替换的方式读入并设置到Bean的相应参数中. 这种做法最典型的就是JDBC的配置,本文就来研究一下.properties文件读取及占位符"${}"替换的源码,首先从代码入手,定义一个DataSource,模拟一下JDB