簡體   English   中英

Spring無法加載資源束

[英]Spring unable to load resourcebundle

我已經聲明了一個組件

@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;
}

}

並且applicationContext.xml messageSource bean定義是

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

但是找不到消息,並且如果使用調試器查看,則getResourceBundle鍵返回的ResourceBundle為null,並且沒有內容。

提供的代碼有什么問題? 這是項目結構的圖像: 在此處輸入圖片說明

<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">

問題在於屬性文件不在源根目錄中。 我將文件插入src config文件夾,並將該文件夾標記為源根。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM