Lombok은 컴파일 시점에 코드를 자동 생성합니다. 우리 눈에는 어노테이션만 보이지만, 실제로 컴파일된 .class 파일에는 getter, setter, 생성자가 전부 들어있습니다.
IntelliJ에서 Lombok이 생성한 코드를 확인하는 방법이 있습니다. 클래스 파일을 열고 Navigate → File Structure (단축키: Ctrl+F12 또는 Cmd+F12)를 누르면, Lombok이 만들어준 메서드 목록이 보입니다.
가장 기본적인 어노테이션입니다.
@Getter
@Setter
public class Post {
private Long id;
private String title;
private String content;
}
컴파일하면 이 코드가 됩니다:
public class Post {
private Long id;
private String title;
private String content;
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; }
}
클래스에 붙이면 모든 필드에 적용되고, 필드에 붙이면 해당 필드에만 적용됩니다:
@Getter // 모든 필드에 Getter 생성
public class Post {
private Long id;
private String title;
@Setter // content 필드에만 Setter 생성
private String content;
}
@Setter를 클래스 전체에 붙이는 건 권장하지 않습니다. 아무 데서나 값을 바꿀 수 있으면 버그 추적이 어려워집니다.
// ❌ 이러면 어디서든 title을 바꿀 수 있음
post.setTitle("아무거나");
// ✅ 의미 있는 메서드를 만들어서 변경
public void updateTitle(String newTitle) {
if (newTitle == null || newTitle.isBlank()) {
throw new IllegalArgumentException("제목은 비어있을 수 없습니다");
}
this.title = newTitle;
}
정리하면:
@Getter → 거의 항상 사용. 문제 없음@Setter → Entity에는 지양. DTO에서는 필요할 때만 사용