使用Spring的ReloadableResourceBundleMessageSource读取properties配置

应用:

1.后台验证提示信息;

2.异常信息。

spring配置文件如下:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  
         <property name="basename" value="classpath:message-resource"/>  
         <property name="defaultEncoding" value="GBK"/>  
    </bean>

message-resource即为classpath下的message-resource.properties文件。

接下来定义我们自己的MessageUtil类来使用Spring的MessageSource读取配置。

 

public class MessageUtil
{
    private static MessageSource messageSource;

    private static void init()
    {
        if (messageSource == null)
        {
            synchronized (MessageUtil.class)
            {
                messageSource = (MessageSource) applicationContextFactory.getBean("messageSource");
            }
        }
    }

    public static String getMessage(String id, Object[] param)
    {
        init();
        return messageSource.getMessage(id, param, "Required", null);
    }
    public static String getMessage(String id)
    {
        init();
        return messageSource.getMessage(id, null, "Required", null);
    }
}

使用的时候就很简单了。MessageUtil.getMessage(properties文件中配置的key)就OK了。
posted @ 2014-11-21 17:51  烟花易冷丶人心易凉  阅读(1281)  评论(0编辑  收藏  举报