messageHelper 统一管理项目的中message

场景: 项目中常常会有一些message , 如邮件, 短信,  UI的提示信息, 多数情况,写在代码中,或者配置文件xxx.properties, @value 或者读取xxx.properties ,这两种方案都...

1.好处,统一管理

2.动态管理,如配置了appolo ,  配置中心

 3. 配置国际化, 如中文 ,英文

使用:

一.依赖:

     
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

二.配置文件xxx.properties位置:

 默认名字为 messages.properties

内容 :eg

     //event.validation.content= Notification 
{0} exception was detected, caused by :

{1} 

Sincerely XXX Team
     //event.validation.subject=ERROR: Validation error found for booking <{0}>

  

三.配置类:

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

import java.util.Locale;


@Component
@Slf4j
public class MesgHelper {

    private static MessageSource messageSource;

    public static MessageSource getMessageSource() {
        return messageSource;
    }
    @Autowired
    public void setMessageSource(MessageSource msgSource) {
        MesgHelper.messageSource = msgSource;
    }

    /**
     * Convert single message according to locale.
     *
     * @param mesgKey
     * @param values
     * @param locale
     * @return
     */
    public static String getMessage(String mesgKey,Object[] values,Locale locale)    {
        Locale locale1 = locale!=null?locale:Locale.US;
        String mesgText = null;

        if(mesgKey != null && locale1 != null)    {
            mesgText = messageSource.getMessage(mesgKey, values, locale1);
        }
        return mesgText;
    }
}

四.使用:

   //注入
      @Autowired
      MesgHelper mesgHelper

    private static Runnable validationNotify(String bookingNo,String mesgKeys,String body) {
        return ()->{
            log.debug("Current thread is {}",Thread.currentThread());
            log.info("@@!!@@ NOT pass the null validation checking , below data not allow null: {}", mesgKeys);
            LinkedHashMap msgBody = JSONObject.parseObject(body,new TypeReference<LinkedHashMap<String, Object>>(){}
                    ,Feature.OrderedField);
            String mailBody = JSONObject.toJSONString(msgBody,
                    SerializerFeature.PrettyFormat,
                    SerializerFeature.WriteMapNullValue);
        //参数变量 用{0}  {2} 表示
     //event.validation.content= Notification 
{0} exception was detected, caused by :

{1} 

Sincerely XXX Team
     //event.validation.subject=ERROR: Validation error found for booking <{0}>
            //参数变量 
            Object[] paras = {bookingNo};
            String subject = mesgHelper.getMessage("event.validation.subject", paras, null);
            //参数变量 
            Object[] params = { mesgKeys, mailBody};
            String content = mesgHelper.getMessage("event.validation.content", params, null);
           
            MailUtils.sendMail(auoExOp,from,subject,content,to);
        };
    }
原文地址:https://www.cnblogs.com/lshan/p/11271665.html