成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

怎么在SpringBoot中實(shí)現(xiàn)Lombok加持-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)怎么在SpringBoot中實(shí)現(xiàn)Lombok加持,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

鹽湖網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),鹽湖網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為鹽湖上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的鹽湖做網(wǎng)站的公司定做!

IntelliJ IDEA上配置

方法一:直接在IDEA界面中配置

首先進(jìn)入Plugins界面:

怎么在SpringBoot中實(shí)現(xiàn)Lombok加持

然后搜索并安裝Lombok插件:

怎么在SpringBoot中實(shí)現(xiàn)Lombok加持

最后不要忘了開(kāi)啟Annotation Processors的Enable選項(xiàng):

怎么在SpringBoot中實(shí)現(xiàn)Lombok加持

上述安裝完成以后需要重啟IDEA生效!

方法二:手動(dòng)下載Lombok插件安裝

有時(shí)由于網(wǎng)絡(luò)原因,上面方法一這種方式安裝失敗,因此只能手動(dòng)下載安裝

下載lombok插件:

https://github.com/mplushnikov/lombok-intellij-plugin/releases

Plugins -> Install plugin from disk... 選擇下載的zip包安裝

怎么在SpringBoot中實(shí)現(xiàn)Lombok加持

重啟idea即可

怎么在SpringBoot中實(shí)現(xiàn)Lombok加持

IDE中設(shè)置完成以后需要在pom.xml中添加如下所示的lombok依賴(lài)才能使用

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.16.16</version>
</dependency>

Lombok主要注解

  1. @Getter and @Setter / 自動(dòng)為屬性提供 Set和Get 方法

  2. @ToString / 該注解的作用是為類(lèi)自動(dòng)生成toString()方法

  3. @EqualsAndHashCode / 為對(duì)象字段自動(dòng)生成hashCode和equals實(shí)現(xiàn)

  4. @AllArgsConstructor, @RequiredArgsConstructor and @NoArgsConstructor / 顧名思義,為類(lèi)自動(dòng)生成對(duì)應(yīng)參數(shù)的constructor

  5. @Log, @Log4j, @Log4j2, @Slf4j, @XSlf4j, @CommonsLog, @JBossLog / 自動(dòng)為類(lèi)添加對(duì)應(yīng)的log支持

  6. @Data / 自動(dòng)為所有字段添加@ToString, @EqualsAndHashCode, @Getter,為非final字段添加@Setter,和@RequiredArgsConstructor,本質(zhì)上相當(dāng)于幾個(gè)注解的綜合效果

  7. @NonNull / 自動(dòng)幫助我們避免空指針。作用在方法參數(shù)上的注解,用于自動(dòng)生成空值參數(shù)檢查

  8. @Cleanup / 自動(dòng)幫我們調(diào)用close()方法。作用在局部變量上,在作用域結(jié)束時(shí)會(huì)自動(dòng)調(diào)用close方法釋放資源

下文就Lombok中用的最為頻繁的@Data@Log注解進(jìn)行代碼實(shí)戰(zhàn)!

@Data注解使用

官網(wǎng)關(guān)于@Data注解的解釋如下:

All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, @Setter on all non-final fields, and @RequiredArgsConstructor!

不難理解,其可以看成是多個(gè)Lombok注解的集成,因此使用很方便!

先來(lái)創(chuàng)建一個(gè)POJO實(shí)體UserLombok,普通的寫(xiě)法如下:

public class UserLombok {
 private final String name;
 private int age;
 private double score;
 private String[] tags;
 
 public UserLombok(String name) {
  this.name = name;
 }
 
 public String getName() {
  return this.name;
 }
 
 void setAge(int age) {
  this.age = age;
 }
 
 public int getAge() {
  return this.age;
 }
 
 public void setScore(double score) {
  this.score = score;
 }
 
 public double getScore() {
  return this.score;
 }
 
 public String[] getTags() {
  return this.tags;
 }
 
 public void setTags(String[] tags) {
  this.tags = tags;
 }
 
 @Override public String toString() {
  return "DataExample(" + this.getName() + ", " + this.getAge() + ", " + this.getScore() + ", " + Arrays.deepToString(this.getTags()) + “)”;
 }
 
 protected boolean canEqual(Object other) {
  return other instanceof DataExample;
 }
 
 @Override public boolean equals(Object o) {
  if (o == this) return true;
  if (!(o instanceof DataExample)) return false;
  DataExample other = (DataExample) o;
  if (!other.canEqual((Object)this)) return false;
  if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;
  if (this.getAge() != other.getAge()) return false;
  if (Double.compare(this.getScore(), other.getScore()) != 0) return false;
  if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;
  return true;
 }
 
 @Override public int hashCode() {
  final int PRIME = 59;
  int result = 1;
  final long temp1 = Double.doubleToLongBits(this.getScore());
  result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
  result = (result*PRIME) + this.getAge();
  result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32));
  result = (result*PRIME) + Arrays.deepHashCode(this.getTags());
  return result;
 }
}

Lombok加持后,寫(xiě)法可簡(jiǎn)化為:

@Data
public class UserLombok {
  private final String name;
  private int age;
  private double score;
  private String[] tags;
}

在IDEA中使用時(shí),Lombok的注解會(huì)自動(dòng)補(bǔ)全,如下圖所示:

怎么在SpringBoot中實(shí)現(xiàn)Lombok加持

我們來(lái)寫(xiě)POJO的測(cè)試代碼

public static void main( String[] args ) {
    UserLombok userLombok = new UserLombok("hansonwang99”);
    userLombok.setAge(18);
    String[] array = new String[]{"apple","juice”};
    userLombok.setTags( array );
    userLombok.setScore( 99.0 );
    System.out.println(userLombok);
  }

由下圖我們可以看到IDEA依然可以自動(dòng)為我們補(bǔ)全由Lombok自動(dòng)生成的代碼:

怎么在SpringBoot中實(shí)現(xiàn)Lombok加持

結(jié)果打印

由于Lombok為我們自動(dòng)生成了toString方法,因此對(duì)象的打印結(jié)果如下:

UserLombok(name=hansonwang99, age=18, score=99.0, tags=[apple, juice])

@Log注解實(shí)戰(zhàn)

在我的文章 Spring Boot日志框架實(shí)踐 一文中,我們使用Log4j2來(lái)作為日志對(duì)象,其寫(xiě)法如下:

@RestController
@RequestMapping("/testlogging”)
public class LoggingTestController {

  private final Logger logger = LogManager.getLogger(this.getClass());

  @GetMapping("/hello”)
  public String hello() {
    for(int i=0;i<10_0000;i++){
      logger.info("info execute index method”);
      logger.warn("warn execute index method”);
      logger.error("error execute index method”);

    }

    return "My First SpringBoot Application”;
  }
}

若改用Lombok后,寫(xiě)法變得更加簡(jiǎn)潔,我們只需要引入對(duì)應(yīng)的@Log注解即可完成log對(duì)象的生成:

@RestController
@RequestMapping("/testloggingwithlombok”)
@Log4j2
public class LoggingTestControllerLombok {

  @GetMapping("/hello”)
  public String hello() {
    for(int i=0;i<10_0000;i++){
      log.info("info execute index method”);
      log.warn("warn execute index method”);
      log.error("error execute index method”);

    }

    return "My First SpringBoot Application”;
  }
}

關(guān)于怎么在SpringBoot中實(shí)現(xiàn)Lombok加持就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

網(wǎng)頁(yè)題目:怎么在SpringBoot中實(shí)現(xiàn)Lombok加持-創(chuàng)新互聯(lián)
分享URL:http://jinyejixie.com/article40/dsepeo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷(xiāo)推廣、關(guān)鍵詞優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、ChatGPT、手機(jī)網(wǎng)站建設(shè)網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化
南江县| 习水县| 马鞍山市| 建阳市| 丰城市| 友谊县| 平顺县| 辽源市| 弥渡县| 白银市| 政和县| 娄烦县| 宜城市| 五河县| 天津市| 浏阳市| 宜都市| 会宁县| 如皋市| 武夷山市| 青海省| 岐山县| 安图县| 京山县| 和平县| 色达县| 自治县| 高雄市| 合水县| 瓦房店市| 鄂伦春自治旗| 五大连池市| 德兴市| 武平县| 喀喇| 新晃| 中山市| 南康市| 深水埗区| 津南区| 上高县|