NoSuchMessageException - Spring ReloadableResourceBundleMessageSource vs ResourceBundleMessageSource
I have 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>
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 great. But when I try to replace:
org.springframework.context.support.ReloadableResourceBundleMessageSource
from:
org.springframework.context.support.ResourceBundleMessageSource
and I get a org.springframework.context.NoSuchMessageException
.
What happens when used org.springframework.context.support.ResourceBundleMessageSource
instead?
source to share
ReloadableResourceBundleMessageSource
is an alternative ResourceBundleMessageSource
that is capable of updating messages while the application is running. It's also more powerful since you're not limited to packages on the classpath, but you can also download files from elsewhere.
When used, ResourceBundleMessageSource
you need to restart the application when you make changes, as it ResourceBundleMessageSource
does not reload your packages when they change. The prefix classpath:
also needs to be removed. This has to do with how the two classes work:
-
ResourceBundleMessageSource
JDK uses the class to carry out its tasks:ResourceBundle
. He delegates to him to download the package. Basically, the packet you are passingResourceBundleMessageSource
in should match what itResourceBundle
expects and processes.ResourceBundle
doesn't know how to handle the prefixclasspath:
and so it fails. -
ReloadableResourceBundleMessageSource
on the other hand is "smarter" and knows how to load packages from other places, not just the classpath. It works with Spring class:Resource
. There are various implementations out of the box . When you pass a packageReloadableResourceBundleMessageSource
, since it can load files from different locations, you must be explicit with the location and say "My file is on the classpath". You say that by adding a prefixclasspath:
and Spring knows how to handle it .
source to share