[英]Spring Boot properties files using UTF-8
Although Java Properties files traditionally supported only ISO-8859-1, JDK 9 and onward supports properties files encoded in UTF-8 .尽管 Java 属性文件传统上仅支持 ISO-8859-1,但JDK 9 及更高版本支持以 UTF-8 编码的属性文件。 And while only JDK 9+ supports UTF-8 with built-in default properties file reading, the same technique it uses could be done in any Java version, wrapping around the properties file to add UTF-8 support and fall back to ISO-8859-1 for backwards compatibility (as the JDK implementation does).
虽然只有 JDK 9+ 支持带有内置默认属性文件读取的 UTF-8,但它使用的相同技术可以在任何 Java 版本中完成,环绕属性文件以添加 UTF-8 支持并回退到 ISO-8859 -1 用于向后兼容(就像 JDK 实现那样)。
I'm new to Spring Boot, and I'm reading about all the nifty properties configurations it brings.我是 Spring Boot 的新手,我正在阅读它带来的所有漂亮的属性配置。 Does Spring Boot support loading properties from a properties file encoded in UTF-8?
Spring Boot 是否支持从以 UTF-8 编码的属性文件加载属性? And if not, where in the Spring Boot code is properties file reading consolidated, so that I can add this capability and submit a pull request?
如果没有,Spring Boot 代码中的何处合并了属性文件读取,以便我可以添加此功能并提交拉取请求?
By default Spring only support ISO-8859-1 properties.默认情况下,Spring 仅支持 ISO-8859-1 属性。 But there exist several ways to work around this:
但是有几种方法可以解决这个问题:
application.yaml
property file.application.yaml
属性文件。@PropertySource(value = "classpath:/custom-name-of-application.properties", encoding="UTF-8")
@PropertySource(value = "classpath:/custom-name-of-application.properties", encoding="UTF-8")
注释您的 @Configuration 类mvn spring-boot:run -Drun.jvmArguments="-Dfile.encoding=UTF-8"
mvn spring-boot:run -Drun.jvmArguments="-Dfile.encoding=UTF-8"
@PropertySource(value = "classpath:application-${env}.properties", encoding = "UTF-8")
use用
@PropertySource(value = "classpath:application.properties", encoding = "UTF-8")
at class level, then you can retrieve the single properties with @value, but you need to define this bean: `在类级别,然后您可以使用@value 检索单个属性,但您需要定义此 bean:`
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
@Value("${my.property1}")
private String property1;
` `
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.