【问题标题】:UTF-8 not supported in environment variable?环境变量不支持 UTF-8?
【发布时间】:2016-12-15 12:30:37
【问题描述】:

我正在使用 Spring @PropertySource 注释根据环境变量加载属性文件。 (在Windows中设置System variablesof this window

我设置MY_APP = C:\Cliché

现在我的@Configuration 类也被注释了:

@PropertySource(value = "file:${MY_APP}/config/my.properties"

但 Spring 无法加载该文件,因为它似乎将 URL/路径解析为:C:\Clich,\config\my.properties

为什么?

【问题讨论】:

    标签: java spring utf-8 properties


    【解决方案1】:

    试试,在属性源中指定编码

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

    而且,您应该在启动应用程序之前设置系统编码

    System.setProperty("file.encoding", "UTF-8");

    或通过命令行 => -Dfile.encoding=UTF-8

    【讨论】:

    • 但是这些设置是为了读取属性文件的内容对吧?
    • 对于@propertysource 是的,但是要解释您的系统编码,您需要设置 file.encoding=UTF-8
    • encoding="UTF-8" 作为@PropertySource 属性,也没有将-Dfile.encoding=UTF-8 添加到我的Tomcat VM 参数中。一旦我运行一个纯 Java 配置的 Spring 应用程序,我就无法设置这个 System.setProperty("file.encoding", "UTF-8"); 行来尝试......
    猜你喜欢
    • 2017-04-17
    • 2015-11-16
    • 2019-07-16
    • 1970-01-01
    • 2014-05-26
    • 2011-11-21
    • 2014-01-03
    • 1970-01-01
    • 2018-08-07
    相关资源
    最近更新 更多