Spring messageSource


Spring中可以使用兩個類加載資源文件:

org.springframework.context.support.ReloadableResourceBundleMessageSource

org.springframework.context.support.ResourceBundleMessageSource

可配置如下:

ReloadableResourceBundleMessageSource:

<bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>andy/resources/testInfo</value>
            </list>
        </property>
    </bean>
ResourceBundleMessageSource:
<bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>andy/resources/testInfo</value>
            </list>
        </property>
    </bean>

Spring提供了一個接口MessageSource用於獲取國際化信息,ReloadableResourceBundleMessageSource和ResourceBundleMessageSource都是繼承了該接口的一個抽象實現類AbstractMessageSource,繼承該抽象類的有四個類,分別是:

1、

public class StaticMessageSource extends AbstractMessageSource {
...
}

2、

public class SpringSecurityMessageSource extends ResourceBundleMessageSource {
...
}

3、

public class ReloadableResourceBundleMessageSource extends AbstractMessageSource
    implements ResourceLoaderAware
{
...
}

4、

public class ResourceBundleMessageSource extends AbstractMessageSource
    implements BeanClassLoaderAware
{
...
}

每個類的用處不同,StaticMessageSource主要用於測試環境,並不用於生產環境,SpringSecurityMessageSource用於Spring security的國際化信息

ApplicationContext實現了MessageSource接口,所以,ApplicationContext自身提供了國際化信息功能

例子如下:

項目結構截圖

在這里,我們測試Spring提供的國際化信息功能,文件名稱為testInfo的Resource bundle中有兩個文件,分別為英語,中文,國際化資源文件有一定的命名規范,只有符合命名規范的國際化資源文件才能正確的被Spring讀取,國際化資源問及愛你命名規范遵循:${filename}_${languagename}_${countryname},其中${}是需要替代的內容,下划線是必需的分隔符,所以如果你想要定義一個中文國際化文件應該是這樣的 testInfo_zh_CN,至於language和countryname的取名請參見java.util.Locale類,里面有詳細說明。

Spring配置文件如下(app-context.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="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-2.5.xsd">

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>andy/resources/testInfo</value>
            </list>
        </property>
    </bean>

</beans>

這里定義了一個MessageSource的實現類ResourceBundleMessageSource用戶提供國際化功能,為什么這里的id以messageSource命名呢?如果不明白可以查看AbstractApplicationContext的源代碼,你會發現里面定義了一個messageSource的屬性,並提供了set方法,也就是Spring在初始化時將Spring配置文件(app-context.xml)中id為messageSource的bean注入到ApplicationContext中,這樣我們就可以使用ApplicationContext提供的國際化功能了,一下是測試類:

package test.andy;

import java.util.Locale;

import junit.framework.TestCase;

import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MessageSourceTest extends TestCase {
    public void testResourceBundleMessageSource(){
        MessageSource messageSource=new ClassPathXmlApplicationContext("app-context.xml");
        String username_us=messageSource.getMessage("userName_lable",new Object[1],Locale.US);
        String username_chinese=messageSource.getMessage("userName_lable",new Object[0],Locale.CHINESE);
        System.out.println("chinese:"+username_chinese);
        System.out.println("english:"+username_us);
    }
}

運行結果:

chinese:用戶名
english:userName
ReloadableResourceBundleMessageSource和ResourceBundleMessageSource有着微小區別,從字面就可以看出,ReloadableResourceBundleMessageSource可以在不用重新啟動服務器的情況下,讀取更改后的資源文件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM