@PropertySource和UTF-8属性文件

and*_*lva 5 spring utf-8 character-encoding

是否可以使用@PropertySource注释配置必须用于加载属性文件的编码?

一个澄清我的问题的例子

@Configuration
@PropertySource("classpath:/myprop.properties")
public class MyApplicationContext {

    @Autowired(required = true)
    private Environment env;

    @Bean
    public MyBean myBean() {
       return new MyBean(env.getRequiredProperty("application.name"));
    }

}
Run Code Online (Sandbox Code Playgroud)

myprop.properties是一个UTF-8文件,但无论如何,"application.name"被解释为ISO-8859-1.

解决方法是在属性文件中转义特殊字符,但是设置编码是可能的,context:property-placeholder所以我认为应该可以做同样的@PropertySource

小智 12

现在可以:

@PropertySource(value = "classpath:/myprop.properties", encoding="UTF-8")


Joa*_*uer 6

.properties文件按照 ISO-8859-1编码定义.所以我担心你不能这样做.

但是,您可以使用\uXXXX unicode转义来表示所需的任何unicode字符.(略微错误命名的)native2ascii工具可以帮助自动执行此操作.