Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoSuchMessageException - Spring ReloadableResourceBundleMessageSource vs ResourceBundleMessageSource

Tags:

java

spring

I defined the following Spring bean:

<bean id="messageSource" 
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>classpath:messages</value>
        </list>
    </property>
</bean>

The controller:

@Controller
public class EuserController {

    @Inject
    MessageSource messageSource;

    @RequestMapping(value="/euser/{empId}", method=RequestMethod.DELETE)
    public @ResponseBody String deleteEeuserById(@PathVariable(value="empId") Integer id) {
        return messageSource.getMessage("deleteEuser.success", null, LocaleContextHolder.getLocale());
    }
}

And it works fine. But when I'm trying to replace:

org.springframework.context.support.ReloadableResourceBundleMessageSource

with:

org.springframework.context.support.ResourceBundleMessageSource

and I get a org.springframework.context.NoSuchMessageException.

What does this happen when using the org.springframework.context.support.ResourceBundleMessageSource instead?

like image 593
user3663882 Avatar asked Dec 31 '25 21:12

user3663882


1 Answers

ReloadableResourceBundleMessageSource is an alternative to ResourceBundleMessageSource that is capable of refreshing the messages while the application is running. It's also more powerful as you are not limited to bundles on the classpath but you can load files from other locations too.

When using a ResourceBundleMessageSource you need to restart your application when making changes as ResourceBundleMessageSource doesn't reload your bundles when you change them. The classpath: prefix also needs to be removed. This is because of the way the two classes work:

  • The ResourceBundleMessageSource uses a JDK class to do its thing: ResourceBundle. It delegates to it to load the bundle. Basically, the bundle you give to ResourceBundleMessageSource must conform to what ResourceBundle expects and processes. ResourceBundle does not know how to handle the classpath: prefix and so it fails.

  • ReloadableResourceBundleMessageSource on the other hand is "smarter" and knows how to load bundles from other places, not just the classpath. It works with a Spring class: Resource. There are various implementations out of the box. When you give a bundle to ReloadableResourceBundleMessageSource, since it can load files from various places, you have to be explicit with the location and say "My file is on the classpath". You say that by adding the classpath: prefix and Spring knows how to handle it.

like image 99
Bogdan Avatar answered Jan 03 '26 11:01

Bogdan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!