我正在尝试使用Spring的Message Source资源包.这是我这样做的方式:
@Component
public class MessageResolver implements MessageSourceAware {
@Autowired
private MessageSource messageSource;
public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}
public String getMessage(){
return messageSource.getMessage("user.welcome", new Object[]{"Rama"} , Locale.US);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的文件夹结构:

messages_en_US.properties只包含一行:
user.welcome=Welcome {0}
Run Code Online (Sandbox Code Playgroud)
这是使用的xml配置:
<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>resourcebundles/messages</value>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
WARNING: ResourceBundle [resourcebundles/messages] not found for MessageSource: Can't find bundle for base name resourcebundles/messages, locale en_US
Exception in thread "main" org.springframework.context.NoSuchMessageException: No message found under code 'user.welcome' for locale 'en_US'.
Run Code Online (Sandbox Code Playgroud)
但是,如果我将资源包直接移动到资源文件夹下,它工作正常.在这种情况下,这是我正在使用的xml配置:
<bean name="messageSource" …Run Code Online (Sandbox Code Playgroud)