<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          利用注解 + 反射消除重復(fù)代碼,妙!

          共 12596字,需瀏覽 26分鐘

           ·

          2021-11-28 15:03

          作者:Leilei Chen

          鏈接:https://llchen60.com/利用注解-反射消除重復(fù)代碼/

          1.1 案例場(chǎng)景

          假設(shè)銀行提供了一些 API 接口,對(duì)參數(shù)的序列化有點(diǎn)特殊,不使用 JSON,而是需要我們把參數(shù)依次拼在一起構(gòu)成一個(gè)大字符串:

          1)按照銀行提供的API文檔順序,將所有的參數(shù)構(gòu)成定長(zhǎng)的數(shù)據(jù),并且拼接在一起作為一整個(gè)字符串

          2)因?yàn)槊恳环N參數(shù)都有固定長(zhǎng)度,未達(dá)到長(zhǎng)度需要進(jìn)行填充處理

          • 字符串類(lèi)型參數(shù)不滿(mǎn)長(zhǎng)度部分要以下劃線右填充,即字符串內(nèi)容靠左
          • 數(shù)字類(lèi)型的參數(shù)不滿(mǎn)長(zhǎng)度部分以0左填充,即實(shí)際數(shù)字靠右
          • 貨幣類(lèi)型的表示需要把金額向下舍入2位到分,以分為單位,作為數(shù)字類(lèi)型同樣進(jìn)行左填充
          • 參數(shù)做MD5 操作作為簽名

          1.2 初步代碼實(shí)現(xiàn)

          public class BankService {

              //創(chuàng)建用戶(hù)方法
              public static String createUser(String name, String identity, String mobile, int age) throws IOException {
                  StringBuilder stringBuilder = new StringBuilder();
                  //字符串靠左,多余的地方填充_
                  stringBuilder.append(String.format("%-10s", name).replace(' ''_'));
                  //字符串靠左,多余的地方填充_
                  stringBuilder.append(String.format("%-18s", identity).replace(' ''_'));
                  //數(shù)字靠右,多余的地方用0填充
                  stringBuilder.append(String.format("%05d", age));
                  //字符串靠左,多余的地方用_填充
                  stringBuilder.append(String.format("%-11s", mobile).replace(' ''_'));
                  //最后加上MD5作為簽名
                  stringBuilder.append(DigestUtils.md2Hex(stringBuilder.toString()));
                  return Request.Post("http://localhost:45678/reflection/bank/createUser")
                          .bodyString(stringBuilder.toString(), ContentType.APPLICATION_JSON)
                          .execute().returnContent().asString();
              }

              //支付方法
              public static String pay(long userId, BigDecimal amount) throws IOException {
                  StringBuilder stringBuilder = new StringBuilder();
                  //數(shù)字靠右,多余的地方用0填充
                  stringBuilder.append(String.format("%020d", userId));
                  //金額向下舍入2位到分,以分為單位,作為數(shù)字靠右,多余的地方用0填充
                  stringBuilder.append(String.format("%010d", amount.setScale(2, RoundingMode.DOWN).multiply(new BigDecimal("100")).longValue()));
                  //最后加上MD5作為簽名
                  stringBuilder.append(DigestUtils.md2Hex(stringBuilder.toString()));
                  return Request.Post("http://localhost:45678/reflection/bank/pay")
                          .bodyString(stringBuilder.toString(), ContentType.APPLICATION_JSON)
                          .execute().returnContent().asString();
              }
          }

          這樣做能夠基本滿(mǎn)足需求,但是存在一些問(wèn)題:

          • 處理邏輯互相之間有重復(fù),稍有不慎就會(huì)出現(xiàn)Bug
          • 處理流程中字符串拼接、加簽和發(fā)請(qǐng)求的邏輯,在所有方法重復(fù)
          • 實(shí)際方法的入?yún)⒌膮?shù)類(lèi)型和順序,不一定和接口要求一致,容易出錯(cuò)
          • 代碼層面參數(shù)硬編碼,無(wú)法清晰進(jìn)行核對(duì)

          1.3 使用接口和反射優(yōu)化代碼

          1.3.1 實(shí)現(xiàn)定義了所有接口參數(shù)的POJO類(lèi)

          @Data
          public class CreateUserAPI {
              private String name;
              private String identity;
              private String mobile;
              private int age;
          }

          1.3.2 定義注解本身

          @Retention(RetentionPolicy.RUNTIME)
          @Target(ElementType.TYPE)
          @Documented
          @Inherited
          public @interface BankAPI {
              String desc() default "";
              String url() default "";
          }


          @Retention(RetentionPolicy.RUNTIME)
          @Target(ElementType.FIELD)
          @Documented
          @Inherited
          public @interface BankAPIField {
              int order() default -1;
              int length() default -1;
              String type() default "";
          }

          1.3.3 反射配合注解實(shí)現(xiàn)動(dòng)態(tài)的接口參數(shù)組裝

          private static String remoteCall(AbstractAPI api) throws IOException {
              //從BankAPI注解獲取請(qǐng)求地址
              BankAPI bankAPI = api.getClass().getAnnotation(BankAPI.class);
              bankAPI.url();
              StringBuilder stringBuilder = new StringBuilder();
              Arrays.stream(api.getClass().getDeclaredFields()) //獲得所有字段
                      .filter(field -> field.isAnnotationPresent(BankAPIField.class)) //查找標(biāo)記了注解的字段
                      .sorted(Comparator.comparingInt(a -> a.getAnnotation(BankAPIField.class).order())) //根據(jù)注解中的order對(duì)字段排序
                      .peek(field -> field.setAccessible(true)) //設(shè)置可以訪問(wèn)私有字段
                      .forEach(field -> {
                          //獲得注解
                          BankAPIField bankAPIField = field.getAnnotation(BankAPIField.class);
                          Object value = "";
                          try {
                              //反射獲取字段值
                              value = field.get(api);
                          } catch (IllegalAccessException e) {
                              e.printStackTrace();
                          }
                          //根據(jù)字段類(lèi)型以正確的填充方式格式化字符串
                          switch (bankAPIField.type()) {
                              case "S": {
                                  stringBuilder.append(String.format("%-" + bankAPIField.length() + "s", value.toString()).replace(' ''_'));
                                  break;
                              }
                              case "N": {
                                  stringBuilder.append(String.format("%" + bankAPIField.length() + "s", value.toString()).replace(' ''0'));
                                  break;
                              }
                              case "M": {
                                  if (!(value instanceof BigDecimal))
                                      throw new RuntimeException(String.format("{} 的 {} 必須是BigDecimal", api, field));
                                  stringBuilder.append(String.format("%0" + bankAPIField.length() + "d", ((BigDecimal) value).setScale(2, RoundingMode.DOWN).multiply(new BigDecimal("100")).longValue()));
                                  break;
                              }
                              default:
                                  break;
                          }
                      });
              //簽名邏輯
             stringBuilder.append(DigestUtils.md2Hex(stringBuilder.toString()));
              String param = stringBuilder.toString();
              long begin = System.currentTimeMillis();
              //發(fā)請(qǐng)求
              String result = Request.Post("http://localhost:45678/reflection" + bankAPI.url())
                      .bodyString(param, ContentType.APPLICATION_JSON)
                      .execute().returnContent().asString();
              log.info("調(diào)用銀行API {} url:{} 參數(shù):{} 耗時(shí):{}ms", bankAPI.desc(), bankAPI.url(), param, System.currentTimeMillis() - begin);
              return result;
          }

          通過(guò)反射來(lái)動(dòng)態(tài)獲得class的信息,并在runtime的時(shí)候完成組裝過(guò)程。

          這樣做的好處是開(kāi)發(fā)的時(shí)候會(huì)方便直觀很多,然后將邏輯與細(xì)節(jié)隱藏起來(lái),并且集中放到了一個(gè)方法當(dāng)中,減少了重復(fù),以及維護(hù)當(dāng)中bug的出現(xiàn)。

          1.3.4 在代碼中的應(yīng)用

          @BankAPI(url = "/bank/createUser", desc = "創(chuàng)建用戶(hù)接口")
          @Data
          public class CreateUserAPI extends AbstractAPI {
              @BankAPIField(order = 1, type = "S", length = 10)
              private String name;
              @BankAPIField(order = 2, type = "S", length = 18)
              private String identity;
              @BankAPIField(order = 4, type = "S", length = 11) //注意這里的order需要按照API表格中的順序
              private String mobile;
              @BankAPIField(order = 3, type = "N", length = 5)
              private int age;
          }



          @BankAPI(url = "/bank/pay", desc = "支付接口")
          @Data
          public class PayAPI extends AbstractAPI {
              @BankAPIField(order = 1, type = "N", length = 20)
              private long userId;
              @BankAPIField(order = 2, type = "M", length = 10)
              private BigDecimal amount;
          }


          怎么接私活?這個(gè)渠道你100%有用!請(qǐng)收藏!


          ,點(diǎn)個(gè)在看 
          瀏覽 37
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  免费看一级高潮毛片 | 久久亚洲成人 | 免费人成在线 | 国产免费AV片 | 丁香六月婷婷综合激情欧美 |