안녕하세요! Spring Boot와 Lombok을 함께 사용할 때 유용한 Lombok 어노테이션들에 대해 알아보겠습니다.
Lombok은 코드의 반복을 줄여주고, 생산성을 높여주는 라이브러리로, Spring Boot와 함께 사용하면 매우 편리합니다.
1. @Getter와 @Setter
가장 많이 사용되는 어노테이션입니다. 필드에 대한 getter와 setter 메서드를 자동으로 생성해줍니다.
사용한 경우
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class User {
private String name;
private int age;
}
사용하지 않은 경우
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2. @ToString
객체의 toString() 메서드를 자동으로 생성해줍니다. 필드 값을 문자열로 쉽게 확인할 수 있습니다.
사용한 경우
import lombok.ToString;
@ToString
public class User {
private String name;
private int age;
}
사용하지 않은 경우
public class User {
private String name;
private int age;
@Override
public String toString() {
return "User{name='" + name + "', age=" + age + "}";
}
}
3. @NoArgsConstructor, @AllArgsConstructor
기본 생성자와 모든 필드를 매개변수로 받는 생성자를 자동으로 생성해줍니다.
사용한 경우
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String name;
private int age;
}
사용하지 않은 경우
public class User {
private String name;
private int age;
// 기본 생성자 (@NoArgsConstructor)
public User() {
}
// 모든 필드를 매개변수로 받는 생성자 (@AllArgsConstructor)
public User(String name, int age) {
this.name = name;
this.age = age;
}
}
4. @Data
@Getter, @Setter, @ToString, @EqualsAndHashCode, @RequiredArgsConstructor를 한 번에 설정해주는 어노테이션입니다.
사용한 경우
import lombok.Data;
@Data
public class User {
private String name;
private int age;
}
사용하지 않은 경우
import java.util.Objects;
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{name='" + name + "', age=" + age + "}";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return age == user.age && Objects.equals(name, user.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
5. @Builder
빌더 패턴을 쉽게 구현할 수 있게 해줍니다.
사용한 경우
import lombok.Builder;
@Builder
public class User {
private String name;
private int age;
}
// 사용 예시
User user = User.builder()
.name("John")
.age(30)
.build();
사용하지 않은 경우
public class User {
private String name;
private int age;
private User(Builder builder) {
this.name = builder.name;
this.age = builder.age;
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String name;
private int age;
public Builder name(String name) {
this.name = name;
return this;
}
public Builder age(int age) {
this.age = age;
return this;
}
public User build() {
return new User(this);
}
}
}
// 사용 예시
User user = User.builder()
.name("John")
.age(30)
.build();
6. @Value
불변 클래스를 만들 수 있도록 해줍니다. 모든 필드는 private final이 되고, getter 메서드만 생성됩니다.
사용한 경우
import lombok.Value;
@Value
public class User {
String name;
int age;
}
사용하지 않은 경우
public class User {
private final String name;
private final int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
7. @NonNull
필드나 메서드 매개변수가 null이 아니어야 함을 명시해줍니다.
사용한 경우
import lombok.NonNull;
public class User {
private String name;
public User(@NonNull String name) {
this.name = name;
}
}
사용하지 않은 경우
public class User {
private String name;
public User(String name) {
if (name == null) {
throw new IllegalArgumentException("name cannot be null");
}
this.name = name;
}
}
8. @Slf4j
로깅을 쉽게 할 수 있도록 해줍니다. org.slf4j.Logger를 자동으로 생성해줍니다.
사용한 경우
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class UserService {
public void createUser(String name) {
log.info("Creating user: {}", name);
}
}
사용하지 않은 경우
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UserService {
private static final Logger log = LoggerFactory.getLogger(UserService.class);
public void createUser(String name) {
log.info("Creating user: {}", name);
}
}
[Reference]
Lombok 사용 하기 : 네이버 블로그 (https://blog.naver.com/rlagyska3319/222067004672?viewType=pc)
[ lombok, springboot ] 스프링부트에 lombok 롬복 설정방법 (https://dion-ko.tistory.com/52)
[Java, Spring] Lombok(롬복) - 사용법 설명 및 예제 소스 다운로드 (https://niceman.tistory.com/99)
[Spring] Spring Boot 시작하기 (3) - Lombok 사용 기본 (https://dev-overload.tistory.com/26)
'Spring > Spring Boot' 카테고리의 다른 글
[Spring Boot] Flyway를 이용한 DB 형상관리 (0) | 2024.06.26 |
---|---|
[Spring Boot]스프링 부트에서 ddl-auto 사용 방법 (0) | 2024.06.03 |
[Spring] Path Variable vs Query Parameter (0) | 2024.03.29 |
[Spring Boot] Access Token & Refresh Token (0) | 2024.02.20 |
[Spring Boot] JWT(Json Web Token) - 웹의 안전한 인증 방식 (0) | 2024.02.20 |