Tags:

Cách sử dụng @AllArgsConstructor của Lombok

Sau phần giới thiệu về @NoArgsConstructor@RequiredArgsConstructor bài viết này chúng ta sẽ đi đến annotation cuối cùng được Lombok cung cấp để sinh constructor tự động. @AllArgsConstructor sẽ sinh constructor với tất cả các tham số cho các thuộc tính của class.

Để sử dụng Lombok trong project Maven, chúng ta phải thêm dependency:

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

@AllArgsConstructor trong Lombok

Mặc định constructor được sinh ra là public access modifiers.

@AllArgsConstructor
public class AllArgsDemo1 {
  private Long id;
  
  private String username;
}

// Code được sinh tương ứng

public class AllArgsDemo1 {
  private Long id;
  private String username;
  public AllArgsDemo1(final Long id, final String username) {
    this.id = id;
    this.username = username;
  }
}

@AllArgsConstructor với non-null fields

Các thuộc tính được đánh dấu với @NonNull annotation sẽ được kiểm tra #null khi constructor được sinh ra.

@AllArgsConstructor
public class AllArgsDemo2 {
  private Long id;
  
  @NonNull
  private String username;
}

// Code được sinh tương ứng

public class AllArgsDemo2 {
  private Long id;
  @NonNull
  private String username;
  public AllArgsDemo2(final Long id, @NonNull final String username) {
    if (username == null) {
      throw new NullPointerException("username is marked non-null but is null");
    }
    this.id = id;
    this.username = username;
  }
}

@AllArgsConstructor với static và final fields

  1. Lombok không bao giờ sinh tham số trong constructor với thuộc tính static đối với @AllArgsConstructor.
  2. @AllArgsConstructor cũng không sinh tham số cho các thuộc tính final nếu chúng đã được khởi tạo giá trị, ngược lại một tham số trong constructor sẽ được tạo cho nó.
@AllArgsConstructor
public class AllArgsDemo3 {
  private Long id;
  
  private static boolean defaultStatus;
  
  private final double minSalary = 10000.00;
  
  private final int defaultRole;
}

// Code được sinh tương ứng

public class AllArgsDemo3 {
  private Long id;
  private static boolean defaultStatus;
  private final double minSalary = 10000.0;
  private final int defaultRole;
  public AllArgsDemo3(final Long id, final int defaultRole) {
    this.id = id;
    this.defaultRole = defaultRole;
  }
}

Tạo private constructor với @AllArgsConstructor

Mặc định thì constructor được sinh ra bởi @AllArgsConstructorpublic, để làm cho nó trở thành private chúng ta có thể khai báo thêm thuộc tính @RequiredArgsConstructor(access = AccessLevel.PRIVATE).

@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class AllArgsDemo4 {
  private Long id;
  
  private String username;
  
}

// Code được sinh tương ứng

public class AllArgsDemo4 {
  private Long id;
  private String username;
  private AllArgsDemo4(final Long id, final String username) {
    this.id = id;
    this.username = username;
  }
}

Khởi tạo một static factory method với @AllAgrsConstructor

@AllArgsConstructor(staticName = “getInstance”) sẽ sinh ra một static factory method có tên getInstance

@AllArgsConstructor(staticName = "getInstance")
public class AllArgsDemo5 {
  private Long id;
  
  private String username;
  
}

// Code được sinh tương ứng

public class AllArgsDemo5 {
  private Long id;
  private String username;
  private AllArgsDemo5(final Long id, final String username) {
    this.id = id;
    this.username = username;
  }
  public static AllArgsDemo5 getInstance(final Long id, final String username) {
    return new AllArgsDemo5(id, username);
  }
}

Tóm lược

Như vậy là mình đã giới thiệu xong một số cách dùng @AllArgsConstructor của Lombok rồi, thường thì mình chỉ sử dụng mặc định thôi chứ cũng ít khi dùng đến static factory, hay private constructor. Thế nhưng cứ hãy tìm hiểu hết đi, biết đâu sau này lại cần dùng.

Nguồn tham thảo

https://javabydeveloper.com/lombok-allargsconstructor-examples/

https://projectlombok.org/features/constructor

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x