开发环境(Idea)之中 Spring 使用@Value注解 .properties文件的中文乱码问题解决

目录

1、Spring 使用@Value 读取.properties文件中文乱码

2、针对*.properties编码转换的几种方法汇集

2.2、针对所需类上注入可使用以下方式来声明编码格式

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

3、Spring  属性说明

4、参考文章


1、Spring 使用@Value 读取.properties文件中文乱码

        最近在整理SpringBoot相关的学习示例,准备针对多环境不同配置进行整理。发现一个问题,我使用的 *.properties 文件。在写单元测试用例的时候,发现从.properties文件之中读取的文件未乱码;导致单元测试不成功。         

      在Spring中常常使用.properties对一些属性进行一些基本的配置, Spring 在读取*.properties文件时,默认使用的是ascii码, 这时 我们需要对其编码进行转换.

2、针对*.properties编码转换的几种方法汇集

2.1、在配置spring.xml文件时,声明所需的∗.properties文件时直接使用"utf−8"编码

       此种方法目前估计很少人使用啦,此方法在Spring xml配置时代可以使用

<context:property-placeholder location="classpath:*.properties" file-encoding="UTF-8"/>

2.2、针对所需类上注入可使用以下方式来声明编码格式

@Component
@PropertySource(value = "classpath:themeConfig.properties",encoding = "utf-8")
public class ThemeStyleConfig {
    @Value("${clolor}")
    private String clolor;
    @Value("${themeName}")
    private String themeName;
}

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

         曾经写过Java代码读取 文件,需要设置读取文件的格式编码

/**
* properties 转换内容转换为Excel
*/
private void propertiesFileConvertToExcel(String inputPropertieFilePath,String outExcelFileNamePath) {    	
	System.setProperty("file.encoding", "UTF-8");     
	readPropertiesContent(inputPropertieFilePath);
	writePropertiesContentToExcelFile(outExcelFileNamePath); 	
}

/**
 * Excel写回到Properties文件之中
 * @param propertiesPath
 */
public void writeExcelToPropertiesFile(String propertiesPath) {
	
	Properties props = new OrderedProperties();

	//创建一个文件对象,该对象将指向属性文件的位置
	File propertiesFile = new File(propertiesPath);

	try {

		//通过传递上述属性文件创建FileOutputStream 并且设置每次覆盖重写
		FileOutputStream xlsFos = new FileOutputStream(propertiesFile,false);

		// 首先将哈希映射键转换为Set,然后对其进行迭代。
		Iterator<String> mapIterator = excelContentToPropertiesMap.keySet().iterator();

		//遍历迭代器属性
		while(mapIterator.hasNext()) {

			// extracting keys and values based on the keys
			String key = mapIterator.next().toString();

			String value = excelContentToPropertiesMap.get(key);
			String keys = "Keys";
			if(!keys.equals(key)) {
				//在上面创建的props对象中设置每个属性key与value
				props.setProperty(key, value);
			}    
		}

		//最后将属性存储到实属性文件中。
		props.store(new OutputStreamWriter(xlsFos, "utf-8"), null);

	} catch (FileNotFoundException e) {
		e.printStackTrace();

	} catch (IOException e) {

		e.printStackTrace();

	}
}

    并且最近使用Maven的一个插件也遇到类似问题。

这个其实是 maven-resources-plugins 搞得鬼,无论如何修改编码都不行。最后没辙了,只好把中文强制修改为 unicode,虽然不利用阅读,但是解决了问题。

问题地址:maven filter 中文乱码,应该用什么思路解决?

2.4、使用IntelliJIDEA 如下图操作即可解决

3、Spring <context:property-placeholder/> 属性说明

<context:property-placeholder   
	location="属性文件,多个之间逗号分隔"  
	file-encoding="文件编码"  
	ignore-resource-not-found="是否忽略找不到的属性文件"  
	ignore-unresolvable="是否忽略解析不到的属性,如果不忽略,找不到将抛出异常"  
	properties-ref="本地Properties配置"  
	local-override="是否本地覆盖模式,即如果true,那么properties-ref的属性将覆盖location加载的属性,否则相反"  
	system-properties-mode="系统属性模式,默认ENVIRONMENT(表示先找ENVIRONMENT,再找properties-ref/location的),NEVER:表示永远不用ENVIRONMENT的,OVERRIDE类似于ENVIRONMENT"  
	order="顺序"  
/>

4、参考文章

Spring使用@Value注释读取.properties文件

@Value读取配置文件,中文字符乱码

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值