Guava动态调用方法
前言
大家在Coding的时候,经常会遇到这样一个情况,根据不同的条件去执行对应的代码。我们通常的处理方式是利用if-else判断,或者直接switch-case,特别是jdk1.6之后,swith开始支持判断String类型了。
但是如果有几十种情况上百种情况,用if-else来处理这段逻辑,代码看着特别不舒服。
解决这个麻烦有好几种办法,比如自定义注解、Spring结合策略模式、反射或者枚举。
下面分享一下使用Guava工具类通过反射动态调用方法。
业务描述
插座统计:数据库每添加一个插座实例,都统计一下这个插座属于中国哪个省份(细节不重要)
pom.xml
添加工具依赖
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>20.0</version> </dependency>
ProvinceEnum.java
前端传过来的是中文,需要获取对应的英文名,然后通过拼接的方式生成方法名。
1 public enum ProvinceEnum { 2 3 SHANDONG("山东", "Shandong"), 4 5 SHANGHAI("上海", "Shanghai"), 6 7 ZHEJIANG("浙江", "Zhejiang"), 8 9 ANHUI("安徽", "Anhui"), 10 11 FUJIAN("福建", "Fujian"), 12 13 JIANGXI("江西", "Jiangxi"), 14 15 GUANGDONG("广东", "Guangdong"), 16 17 GUANGXI("广西", "Guangxi"), 18 19 HAINAN("海南", "Hainan"), 20 21 HENAN("河南", "Henan"), 22 23 HUNAN("湖南", "Hunan"), 24 25 HUBEI("湖北", "Hubei"), 26 27 BEIJING("北京", "Beijing"), 28 29 TIANJIN("天津", "Tianjin"), 30 31 HEBEI("河北", "Hebei"), 32 33 SHANXI("山西", "Shanxi"), 34 35 INNERMONGOLIA("内蒙古", "Innermongolia"), 36 37 NINGXIA("宁夏", "Ningxia"), 38 39 QINGHAI("青海", "Qinghai"), 40 41 SHAANXI("陕西", "Shaanxi"), 42 43 GANSU("甘肃", "Gansu"), 44 45 SINKIANG("新疆", "Sinkiang"), 46 47 SICHUAN("四川", "Sichuan"), 48 49 GUIZHOU("贵州", "Guizhou"), 50 51 YUNNAN("云南", "Yunnan"), 52 53 CHONGQING("重庆", "Chongqing"), 54 55 TIBET("西藏", "Tibet"), 56 57 LIAONING("辽宁", "Liaoning"), 58 59 JILIN("吉林", "Jilin"), 60 61 HEILONGJIANG("黑龙江", "Heilongjiang"), 62 63 HONGKONG("香港", "Hongkong"), 64 65 MACAO("澳门", "Macao"), 66 67 TAIWAN("台湾", "Taiwan"), 68 69 JIANGSU("江苏", "Jiangsu"); 70 71 private String code; 72 73 private String msg; 74 75 private ProvinceEnum(String code, String msg) { 76 this.code = code; 77 this.msg = msg; 78 } 79 80 public String getCode() { 81 return code; 82 } 83 84 public String getMsg() { 85 return msg; 86 } 87 88 public static String getMsgByCode(String code) { 89 String msg = null; 90 for (ProvinceEnum e : ProvinceEnum.values()) { 91 if (e.getCode().equals(code)) { 92 msg = e.getMsg(); 93 break; 94 } 95 } 96 return msg; 97 } 98 99 }
SocketStatistics.java
实体类,统计的对象
1 public class SocketStatistics { 2 /** 3 * 主键自增长 4 */ 5 private Long pid; 6 7 private Integer shandong; 8 9 private Integer jiangsu; 10 11 private Integer shanghai; 12 13 private Integer zhejiang; 14 15 private Integer anhui; 16 17 private Integer fujian; 18 19 private Integer jiangxi; 20 21 private Integer guangdong; 22 23 private Integer guangxi; 24 25 private Integer hainan; 26 27 private Integer henan; 28 29 private Integer hunan; 30 31 private Integer hubei; 32 33 private Integer beijing; 34 35 private Integer tianjin; 36 37 private Integer hebei; 38 39 private Integer shanxi; 40 41 /** 42 * 内蒙古 43 */ 44 private Integer innermongolia; 45 46 private Integer ningxia; 47 48 private Integer qinghai; 49 50 /** 51 * 陕西 52 */ 53 private Integer shaanxi; 54 55 private Integer gansu; 56 57 private Integer sinkiang; 58 59 private Integer sichuan; 60 61 private Integer guizhou; 62 63 private Integer yunnan; 64 65 private Integer chongqing; 66 67 /** 68 * 西藏 69 */ 70 private Integer tibet; 71 72 private Integer liaoning; 73 74 private Integer jilin; 75 76 private Integer heilongjiang; 77 78 private Integer hongkong; 79 80 /** 81 * 澳门 82 */ 83 private Integer macao; 84 85 private Integer taiwan; 86 87 /** 88 * 在线数量 89 */ 90 private Integer online; 91 92 /** 93 * 离线数量 94 */ 95 private Integer outline; 96 97 public Long getPid() { 98 return pid; 99 } 100 101 public void setPid(Long pid) { 102 this.pid = pid; 103 } 104 105 public Integer getShandong() { 106 return shandong; 107 } 108 109 public void setShandong(Integer shandong) { 110 this.shandong = shandong; 111 } 112 113 public Integer getJiangsu() { 114 return jiangsu; 115 } 116 117 public void setJiangsu(Integer jiangsu) { 118 this.jiangsu = jiangsu; 119 } 120 121 public Integer getShanghai() { 122 return shanghai; 123 } 124 125 public void setShanghai(Integer shanghai) { 126 this.shanghai = shanghai; 127 } 128 129 public Integer getZhejiang() { 130 return zhejiang; 131 } 132 133 public void setZhejiang(Integer zhejiang) { 134 this.zhejiang = zhejiang; 135 } 136 137 public Integer getAnhui() { 138 return anhui; 139 } 140 141 public void setAnhui(Integer anhui) { 142 this.anhui = anhui; 143 } 144 145 public Integer getFujian() { 146 return fujian; 147 } 148 149 public void setFujian(Integer fujian) { 150 this.fujian = fujian; 151 } 152 153 public Integer getJiangxi() { 154 return jiangxi; 155 } 156 157 public void setJiangxi(Integer jiangxi) { 158 this.jiangxi = jiangxi; 159 } 160 161 public Integer getGuangdong() { 162 return guangdong; 163 } 164 165 public void setGuangdong(Integer guangdong) { 166 this.guangdong = guangdong; 167 } 168 169 public Integer getGuangxi() { 170 return guangxi; 171 } 172 173 public void setGuangxi(Integer guangxi) { 174 this.guangxi = guangxi; 175 } 176 177 public Integer getHainan() { 178 return hainan; 179 } 180 181 public void setHainan(Integer hainan) { 182 this.hainan = hainan; 183 } 184 185 public Integer getHenan() { 186 return henan; 187 } 188 189 public void setHenan(Integer henan) { 190 this.henan = henan; 191 } 192 193 public Integer getHunan() { 194 return hunan; 195 } 196 197 public void setHunan(Integer hunan) { 198 this.hunan = hunan; 199 } 200 201 public Integer getHubei() { 202 return hubei; 203 } 204 205 public void setHubei(Integer hubei) { 206 this.hubei = hubei; 207 } 208 209 public Integer getBeijing() { 210 return beijing; 211 } 212 213 public void setBeijing(Integer beijing) { 214 this.beijing = beijing; 215 } 216 217 public Integer getTianjin() { 218 return tianjin; 219 } 220 221 public void setTianjin(Integer tianjin) { 222 this.tianjin = tianjin; 223 } 224 225 public Integer getHebei() { 226 return hebei; 227 } 228 229 public void setHebei(Integer hebei) { 230 this.hebei = hebei; 231 } 232 233 public Integer getShanxi() { 234 return shanxi; 235 } 236 237 public void setShanxi(Integer shanxi) { 238 this.shanxi = shanxi; 239 } 240 241 public Integer getInnermongolia() { 242 return innermongolia; 243 } 244 245 public void setInnermongolia(Integer innermongolia) { 246 this.innermongolia = innermongolia; 247 } 248 249 public Integer getNingxia() { 250 return ningxia; 251 } 252 253 public void setNingxia(Integer ningxia) { 254 this.ningxia = ningxia; 255 } 256 257 public Integer getQinghai() { 258 return qinghai; 259 } 260 261 public void setQinghai(Integer qinghai) { 262 this.qinghai = qinghai; 263 } 264 265 public Integer getShaanxi() { 266 return shaanxi; 267 } 268 269 public void setShaanxi(Integer shaanxi) { 270 this.shaanxi = shaanxi; 271 } 272 273 public Integer getGansu() { 274 return gansu; 275 } 276 277 public void setGansu(Integer gansu) { 278 this.gansu = gansu; 279 } 280 281 public Integer getSinkiang() { 282 return sinkiang; 283 } 284 285 public void setSinkiang(Integer sinkiang) { 286 this.sinkiang = sinkiang; 287 } 288 289 public Integer getSichuan() { 290 return sichuan; 291 } 292 293 public void setSichuan(Integer sichuan) { 294 this.sichuan = sichuan; 295 } 296 297 public Integer getGuizhou() { 298 return guizhou; 299 } 300 301 public void setGuizhou(Integer guizhou) { 302 this.guizhou = guizhou; 303 } 304 305 public Integer getYunnan() { 306 return yunnan; 307 } 308 309 public void setYunnan(Integer yunnan) { 310 this.yunnan = yunnan; 311 } 312 313 public Integer getChongqing() { 314 return chongqing; 315 } 316 317 public void setChongqing(Integer chongqing) { 318 this.chongqing = chongqing; 319 } 320 321 public Integer getTibet() { 322 return tibet; 323 } 324 325 public void setTibet(Integer tibet) { 326 this.tibet = tibet; 327 } 328 329 public Integer getLiaoning() { 330 return liaoning; 331 } 332 333 public void setLiaoning(Integer liaoning) { 334 this.liaoning = liaoning; 335 } 336 337 public Integer getJilin() { 338 return jilin; 339 } 340 341 public void setJilin(Integer jilin) { 342 this.jilin = jilin; 343 } 344 345 public Integer getHeilongjiang() { 346 return heilongjiang; 347 } 348 349 public void setHeilongjiang(Integer heilongjiang) { 350 this.heilongjiang = heilongjiang; 351 } 352 353 public Integer getHongkong() { 354 return hongkong; 355 } 356 357 public void setHongkong(Integer hongkong) { 358 this.hongkong = hongkong; 359 } 360 361 public Integer getMacao() { 362 return macao; 363 } 364 365 public void setMacao(Integer macao) { 366 this.macao = macao; 367 } 368 369 public Integer getTaiwan() { 370 return taiwan; 371 } 372 373 public void setTaiwan(Integer taiwan) { 374 this.taiwan = taiwan; 375 } 376 377 public Integer getOnline() { 378 return online; 379 } 380 381 public void setOnline(Integer online) { 382 this.online = online; 383 } 384 385 public Integer getOutline() { 386 return outline; 387 } 388 389 public void setOutline(Integer outline) { 390 this.outline = outline; 391 } 392 393 public void addTibet() { 394 this.tibet = this.tibet + 1; 395 } 396 397 /** 398 * 山东+1 399 * 400 */ 401 public void addShandong() { 402 this.shandong = this.shandong + 1; 403 } 404 405 /** 406 * 江苏+1 407 * 408 */ 409 public void addJiangsu() { 410 this.jiangsu = this.jiangsu + 1; 411 } 412 413 /** 414 * 上海+1 415 * 416 */ 417 public void addShanghai() { 418 this.shanghai = this.shanghai + 1; 419 } 420 421 /** 422 * 浙江+1 423 * 424 */ 425 public void addZhejiang() { 426 this.zhejiang = this.zhejiang + 1; 427 } 428 429 /** 430 * 安徽+1 431 * 432 */ 433 public void addAnhui() { 434 this.anhui = this.anhui + 1; 435 } 436 437 /** 438 * 福建+1 439 * 440 */ 441 public void addFujian() { 442 this.fujian = this.fujian + 1; 443 } 444 445 /** 446 * 江西+1 447 * 448 */ 449 public void addJiangxi() { 450 this.jiangxi = this.jiangxi + 1; 451 } 452 453 /** 454 * 广东+1 455 * 456 */ 457 public void addGuangdong() { 458 this.guangdong = this.guangdong + 1; 459 } 460 461 /** 462 * 广西+1 463 * 464 */ 465 public void addGuangxi() { 466 this.guangxi = this.guangxi + 1; 467 } 468 469 /** 470 * 海南+1 471 * 472 */ 473 public void addHainan() { 474 this.hainan = this.hainan + 1; 475 } 476 477 /** 478 * 河南+1 479 * 480 */ 481 public void addHenan() { 482 this.henan = this.henan + 1; 483 } 484 485 /** 486 * 湖南+1 487 * 488 */ 489 public void addHunan() { 490 this.hunan = this.hunan + 1; 491 } 492 493 /** 494 * 湖北+1 495 * 496 */ 497 public void addHubei() { 498 this.hubei = this.hubei + 1; 499 } 500 501 /** 502 * 北京+1 503 * 504 */ 505 public void addBeijing() { 506 this.beijing = this.beijing + 1; 507 } 508 509 /** 510 * 天津+1 511 * 512 */ 513 public void addTianjin() { 514 this.tianjin = this.tianjin + 1; 515 } 516 517 /** 518 * 河北+1 519 * 520 */ 521 public void addHebei() { 522 this.hebei = this.hebei + 1; 523 } 524 525 /** 526 * 山西+1 527 * 528 */ 529 public void addShanxi() { 530 this.shanxi = this.shanxi + 1; 531 } 532 533 /** 534 * 陕西+1 535 * 536 */ 537 public void addShaanxi() { 538 this.shaanxi = this.shaanxi + 1; 539 } 540 541 /** 542 * 内蒙古+1 543 * 544 */ 545 public void addInnermongolia() { 546 this.innermongolia = this.innermongolia + 1; 547 } 548 549 /** 550 * 宁夏+1 551 * 552 */ 553 public void addNingxia() { 554 this.ningxia = this.ningxia + 1; 555 } 556 557 /** 558 * 青海+1 559 * 560 */ 561 public void addQinghai() { 562 this.qinghai = this.qinghai + 1; 563 } 564 565 /** 566 * 甘肃+1 567 * 568 */ 569 public void addGansu() { 570 this.gansu = this.gansu + 1; 571 } 572 573 /** 574 * 新疆+1 575 * 576 */ 577 public void addSinkiang() { 578 this.sinkiang = this.sinkiang + 1; 579 } 580 581 /** 582 * 四川+1 583 * 584 */ 585 public void addSichuan() { 586 this.sichuan = this.sichuan + 1; 587 } 588 589 /** 590 * 贵州+1 591 * 592 */ 593 public void addGuizhou() { 594 this.guizhou = this.guizhou + 1; 595 } 596 597 /** 598 * 云南+1 599 * 600 */ 601 public void addYunnan() { 602 this.yunnan = this.yunnan + 1; 603 } 604 605 /** 606 * 重庆+1 607 * 608 */ 609 public void addChongqing() { 610 this.chongqing = this.chongqing + 1; 611 } 612 613 /** 614 * 辽宁+1 615 * 616 */ 617 public void addLiaoning() { 618 this.liaoning = this.liaoning + 1; 619 } 620 621 /** 622 * 吉林+1 623 * 624 */ 625 public void addJilin() { 626 this.jilin = this.jilin + 1; 627 } 628 629 /** 630 * 黑龙江+1 631 * 632 */ 633 public void addHeilongjiang() { 634 this.heilongjiang = this.heilongjiang + 1; 635 } 636 637 /** 638 * 香港+1 639 * 640 */ 641 public void addHongkong() { 642 this.hongkong = this.hongkong + 1; 643 } 644 645 /** 646 * 澳门+1 647 * 648 */ 649 public void addMacao() { 650 this.macao = this.macao + 1; 651 } 652 653 /** 654 * 台湾+1 655 * 656 */ 657 public void addTaiwan() { 658 this.taiwan = this.taiwan + 1; 659 } 660 }
service层逻辑
// 从数据库中取出插座统计记录,该表中只有一条记录,如有变化直接更新就是 SocketStatistics socketStatistics = socketStatisticsMapper.selectOne(); // 根据前端传过来的插座所属省份中文名,到省份枚举类中得到对应的英文名 // 再通过拼接的方式得到对应的方法名 String methodName = "add" + ProvinceEnum.getMsgByCode(socket.getProvince()); // 到SocketStatistics类中找到这个方法 Method method = SocketStatistics.class.getDeclaredMethod(methodName); // Guava的工具类通过反射,动态调用该方法 Invokable<SocketStatistics, Object> invokable = (Invokable<SocketStatistics, Object>) Invokable .from(method); invokable.invoke(socketStatistics); // 数据处理完后,更新数据库 socketStatisticsMapper.updateByPrimaryKeySelective(socketStatistics);