ResourceBundle not found for MessageSource when placed inside a folder

后端 未结 13 1275
星月不相逢
星月不相逢 2020-12-08 17:11

I am trying to use resource bundles with Spring\'s Message Source. Here is the way I am doing it:

@Component
public class MessageResolver implements MessageS         


        
相关标签:
13条回答
  • 2020-12-08 17:56

    What worked for me was something really simple.

    It was

    <property name="basename">
                <value>locale\messages</value>
            </property>
    

    I changed it to

    <property name="basename">
                <value>locale/messages</value>
            </property>
    

    Just a \ to / change fixed it for me. I am using a MAC.

    I have not tried *classpath, that may not have worked for me.

    0 讨论(0)
  • 2020-12-08 17:58
     <!-- Application Message Bundle -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="resourcebundles/messages" />
    </bean>
    

    You have to configure your messages path as shown above. Also, check class name.

    0 讨论(0)
  • 2020-12-08 18:02

    Change your configuration to the following for messageSource bean in your xml file.

    <bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
        <property name="basename"> 
            <value>classpath*:resourcebundles/messages</value> 
        </property> 
    </bean>
    

    Since all your properties files are in classpath of java you need to define the path with prefix classpath*: otherwise it will look into the web directory of your application.

    Hope this helps you. Cheers.

    0 讨论(0)
  • 2020-12-08 18:03

    Is is that if I have to use ResourceBundleMessageSource, I should put my resource bundles directly under the resources? If i have to keep it in specified folder only, is there any other way to get this one work?

    You can define your messages in your own package, it does not need to be located in the resources folder.

    Using Spring 5.2.2.RELEASE versioned components, this is the way I managed to make it work:

    The qualified name of the file would be:

    /tutproject/src/com/tutproject/app/messages/messages.properties
    

    The bean is defined like this in the my Spring Bean Configuration File (XML):

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

    The Java build path includes tutproject/src , this is the part of the location omitted in the XML definition.

    Some additional useful information from the ResourceBundleMessageSource class:

    • The basenames follow {@link java.util.ResourceBundle} conventions: essentially, * a fully-qualified classpath location. If it doesn't contain a package qualifier * (such as {@code org.mypackage}), it will be resolved from the classpath root. * Note that the JDK's standard ResourceBundle treats dots as package separators: * This means that "test.theme" is effectively equivalent to "test/theme".

    0 讨论(0)
  • 2020-12-08 18:03

    YAML version for this

      spring:
         messages:
           basename: i18n/validation, i18n/message # for multiple properties just use comma separated values
           encoding: UTF-8
    

    You can refer to documentation to see full description.

    Also I should mention that the default MessageSource bean is a ResourceBundleMessageSource which is already reading form a classpath so there is no need to use nonation like classpath:i18n/validation.

    Directory structure

    0 讨论(0)
  • 2020-12-08 18:06

    boy, maybe you can change the xml configuration as follows:

    use

    org.springframework.context.support.ReloadableResourceBundleMessageSource
    

    instead of

    org.springframework.context.support.ResourceBundleMessageSource
    

    all configuration like this:

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:resourcebundles/messages" />
        <property name="useCodeAsDefaultMessage" value="true" />
    </bean>
    
    0 讨论(0)
提交回复
热议问题