StackoverflowExcepton in Spring i18n Message ReloadableResourceBundleMessageSource

I am including the i18n post in my Spring web application. For this, I have the code below inservlet.xml

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages/message"/>
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>

After adding the above code, as soon as I hit my application in the browser, I got below the exception log:

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)

where the last 2 lines are repeated 100 times and give me StackoverflowException.

Exactly the same exception occurs when I use a class ResourceBundleMessageSource.

My version of Spring 4.3.6.RELEASE.

Below is the content of my properties file

action.add.success = New {0} added successfully.
action.add.failure = Some error occurred in adding new {0}. Please try again later or contact administrator.

An example project is on GitHub

+4
source share
5 answers

A very strange situation; -)

, messageSource ( parentMessageSource), default-autowire="byType", stackoverflow , . , logback , , , Log4J .

Autowiring , , , , , messageSource bean :

       <property name="parentMessageSource"><null/></property>

.

, NoSuchMessageException

:

  • hello.world , message
  • , , . messages/message.properties , - , . , ( ).
+1

:

  • default-autowire="byType", , stackoverflow;
  • LocaleContextHolder.getLocale(), ;
  • , ReloadableResourceBundleMessageSource ResourceBundleMessageSource, ;

    ResourceBundle [/] MessageSource: /message, locale en_US

Runnable example


StackTrace

, :

  • , ;
  • , , , StackOverflow ( AbstractMessageSource ​​, , , );
  • , , , , StackOverflow;

  • , , , spring ;
  • , 4.1.6 Relase, , ;
  • DEBUG , , ;
+1

github, , :

Config:

<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.

.

:

, , , beans ( ), , , , beans, .

<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;

, .

+1

ResourceBundleMessageSource basename.

, :

  • messages_en.properties
  • messages_es.properties

resources. bean :

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basename" value="messages" />
</bean>

messages - .

, Exception, Spring id , , ...

Mkyong.

0

, applicationContext.xml :

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:i18n/message" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="lang" />
</bean>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="en"/>
</bean>

<bean id="handlerMapping"
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <ref bean="localeChangeInterceptor" />
    </property>
</bean>

, :

src/main/resources/i18n/message_en.properties
0

Source: https://habr.com/ru/post/1677918/


All Articles