[英]UTF-8 encoding of application.properties attributes in Spring-Boot
[英]Spring Boot properties files using UTF-8
尽管 Java 属性文件传统上仅支持 ISO-8859-1,但JDK 9 及更高版本支持以 UTF-8 编码的属性文件。 虽然只有 JDK 9+ 支持带有内置默认属性文件读取的 UTF-8,但它使用的相同技术可以在任何 Java 版本中完成,环绕属性文件以添加 UTF-8 支持并回退到 ISO-8859 -1 用于向后兼容(就像 JDK 实现那样)。
我是 Spring Boot 的新手,我正在阅读它带来的所有漂亮的属性配置。 Spring Boot 是否支持从以 UTF-8 编码的属性文件加载属性? 如果没有,Spring Boot 代码中的何处合并了属性文件读取,以便我可以添加此功能并提交拉取请求?
默认情况下,Spring 仅支持 ISO-8859-1 属性。 但是有几种方法可以解决这个问题:
application.yaml
属性文件。@PropertySource(value = "classpath:/custom-name-of-application.properties", encoding="UTF-8")
注释您的 @Configuration 类mvn spring-boot:run -Drun.jvmArguments="-Dfile.encoding=UTF-8"
@PropertySource(value = "classpath:application-${env}.properties", encoding = "UTF-8")
用
@PropertySource(value = "classpath:application.properties", encoding = "UTF-8")
在类级别,然后您可以使用@value 检索单个属性,但您需要定义此 bean:`
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
@Value("${my.property1}")
private String property1;
`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.