简体   繁体   English

NoSuchMessageException - Spring ReloadableResourceBundleMessageSource vs ResourceBundleMessageSource

[英]NoSuchMessageException - Spring ReloadableResourceBundleMessageSource vs ResourceBundleMessageSource

I defined the following Spring bean: 我定义了以下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 . 我得到一个org.springframework.context.NoSuchMessageException

What does this happen when using the org.springframework.context.support.ResourceBundleMessageSource instead? 使用org.springframework.context.support.ResourceBundleMessageSource时会发生什么?

ReloadableResourceBundleMessageSource is an alternative to ResourceBundleMessageSource that is capable of refreshing the messages while the application is running. ReloadableResourceBundleMessageSourceResourceBundleMessageSource的替代方法,它能够在应用程序运行时刷新消息。 It's also more powerful as you are not limited to bundles on the classpath but you can load files from other locations too. 它也更强大,因为您不仅限于类路径上的bundle,但您也可以从其他位置加载文件。

When using a ResourceBundleMessageSource you need to restart your application when making changes as ResourceBundleMessageSource doesn't reload your bundles when you change them. 使用ResourceBundleMessageSource您需要在进行更改时重新启动应用程序,因为ResourceBundleMessageSource在您更改它们时不会重新加载它们。 The classpath: prefix also needs to be removed. 还需要删除classpath:前缀。 This is because of the way the two classes work: 这是因为这两个类的工作方式:

  • The ResourceBundleMessageSource uses a JDK class to do its thing: ResourceBundle . ResourceBundleMessageSource使用JDK类来完成它的任务: ResourceBundle It delegates to it to load the bundle. 它委托它来加载bundle。 Basically, the bundle you give to ResourceBundleMessageSource must conform to what ResourceBundle expects and processes. 基本上,您提供给ResourceBundleMessageSource的包必须符合ResourceBundle期望和处理的内容。 ResourceBundle does not know how to handle the classpath: prefix and so it fails. ResourceBundle不知道如何处理classpath:前缀,因此失败。

  • ReloadableResourceBundleMessageSource on the other hand is "smarter" and knows how to load bundles from other places, not just the classpath. 另一方面, ReloadableResourceBundleMessageSource是“更智能”的,并且知道如何从其他地方加载包,而不仅仅是类路径。 It works with a Spring class: Resource . 它适用于Spring类: 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". 当您向ReloadableResourceBundleMessageSource提供一个包时,因为它可以从各个位置加载文件,您必须明确该位置并说“我的文件在类路径上”。 You say that by adding the classpath: prefix and Spring knows how to handle it . 你说通过添加classpath:前缀和Spring知道如何处理它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring ReloadableResourceBundleMessageSource VS ResourceBundleMessageSource - Spring ReloadableResourceBundleMessageSource VS ResourceBundleMessageSource ReloadableResourceBundleMessageSource:org.springframework.context.NoSuchMessageException - ReloadableResourceBundleMessageSource: org.springframework.context.NoSuchMessageException 无法将[…ReloadableResourceBundleMessageSource]转换为所需的类型[…ResourceBundleMessageSource] - Cannot convert […ReloadableResourceBundleMessageSource] to required type […ResourceBundleMessageSource] Spring Boot 2.0.1中的NoSuchMessageException - NoSuchMessageException in Spring boot 2.0.1 Spring Boot中带有MessageSource的NoSuchMessageException - NoSuchMessageException with MessageSource in Spring Boot Spring ReloadableResourceBundleMessageSource的问题 - Issue with Spring ReloadableResourceBundleMessageSource JSP中的Spring ReloadableResourceBundleMessageSource实现 - Spring reloadableresourcebundlemessagesource implementation in jsp Spring ReloadableResourceBundleMessageSource:看到UnknownFormatConversionException - Spring ReloadableResourceBundleMessageSource: Seeing UnknownFormatConversionException 在Java类中使用Spring ReloadableResourceBundleMessageSource - Using Spring ReloadableResourceBundleMessageSource in java class Spring MVC ResourceBundleMessageSource用于不同的主题 - Spring mvc ResourceBundleMessageSource for different themes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM