• 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);
            };
        }
  • 相关阅读:
    IO 单个文件的多线程拷贝
    day30 进程 同步 异步 阻塞 非阻塞 并发 并行 创建进程 守护进程 僵尸进程与孤儿进程 互斥锁
    day31 进程间通讯,线程
    d29天 上传电影练习 UDP使用 ScketServer模块
    d28 scoket套接字 struct模块
    d27网络编程
    d24 反射,元类
    d23 多态,oop中常用的内置函数 类中常用内置函数
    d22 封装 property装饰器 接口 抽象类 鸭子类型
    d21天 继承
  • 原文地址:https://www.cnblogs.com/lshan/p/11271665.html
Copyright © 2020-2023  润新知