CentOS使用@Value注解为属性赋值的时候出现乱码

在本地开发用windows的没有出现乱码,在CentOS上运行的时候出现乱码.

 

1.修改中文的编码方式  (成功) env.properties为ANSI格式

先设置idea编码格式,utf-8,

 

将中文转成Unicode编码方式,然后替换配置文件中的对应中文,这样加载进去的时候就是中文,乱码问题完美解决.

env.properties有中文如下

#是否调用测试接口,为false为正式接口,为true为测试接口, 测试接口在代码中修改
KDN_IsTest=false

中文转成ANSI格式以后变为这样:

#\u662F\u5426\u8C03\u7528\u6D4B\u8BD5\u63A5\u53E3,\u4E3Afalse\u4E3A\u6B63\u5F0F\u63A5\u53E3\uFF0C\u4E3Atrue\u4E3A\u6D4B\u8BD5\u63A5\u53E3\uFF0C \u6D4B\u8BD5\u63A5\u53E3\u5728\u4EE3\u7801\u4E2D\u4FEE\u6539
KDN_IsTest=false

 

 (以下网上找的失败方案)

1.使用PropertySource (该方案失败)  env.properties为utf-8

@PropertySource(value = "classpath:conf/evn.properties",encoding = "utf-8")
public class CopyWriteUI {
 @Value("${a}")
 private String a;
}
 
2.修改applection中引用加入<property name="fileEncoding" value="UTF-8"></property>(该方案失败) env.properties为utf-8
<bean

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:properties/env.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"></property>
</bean>

posted @ 2019-03-18 21:24  为乐而来  阅读(407)  评论(0编辑  收藏  举报