Backend/Java

[Lombok] Constructor Annotation 정리

김룹 2024. 1. 5. 13:19

Lombok에서는 생성자를 생성해주는 기능을 사용할 수 있도록 다음과 같은 어노테이션을 제공해준다.

@NoArgsConstructor 기본 생성자 만드는 데 사용
@AllArgsConstructor 모든 필드의 생성자 만드는 데 사용
@RequiredArgsConstructor 필수 생성자 만드는 데 사용

 

각각의 기본 사용법에 대해 알아볼 예정이다!

 

 

 @NoArgsConstructor

📌 기본 사용법

@NoArgsConstructor
public class BookWithLombok {

    private Long id;
    private String isbn;
    private String name;
    private String author;
}

 

 

📌 자바로 표현했을 때

public class BookWithOutLombok {

    private Long id;
    private String isbn;
    private String name;
    private String author;

    public BookWithOutLombok() {

    }
}

 

 

 

 

 @AllArgsConstructor

클래스에 존재하는 모든 필드에 대한 생성자를 자동으로 생성 

 

📌 기본 사용법

@AllArgsConstructor
public class BookWithLombok {

    private Long id;
    private String isbn;
    private String name;
    private String author;
    private boolean useYn;
}

 

 

📌 자바로 표현했을 때

public class BookWithLombok {
    private Long id;
    private String isbn;
    private String name;
    private String author;
    private boolean useYn;

    public BookWithLombok(final Long id, final String isbn, final String name, final String author, final boolean useYn) {
        this.id = id;
        this.isbn = isbn;
        this.name = name;
        this.author = author;
        this.useYn = useYn;
    }
}

 

 

 

 

 @RequiredConstructor

초기화되지 않은 모든 final 필드, @NonNull로 되어있는 필드들에 대한 생성자를 자동으로 생성한다! 

💡 주의할 점
파라미터의 순서는 클래스에 있는 필드 순서에 맞춰서 생성자 생성
@NonNull 필드들은 null-check가 추가적으로 생성되며, @NonNull이 마크되어 있어도 파라미터에서 null 값이 들어오면 생성자에서 NullPointerException이 발생한다.

 

 

📌 기본 사용법

@RequiredArgsConstructor
public class BookWithLombok {

    private final Long id;
    private final String isbn;
    private final String name;
    private final String author;
    private boolean useYn;
}

 

 

📌 자바로 표현했을 때

public class BookWithLombok {
    private final Long id;
    private final String isbn;
    private final String name;
    private final String author;
    private boolean useYn;

    public BookWithLombok(final Long id, final String isbn, final String name, final String author) {
        this.id = id;
        this.isbn = isbn;
        this.name = name;
        this.author = author;
    }
}

 

 

 

 

 

 

 세부 옵션들

staticName 정적 팩토리 메소드 생성
access 접근제한자 지정
onConstructor 생성자의 어노테이션 지정
force (@NoArgsConstructor만 해당) final 필드가 선언된 경우, 컴파일 타임에 기본값을 0 / null / false 로 설정

 

 

 

staticName

📌 기본 사용법

@NoArgsConstructor(staticName = "of")
public class BookWithLombok {

    private Long id;
    private String isbn;
    private String name;
    private String author;
}

 

 

📌 자바로 표현했을 때

public class BookWithOutLombok { 
    private Long id; 
    private String isbn; 
    private String name; 
    private String author; 

    private BookWithOutLombok() { 
    } 

    public static BookWithOutLombok of() { 
        return new BookWithOutLombok(); 
    } 
}

정적 팩토리 메소드인 of를 생성하고 내부 기본 생성자 생성을 위해 private으로 호출하게 된다! 

 

 

 

access

생성자에 대해 접근제한자 지정 가능! 기본 접근제한자는 public이다. 

접근제한자 목록은 다음과 같다

PUBLIC 모든 곳에서 접근 가능
MODULE 같은 패키지 내에서 접근 가능
PROTECTED 같은 패키지 또는 자식 클래스에서 사용 가능
PACKAGE 같은 패키지 내에서 접근 가능
PRIVATE 내부 클래스에서만 사용 가능
NONE 모든 곳에서 접근 가능

 

 

PUBLIC, NONE

@NoArgsConstructor(access = AccessLevel.PUBLIC)
@NoArgsConstructor(access = AccessLevel.NONE)
public class BookWithLombok {
    private Long id;
    private String isbn;
    private String name;
    private String author;

    public BookWithLombok() {
    }
}

 

 

 

 MODULE, PACKAGE

@NoArgsConstructor(access = AccessLevel.MODULE)
@NoArgsConstructor(access = AccessLevel.PACKAGE)
public class BookWithLombok {
    private Long id;
    private String isbn;
    private String name;
    private String author;

    BookWithLombok() {
    }
}

 

 

 

 PROTECTED

@NoArgsConstructor(access = AccessLevel.PROCTECTED)
public class BookWithLombok {
    private Long id;
    private String isbn;
    private String name;
    private String author;

    protected BookWithLombok() {
    }
}

 

 

 

 PRIVATE

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class BookWithLombok {
    private Long id;
    private String isbn;
    private String name;
    private String author;

    private BookWithLombok() {
    }
}