0

I'm trying to have my project's Strings/ messages stored in an external .properties file. I think I have everything wired up OK, but still I get:

org.springframework.context.NoSuchMessageException: No message found under code 'subtype.user.client' for locale 'null'.

Whenever I try:

String string = messageSource.getMessage("subtype.user.client", null, null);

My spring xml config files are as follows. Since the project is really big with lots of beans, I have different spring xml config files defining different types of beans, and a main spring.config.xml file that wires them all together.

Messages file named messages.subtypes

subtype.user.user=User
subtype.user.client=Client props
subtype.user.staff=Staff
subtype.user.clerk=Clerk
subtype.user.secretary=Secretary

Messages beans file called spring.messages.config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

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

    <bean id="myProjectLangs" class="myprojectbase.MyProjectLangs">
        <property name="messageSource" ref="messageSource"></property>
    </bean>

</beans>

The main spring.config.xml config file that wires all the beans together via <import resource="classpath:filename.xml"/>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd 
  http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

    <aop:aspectj-autoproxy />

    <import resource="classpath:spring.messages.config.xml"/>

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" />

    <context:annotation-config />
    <context:component-scan base-package="myprojectbase"/>

</beans>
2
  • Did you put the message file in the classpath or in maven resources folder? Commented Oct 1, 2015 at 7:10
  • I'm using NetBeans. It is in the <default package> Commented Oct 1, 2015 at 7:13

3 Answers 3

2

You get this error because you pass the Locale parameter as null. Try

String string = messageSource.getMessage("subtype.user.client", null, Locale.ENGLISH);

Even though you have not defined a file messages.subtypes_en.properties defined it should fall back to messages.subtypes.properties

Sign up to request clarification or add additional context in comments.

Comments

1

A couple of things come to mind looking at your code any of which might cause the problem:

  • Your xml config name contains "." as separators. This is against conventions. Consider renaming your config file to spring-messages-config.xml
  • Your language properties file has no properties suffix, again convention suggests to name this file messages-subtypes.properties
  • In both your application context xml files you define a bean named messageSource. Consider deleting one of them.
  • My prime suspicion as to why your code does not work lies with the way you define basename on ReloadableResourceBundleMessageSource. Looking at the JavaDoc for setBasename method there is some form of convention of configuration at work:

Set a single basename, following the basic ResourceBundle convention of not specifying file extension or language codes, but in contrast to {@link ResourceBundleMessageSource} referring to a Spring resource location: e.g. "WEB-INF/messages" for "WEB-INF/messages.properties", "WEB-INF/messages_en.properties", etc. XML properties files are also supported: .g. "WEB-INF/messages" will find and load "WEB-INF/messages.xml", "WEB-INF/messages_en.xml", etc as well.

This suggests that once you have renamed your message properties file to messages-subtypes.properties, you should change your config to <value>classpath:messages-subtypes</value>, make sure that the file is in the classpath and everything should start working.

Comments

0

Try renaming the messages.subtypes file to messages.subtypes.properties.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.