天天看點

Prototype功能預覽八:一個注解實作短信郵件内容模闆

架構整體介紹:http://blog.csdn.net/flyxxxxx/article/category/7055640

對此架構有興趣或想參與開發的請加QQ群:255195191

一般發送短信或郵件,均需要調用相關接口,如果接口發生變理,通常可能需要修改大量的調用代碼,而采用Prototype架構,可以做到更大程度的解耦。

@Prototype

public class TemplateBusiness {

   public void business(){

      ...;

      sendSms(user);

   }

   @Msg(type="sms")//這裡發短信消息

    SMS sendSms(User user){

      return new SMS(user.getTelephone(),template(user));//構造一個短信對象,具體如何發送與此業務類無關

    }

    @Template("${user.name},你好")//這裡采用jsp el語言作為預設的模闆引擎

    String template1(User user){

        return null;//不需要做任何實作

    }

    @Template(file="classpath:template1.txt",engine="js")//這裡采用javascript作為模闆引擎

    String template3(User user){

        return null;//不需要做任何實作

    } 

    @Data

    public static class User {

        private String name;

    }

}

當然,如果短信或郵件内容的模闆來源于資料庫,隻需要實作一個接口就可以完成對接。

繼續閱讀