๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’Š Java & Kotlin & Spring/- spring framework +

02 Lombok

by Wonit 2019. 11. 23.

Lombok ์ด๋ž€?

Getter/Setter ๊ทธ๋ฆฌ๊ณ  ์ƒ์„ฑ์ž๋“ค์„ Annotation์œผ๋กœ ๊ฐ„ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํ”Œ๋Ÿฌ๊ทธ์ธ

์ƒ์‚ฐ์„ฑ์„ ๋†’ํ˜€์ฃผ๋Š” ๋„๊ตฌ

gradle์— dependencise ์— Lombok์„ ์ถ”๊ฐ€์‹œ์ผœ์•ผ ํ•œ๋‹ค.

dependencies{
    compile('org.projectlombok:lombok) // lombok ์ถ”๊ฐ€
    implementation 'org.springframework.boot:spring-boot-starter-web'
    textImplementation 'org.springframework.boot:spring-boot-starter-web'
}
public class SearchParam {
    private String name;
    private String id;
    private String password;
    private int age;

    SearchParam(String name, String id, String password, int age){ // ์ƒ์„ฑ์ž
        this.name = name;
        this.id = id;
        this.password = password;
        this.age = age;
    }

    public String getName(){ // getter 
        return name;
    }

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

์ด์ฒ˜๋Ÿผ getter/setter์„ ์‚ฌ์šฉํ•œ๋‹ค๊ฑฐ๋‚˜ ์ƒ์„ฑ์ž๋ฅผ ์ฝ”๋“œ์— ์ถ”๊ฐ€์‹œ์ผœ์ฃผ๋ฉด ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๋„ ๋–จ์–ด์งˆ ๋ฟ๋”๋Ÿฌ ์ฝ”๋“œ์˜ ๊ธธ์ด๊ฐ€
๋น„ํšจ์œจ์ ์œผ๋กœ ๊ธธ์–ด์ง€๋Š” ๊ฒƒ์„

@Data ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ํ•ด๊ฒฐ ํ•˜๊ฒŒ ํ•ด์ค€๋‹ค.

@Data // ๊ธฐ๋ณธ ์ƒ์„ฑ์ž, getter/setter, toString ์ถ”๊ฐ€
@AllArgsConstructor //๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ์ƒ์„ฑ์ž ์ถ”๊ฐ€
public class SearchaParam {
    private String name;
    private String id;
    private String password;
    private int age;
    //getter, setter generator์„ ์“ฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
}

๋Œ“๊ธ€