我正在我的 Spring Web 应用程序中启用消息国际化。为此,我的 servlet.xml
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages/message"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
添加上面的代码后,只要我在浏览器中点击我的应用程序,我就会得到以下异常日志:
SEVERE: Servlet.service() for servlet [default] in context with path [/ERP-Web] threw exception [Filter execution threw an exception] with root cause
java.lang.StackOverflowError
at org.springframework.context.support.ReloadableResourceBundleMessageSource.getMergedProperties(ReloadableResourceBundleMessageSource.java:235)
at org.springframework.context.support.ReloadableResourceBundleMessageSource.resolveCodeWithoutArguments(ReloadableResourceBundleMessageSource.java:176)
at org.springframework.context.support.AbstractMessageSource.getMessageInternal(AbstractMessageSource.java:209)
at org.springframework.context.support.AbstractMessageSource.getMessageFromParent(AbstractMessageSource.java:257)
最后 2 行重复了 100 次并给我 StackoverflowException
。
当我使用 ResourceBundleMessageSource
类时出现完全相同的异常。
我的 Spring 版本是4.3.6.RELEASE
。
下面是我的属性文件的内容
action.add.success = New {0} added successfully.
action.add.failure = Some error occurred in adding new {0}. Please try again later or contact administrator.
示例项目在 GitHub 上
最佳答案
我已经在github上测试了你的示例代码,运行时出现了所描述的错误,然后修改如下:
配置:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages/message"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
类:
@RequestMapping(method=RequestMethod.GET)
@ResponseBody
public String getMessage() {
String msg = messageSource.getMessage("hello.world", null, LocaleContextHolder.getLocale());
return msg;
}
hello.word 是文件中带有名称 message_en.properties 的文本属性。
修改后代码运行。
编辑未知消息代码:
我尝试使用未知消息代码,但错误重复出现,所以我查看了注册表,发现可能有多个 bean 具有相同的名称(潜在的循环引用),但我还没有检测到为什么会发生这种情况,但如果你需要它工作,你必须像这样重命名 bean。
<bean id="myMessageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages/message"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
然后使用:
@Autowired
private MessageSource myMessageSource;
但我认为这并没有解决循环潜在错误的主要问题。
关于java - Spring 消息 i18n ReloadableResourceBundleMessageSource 中的 StackoverflowExcepton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44182515/