Joc*_*ckX 3 java utf-8 properties-file spring-boot
我试图找到一种方法来为@ValueSpring Boot中的application.property文件中的注释访问的属性设置UTF-8编码.到目前为止,我已经通过创建bean成功地将编码设置为我自己的属性源:
@Bean
@Primary
public PropertySourcesPlaceholderConfigurer placeholderConfigurer(){
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setLocation(new ClassPathResource("app.properties");
configurer.setFileEncoding("UTF-8");
return configurer;
}
Run Code Online (Sandbox Code Playgroud)
这种解决方案存在两个问题 这一次,它不适用于Spring Boot默认使用的"application.properties"位置(http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config .html #boot-features-external-config),我被迫使用不同的文件名.
另一个问题是,通过它我可以手动定义和排序多个源的支持位置(例如,在jar与外部jar属性文件等中),从而重做已经完成的工作.
如何获取对已配置的PropertySourcesPlaceholderConfigurer的引用,并在应用程序初始化的恰当时间更改其文件编码?
编辑:也许我在其他地方犯了错误?这就是导致实际问题的原因:当我使用application.properties允许用户将个人名称应用于从应用程序发送的电子邮件时:
@Value("${mail.mailerAddress}")
private String mailerAddress;
@Value("${mail.mailerName}")
private String mailerName; // Actual property is ?wi?ty Miko?aj
private InternetAddress getSender(){
InternetAddress sender = new InternetAddress();
sender.setAddress(mailerAddress);
try {
sender.setPersonal(mailerName, "UTF-8"); // Result is Święty Mikołaj
// OR: sender.setPersonal(mailerName); // Result is ??wiÄ?ty Miko??aj
} catch (UnsupportedEncodingException e) {
logger.error("Unsupported encoding used in sender name", e);
}
return sender;
}
Run Code Online (Sandbox Code Playgroud)
当我placeholderConfigurer添加了如上所示的bean,并将我的属性放在'app.properties'中时,它已经恢复正常了.只需将文件重命名为'application.properties'就可以打破它.
| 归档时间: |
|
| 查看次数: |
8172 次 |
| 最近记录: |