• ๋ฃธ๋ถ(Lombok) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ž€? InteliJ ์„ค์น˜/ ์ž์ฃผ ์‚ฌ์šฉ๋˜์•ผ ํ• , ์ง€์–‘๋˜์–ด์•ผ ํ•  ๋ฃธ๋ถ ์–ด๋…ธํ…Œ์ด์…˜

    2023. 10. 11.

    by. @leeeun

    Lombok ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

    • java ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผ ํ•  ๋•Œ ์ž๋™์œผ๋กœ ์ถ”๊ฐ€ ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ปดํŒŒ์ผํ•ด์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
    • ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์–ด๋…ธํ…Œ์ด์…˜์„ ์ œ๊ณตํ•˜๊ณ  ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผ๊ณผ์ •์—์„œ ์ƒ์„ฑํ•ด ์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘→ ์ฆ‰ ์ฝ”๋”ฉ ๊ณผ์ •์—์„œ๋Š” ๋กฌ๋ณต๊ณผ ๊ด€๋ จ๋œ ์–ด๋…ธํ…Œ์ด์…˜๋งŒ ๋ณด์ด๊ณ  getter์™€ setter ๋ฉ”์„œ๋“œ ๋“ฑ์€ ๋ณด์ด์ง€ ์•Š์ง€๋งŒ ์‹ค์ œ๋กœ ์ปดํŒŒ์ผ๋œ ๊ฒฐ๊ณผ๋ฌผ(.class)์—๋Š” ์ฝ”๋“œ๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์žˆ๋‹ค๋Š” ๋œป

     

    ์ธํ…”๋ฆฌ์ œ์ด Lombok ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

     

    1. Lombok plugin ์„ค์น˜

    File → Settings → Plugins → Marketplace์— lombok์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์„ค์น˜/ IDE ์žฌ์‹œ์ž‘

     

    2. Dependency ์„ค์ •

     

    (1) ์ˆ˜๋™์ถ”๊ฐ€ : ์ธํ…”๋ฆฌ์ œ์ด์—์„œ ๋ฃธ๋ถ ์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ๋‚˜์˜ค๋Š” ๋„์›€๋ง๋กœ ์„ธํŒ…

    (2) Maven ์„ค์ •

    pom.xmlํŒŒ์ผ์— ๋‹ค์Œ ํƒœ๊ทธ ์ƒ์„ฑ

    
    
        org.projectlombok
        lombok
        1.18.4
        provided
    
    

    (3) Gradle ์„ค์ •

    build.gradle ํŒŒ์ผ์— ๋‹ค์Œ ์ฝ”๋“œ ์ถ”๊ฐ€

    dependencies {
        # lombok plugin
        implementation('org.projectlombok:lombok')
        annotationProcessor('org.projectlombok:lombok')    
    
        # test ํ™˜๊ฒฝ
        testImplementation('org.projectlombok:lombok')
        testAnnotationProcessor('org.projectlombok:lombok')    
    }
    

     

    3. Lombok ์–ด๋…ธํ…Œ์ด์…˜ ํ™œ์„ฑํ™”

    File(๋งฅ๋ถ์€ InteliJ) > Setting > Build, Execution, Deployment > Compiler > Annotation Processors ์—์„œ Enable annotation processing์„ ์ฒดํฌ

     

    ์ž์ฃผ ์“ฐ๋Š” Annotation

     

    ์ƒ์„ฑ์ž ๊ด€๋ จ

     

    @NoArgsConstructor : ๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑ

    @RequiredArgsConstructor : final ํ•„๋“œ๋งŒ ํฌํ•จ๋œ ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑ

    @AllArgsConstructor : ๋ชจ๋“  ํ•„๋“œ๋ฅผ ํฌํ•จํ•œ ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑ

     

    ๋ฉ”์†Œ๋“œ ๊ด€๋ จ

     

    @Getter : Getter๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑ

    @Setter : Setter๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑ

    @ToString : toString ๋ฉ”์†Œ๋“œ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑ

    @EqualsAndHashCode : equals, hashCode ๋ฉ”์†Œ๋“œ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑ

    @With : with ๋ฉ”์†Œ๋“œ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑ

     

    ํ†ตํ•ฉ ๊ธฐ๋Šฅ

     

    @Data : ์—ฌ๋Ÿฌ ์–ด๋…ธํ…Œ์ด์…˜ ํ†ตํ•ฉ

    @ToString, @EqualsAndHashCode, @Getter, @Setter, @RequiredArgsConstructor ๊ธฐ๋Šฅ์„ ํ•จ๊ป˜ ์ œ๊ณต

    @Value : @Data์˜ ๋ณ€ํ˜•๋œ ๊ธฐ๋Šฅ

    ๋ชจ๋“  ํ•„๋“œ๋ฅผ private final๋กœ ์„ค์ •, ํด๋ž˜์Šค๋ฅผ final๋กœ ์„ค์ •, Setter๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š”๋‹ค.

    • staticConstructor : final ๋ณ€์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ static ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ ๋‹ค. (@RequiredArgsConstructor๋ฅผ private๋กœ ์ƒ์„ฑ)

    ๋นŒ๋” ํŒจํ„ด

     

    @Builder : ๋ฉ”์†Œ๋“œ ์ฒด์ด๋‹์„ ์ด์šฉํ•˜๋Š” static ๋ฉ”์†Œ๋“œ builder()๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

    ์ƒ์„ฑ์ž ๋Œ€์‹  ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉ

    @Builder.Default : ๋ช…์‹œ์ ์œผ๋กœ ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •

    @Singular : ํ•ด๋‹น ํ•„๋“œ๋ฅผ ์ปฌ๋ ‰์…˜์œผ๋กœ ์ฒ˜๋ฆฌํ•˜์—ฌ ๋‹ค์ˆ˜์˜ ๊ฐ’์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.

    ๊ทธ ์™ธ

    @NonNull : null-check ๋กœ์ง์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜์—ฌ null ๊ฐ’์ด ๋„˜์–ด์˜จ ๊ฒฝ์šฐ NulPointerException ๋ฐœ์ƒ

    @Cleanup : ํ•ด๋‹น ์˜์—ญ์„ ๋ฒ—์–ด๋‚  ๋•Œ ์ง€์ •๋œ ๋ฆฌ์†Œ์Šค์˜ close()๋ฅผ ํ˜ธ์ถœ

    try-finally ๊ตฌ๋ฌธ์ด ์ ์šฉ๋œ๋‹ค.

    @Synchronized : java์˜ synchronized์˜ ๋” ์•ˆ์ „ํ•˜๊ฒŒ ๋ณ€ํ˜•๋œ ๊ธฐ๋Šฅ

    static, instance ๋‹จ์œ„๋กœ ๋ฝ์„ ๊ฑฐ๋Š” synchronized์™€ ๋‹ค๋ฅด๊ฒŒ ํ•„๋“œ ๋‹จ์œ„๋กœ ๋ฝ์„ ๊ฑด๋‹ค. (๋ฉ”์†Œ๋“œ์—๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ)

    @SneakyThrows(์˜ˆ์™ธํด๋ž˜์Šค) : try-catch ๋ฌธ๋ฒ•์œผ๋กœ ํ•ด๋‹น ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌ

    Checked Exception ์ฒ˜๋ฆฌ์— ์œ ์šฉ

    @Log, @CommonsLog, @Log4j2, @Slf4j ๋“ฑ...

    ๋กœ๊ทธ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ log๋ผ๋Š” ์ด๋ฆ„์˜ private static final ํ•„๋“œ๋กœ ์ƒ์„ฑ

     

    Lombok ์–ด๋…ธํ…Œ์ด์…˜ ์ •๋ฆฌ

     

    ์ง€์–‘ํ•ด์•ผ ํ•  Annotation

    @AllArgsConstructor

    @Data

    @NoArgsConstructor

    @Builder

    Lombok - ์ง€์–‘ํ•ด์•ผ ํ•  annotation

     

    '๐Ÿ–ฅ๏ธ Computer science > : Software๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

    ๐ŸณDocker์™€ ์ปจํ…Œ์ด๋„ˆ  (0) 2023.04.09

    ๋Œ“๊ธ€