[Spring Boot] Lombok

SeongWon Ohยท2021๋…„ 9์›” 13์ผ
1

Spring Framework

๋ชฉ๋ก ๋ณด๊ธฐ
12/33
post-thumbnail

๐Ÿ“ŒLombok์ด๋ž€?

Java๋กœ ๊ฐœ๋ฐœ์„ ํ•˜๋‹ค๋ณด๋ฉด getter, setter, constructor์ž‘์„ฑ ๋“ฑ์˜ ๋งŽ์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผํ•œ๋‹ค. Lombok์€ ์ด๋Ÿฌํ•œ ๊ธฐ๊ณ„์ ์ธ ์ฝ”๋“œ๋“ค์„ ์–ด๋…ธํ…Œ์ด์…˜์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž๋™ํ™”ํ•˜์—ฌ ์ž‘์„ฑํ•ด์ฃผ๋Š” Java์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

  • Lombok์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ์˜ ๊ธธ์ด๋ฅผ ์ค„์—ฌ ์ฝ”๋“œ๋ฅผ ๋‹จ์ˆœํ™”ํ•˜๊ณ  ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ํ‚ค์šธ ์ˆ˜ ์žˆ๋‹ค.
  • ์ฝ”๋“œ ์ž๋™ ์ƒ์„ฑ์„ ํ†ตํ•ด ์ƒ์‚ฐ์„ฑ์ด ํ–ฅ์ƒ๋œ๋‹ค.
  • ๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ์˜ ๋‹ค์ด์–ดํŠธ๋ฅผ ํ†ตํ•œ ๊ฐ€๋…์„ฑ ๋ฐ ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚จ๋‹ค.

Lombok์‚ฌ์šฉ ์„ค์ •

์ €๋Š” IntelliJํ™˜๊ฒฝ์—์„œ ํ•™์Šต์„ ํ•˜๊ณ  ์žˆ๊ธฐ์— IntelliJ๊ธฐ์ค€์œผ๋กœ ์„ค๋ช…์„ ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

IntelliJ์—์„œ Lombok์„ ์‚ฌ์šฉํ•  ํ™˜๊ฒฝ์„ ๋งŒ๋“ค๋ ค๋ฉด ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์‹œ์— ์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด Developer Tools์— ์œ„์น˜ํ•œ Lombok์„ ์„ ํƒํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์‹œ์— ์ถ”๊ฐ€๋ฅผ ํ•˜์ง€ ๋ชปํ•˜์˜€์„ ๊ฒฝ์šฐ build.gradle์˜ dependencies์— ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ ํ›„ dependency update๋ฅผ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

๐Ÿ“ŒLombok์‚ฌ์šฉ ์˜ˆ์ œ

@Getter, @Setter

public class User {
    private String name;
    private String email;
    private LocalDateTime createdAt; // ์ƒ์„ฑ๋œ ์‹œ๊ฐ„
    private LocalDateTime updatedAt; // ์—…๋ฐ์ดํŠธ๋œ ์‹œ๊ฐ„

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
}

Lombok์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์šฐ๋ฆฌ๋Š” User class์—์„œ getter์™€ setter ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„์™€ ๊ฐ™์ด ๋ณ€์ˆ˜ ํ•˜๋‚˜์”ฉ ์„ ์–ธ์„ ํ•ด์ค˜์•ผํ•œ๋‹ค. ์œ„์˜ User class์—์„œ๋Š” ๋ณ€์ˆ˜๊ฐ€ 4๊ฐœ๋ฐ–์— ์—†์–ด๋„ getter, setter๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ์—๋งŒ 8๊ฐœ์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ๋งŒ๋“ค์–ด์กŒ์œผ๋‚˜ ์‹ค์ œ DTO๋ฅผ ์ƒ์„ฑํ•˜๋‹ค๋ณด๋ฉด ํ›จ์”ฌ ๋” ๋งŽ์€ ๋ณ€์ˆ˜๋“ค์„ ๋งŒ๋“ค๊ฒŒ ๋  ๊ฒƒ์ด๊ณ  ๊ทธ์— ๋”ฐ๋ผ ์„ ์–ธํ•˜๋Š” ๋ฉ”์„œ๋“œ์˜ ์ˆ˜๋„ ํฌ๊ฒŒ ๋Š˜์–ด๋‚  ๊ฒƒ์ด๋‹ค.

์ด๋Ÿฌํ•œ ์ฝ”๋“œ๋“ค์€ Lombok์˜ @Getter, @Setter ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๋ฅผ ๋‹จ์ˆœํ™” ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

@Getter
public class User {
    
    @Setter
    private String name;
    
    private String email;
    private LocalDateTime createdAt; // ์ƒ์„ฑ๋œ ์‹œ๊ฐ„
    private LocalDateTime updatedAt; // ์—…๋ฐ์ดํŠธ๋œ ์‹œ๊ฐ„
}

@Getter, @Setter๋Š” class์ „์ฒด์— ํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ ํŠน์ • ๋ณ€์ˆ˜์—๋งŒ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
ํŠน์ • ๋ณ€์ˆ˜์— ๋Œ€ํ•ด์„œ๋งŒ ์ง€์ •ํ•  ๊ฒฝ์šฐ ์œ„์˜ name์œ„์— ์œ„์น˜ํ•œ @Setter์ฒ˜๋Ÿผ ๋ณ€์ˆ˜ ์œ„์— ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค.


@ToString

public class User {
    private String name;
    private String email;
    private LocalDateTime createdAt; // ์ƒ์„ฑ๋œ ์‹œ๊ฐ„
    private LocalDateTime updatedAt; // ์—…๋ฐ์ดํŠธ๋œ ์‹œ๊ฐ„

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", createdAt=" + createdAt +
                ", updatedAt=" + updatedAt +
                '}';
    }
}

toString์˜ ๊ฒฝ์šฐ๋„ Overriding์„ ํ•˜๋ฉฐ ์ƒˆ๋กœ ์ง€์ •ํ•ด์•ผํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ์—ˆ์œผ๋‚˜ Lombok์„ ์‚ฌ์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด @ToString ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ž„์œผ๋กœ์„œ ๋‹จ์ˆœํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
@ToString(exclude = "regionMoneyName")์™€ ๊ฐ™์ด ํŠน์ • ๋ณ€์ˆ˜๋ฅผ ์ œ์™ธ์‹œํ‚ฌ ์ˆ˜๋„ ์žˆ๋‹ค.

@ToString
public class User {
    private String name;
    private String email;
    private LocalDateTime createdAt; // ์ƒ์„ฑ๋œ ์‹œ๊ฐ„
    private LocalDateTime updatedAt; // ์—…๋ฐ์ดํŠธ๋œ ์‹œ๊ฐ„

}

@NoArgsConstructor

parameter์—†์ด ์ƒ์„ฑ๋˜๋Š” ๊ธฐ๋ณธ constructor๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋Š” annotation์ด๋‹ค.

@NoArgsConstructor
public class User {
    private String name;
    private String email;
    private LocalDateTime createdAt; // ์ƒ์„ฑ๋œ ์‹œ๊ฐ„
    private LocalDateTime updatedAt; // ์—…๋ฐ์ดํŠธ๋œ ์‹œ๊ฐ„

    // public User() { }  ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ์ƒ๋žต๋œ๋‹ค.
}


User user = new User();

@AllArgsConstructor

@NoArgsConstructor๊ฐ€ ์•„๋ฌด๋Ÿฐ ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์ง€ ์•Š๋Š” Constructor๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” Annotation์ด์—ˆ๋‹ค๋ฉด @AllArgsConstructor์€ ๊ฐ์ฒด๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ชจ๋“  ๋ณ€์ˆ˜๋ฅผ ๋ฐ›๋Š” constructor๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

@AllArgsConstructor
public class User {
    private String name;
    private String email;
    private LocalDateTime createdAt; // ์ƒ์„ฑ๋œ ์‹œ๊ฐ„
    private LocalDateTime updatedAt; // ์—…๋ฐ์ดํŠธ๋œ ์‹œ๊ฐ„

//    ์•„๋ž˜์˜ constructor๋ฅผ @AllArgsConstructor์ด ๋Œ€์‹  ๋งŒ๋“ค์–ด์ค€๋‹ค.
//    public User(String name, String email, LocalDateTime createdAt, LocalDateTime updatedAt) {
//        this.name = name;
//        this.email = email;
//        this.createdAt = createdAt;
//        this.updatedAt = updatedAt;
//    }
}

@RequiredArgsConstructor, @NonNull

๊ผญ ํ•„์š”ํ•œ ๋ณ€์ˆ˜๋งŒ์„ ์‚ฌ์šฉํ•˜๋Š” construtor๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” annotation์ด๋‹ค. ํ•„์ˆ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜์•ผํ•˜๋Š” ๋ณ€์ˆ˜๋“ค์€ @NonNull์„ ๋ถ™์—ฌ์„œ ์„ค์ •ํ•œ๋‹ค. ์•„๋‹ˆ๋ฉด ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ final๋กœ ์„ ์–ธํ•ด๋„ ์˜์กด์„ฑ์„ ์ฃผ์ž…๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

@RequiredArgsConstructor
public class User {
    @NonNull // @RequiredArgsConstructor์—์„œ ์‚ฌ์šฉ๋  field๋“ค์„ ์ง€์ •
    private String name;
    
    @NonNull
    private String email;
    
    private LocalDateTime createdAt; // ์ƒ์„ฑ๋œ ์‹œ๊ฐ„
    private LocalDateTime updatedAt; // ์—…๋ฐ์ดํŠธ๋œ ์‹œ๊ฐ„


//    public User(String name, String email) {
//        this.name = name;
//        this.email = email;
//    }
}

@EqualsAndHashCode

equals method์™€ hashcode method๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด์ค€๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ๊ฐ์ฒด์—์„œ ํŠน์ • ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ด ๋˜‘๊ฐ™์€ ๊ฒฝ์šฐ ๊ฐ™์€ ๊ฐ์ฒด๋กœ ํŒ๋‹จ์„ ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

@EqualsAndHashCode(of = {"name", "email"})
public class User {
    private String name;
    private String email;
    private LocalDateTime createdAt; // ์ƒ์„ฑ๋œ ์‹œ๊ฐ„
    private LocalDateTime updatedAt; // ์—…๋ฐ์ดํŠธ๋œ ์‹œ๊ฐ„
}

์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด์—์„œ๋„ name, email๊ฐ€ ๋™์ผํ•˜๋‹ค๋ฉด ๊ฐ™์€ ๊ฐ์ฒด๋กœ ์ธ์‹ํ•˜๊ฒ ๋‹ค~๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„๋‹ค.


@Data

entity๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ์‹ค์Šต์„ ํ•  ๋•Œ๋Š” ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค.
@Data๋Š” @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode.๋ฅผ ๋ชจ๋‘ ํฌํ•จํ•˜๋Š” annotaion์ด๋‹ค. ํ•˜์ง€๋งŒ ์‹ค๋ฌด์—์„œ๋Š” ๋„ˆ๋ฌด ๋ฌด๊ฒ๊ณ  ๊ฐ์ฒด์˜ ์•ˆ์ •์„ฑ์„ ์ง€ํ‚ค๊ธฐ ๋•Œ๋ฌธ์— @Data์˜ ํ™œ๋™์„ ์ง€์–‘ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.


@Builder

๋‹ค์ˆ˜์˜ ํ•„๋“œ๋ฅผ ๊ฐ€์ง€๋Š” ๋ณต์žกํ•œ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ, ์ƒ์„ฑ์ž ๋Œ€์‹ ์— ๋นŒ๋”๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค
@AllArgsConstructor์™€ ๋น„์Šทํ•˜๊ฒŒ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ณ€์ˆ˜๊ฐ’์„ ์ฃผ์ž…ํ•ด์ฃผ๋Š”๋ฐ ๊ฐ์ฒด์˜ ์ƒ์„ฑ์„ builder์˜ ํ˜•์‹์œผ๋กœ ์ œ๊ณตํ•ด์ค€๋‹ค. @Builder๋„ ๋ชจ๋“  ๋ณ€์ˆ˜๋“ค์— ๋Œ€ํ•ด์„œ buildํ•˜๊ธฐ๋ฅผ ์›ํ•˜๋ฉด Class์œ„์— ๋ถ™์ด๊ณ  ํŠน์ • ๋ณ€์ˆ˜์— ๋Œ€ํ•ด buildํ•˜๊ธฐ๋ฅผ ์›ํ•œ๋‹ค๋ฉด ์ƒ์„ฑ์ž๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ๊ทธ ์œ„์— annotation์„ ๋ถ™์—ฌ์ค€๋‹ค.



Reference

profile
๋ธ”๋กœ๊ทธ ์ด์ „ํ–ˆ์Šต๋‹ˆ๋‹ค. -> https://seongwon.dev/

0๊ฐœ์˜ ๋Œ“๊ธ€