【问题标题】:Spring unable to load resourcebundleSpring 无法加载资源包
【发布时间】:2015-05-26 13:50:53
【问题描述】:

我已经声明了一个组件

@Component
public class I18nManagerImpl implements I18nManager{

@Autowired
MessageSource messageSource;

public ResourceBundle getResourceBundle() {
    Locale fromConfig = Locale.UK;

    //Resolve Locale from a configuration service

    return new MessageSourceResourceBundle(messageSource, fromConfig); //replace code in my question
}
@Override
public String message(String locale) {
    ResourceBundle beanResourceBundle = getResourceBundle();
    String invalidEmail = beanResourceBundle.getString("invalidEmail");
    return invalidEmail;
}

}

messageSource bean 定义在applicationContext.xml

<bean id="messageSource"         
class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames" value="messages"/>
</bean>

但是找不到消息,如果用调试器查找由 getResourceBundle 键返回的 ResourceBundle 为 null 并且没有内容。

提供的代码有什么问题? 这是项目结构图:

【问题讨论】:

  • 你可以尝试编辑:
  • 你为什么用这个?为什么不简单地使用可用的默认支持?有什么特别需要你自己定制的I18nManagerImpl
  • @Arpit 不工作,结果相同。
  • @M.Deinum 你能举个例子吗?我将自定义以增加灵活性,启用缓存并提供方便的方法来访问某些不使用原始字符串的翻译,
  • 您的包含属性文件的文件夹是“messages”或“Resource Bundle”还是“Resource Bundle 'messages'”?

标签: java spring resourcebundle properties-file


【解决方案1】:
<bean id="messageSource"         
class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames" value="classpath:messages"/>
</bean>

添加类路径

还要加-

<resources mapping="/resources/**" location="/resources/" />

由于您使用的是注释 - 添加

<annotation-driven />
<context:component-scan base-package="your.base.package" />

资源的命名空间 -

<beans xmlns="http://www.springframework.org/schema/mvc"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

【讨论】:

【解决方案2】:

问题是propoerties 文件不在源根目录中。我将文件插入到 src config 文件夹中,并将该文件夹标记为源根目录。

【讨论】:

    猜你喜欢
    • 2014-04-15
    • 2013-07-18
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多