Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring MessageSource似乎忽略了属性fallbackToSystemLocale_Java_Spring_Spring Java Config - Fatal编程技术网

Java Spring MessageSource似乎忽略了属性fallbackToSystemLocale

Java Spring MessageSource似乎忽略了属性fallbackToSystemLocale,java,spring,spring-java-config,Java,Spring,Spring Java Config,我在配置Spring MessageSource以忽略系统区域设置时遇到问题。当我使用null语言环境参数调用getMessage时,我希望MessageSource选择默认属性文件messages.properties。相反,它选择messages\u en.properties。当我将此属性文件的名称更改为messages\u fr.properties时,默认属性文件为choosen。我的系统区域设置为“en”。因此,MessageSource似乎忽略了我设置为false的fallback

我在配置Spring MessageSource以忽略系统区域设置时遇到问题。当我使用null语言环境参数调用getMessage时,我希望MessageSource选择默认属性文件messages.properties。相反,它选择messages\u en.properties。当我将此属性文件的名称更改为messages\u fr.properties时,默认属性文件为choosen。我的系统区域设置为“en”。因此,MessageSource似乎忽略了我设置为false的fallbackToSystemLocale属性

此行为与Spring版本4.1.4和4.1.5相同

消息源配置:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="fallbackToSystemLocale" value="false"></property>
    <property name="basenames">
        <list>
            <value>locale/messages</value>
        </list>
    </property>
    <property name="defaultEncoding" value="UTF-8"></property>
</bean>

谢谢你的建议

fallbackToSystemLocale
不是当您使用
locale=null调用消息源时,用于指导消息源所做的操作


fallbackToSystemLocale
控制当您请求一条对于所请求的本地语言不存在的消息(代码)时要执行的操作,这可能是因为该语言没有消息属性文件,或者只是因为消息文件不包含消息代码

另一方面,当您使用
locale=null
messageSource.getMessage(“key”,null);
)调用
getMessage
时,locale将由
locale.getDefault
设置

org.springframework.context.support.AbstractMessageSource:

在考虑
fallbackToSystemLocale
属性之前

因此,最简单的黑客-around(它不是workaround,它是黑客)将使用您不支持的语言,而不是
null

messageSource.getMessage(“key”,新语言环境(“XX”)

谢谢您的回答。不管怎样,你不觉得这种行为不合逻辑吗?我希望在提供空语言环境的情况下使用默认消息属性文件。如果我想使用本地系统,我会做一些类似于
messageSource.getMessage(“key”,locale==null?locale.getDefault():locale)的事情。i、 Wicket框架在我写作时解决了这个问题,对我来说似乎更好。我可能会扩展
ResourceBundleMessageSource
类并重写
getMessageInternal
,以反映我的需要。如果未宣布为最终版本,当然:)
String message = messageSource.getMessage("user.email.notFound", new Object[] {email}, null);
protected String getMessageInternal(String code, Object[] args, Locale locale) {
    ...
    if (locale == null) {
        locale = Locale.getDefault();
    }
    ...