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 \n{0} exception was detected, caused by :\n\n{1} \n\nSincerely \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 \n{0} exception was detected, caused by :\n\n{1} \n\nSincerely \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);
        };
    }

 


免責聲明!

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



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