๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BackEnd๐ŸŒฑ/Spring

[Spring] @NoArgsConstructor, @RequiredArgsConstructor, @RequiredArgsConstructor

by dkswnkk 2022. 3. 30.

์„œ๋ก 

@NoArgsConstructor, @AllArgsConstructor, @RequiredArgsConstructor ์…‹ ๋‹ค Lombok ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์˜๋ฏธ์™€ ์–ธ์ œ ์‚ฌ์šฉํ•˜๋Š”์ง€์— ๋Œ€ํ•ด ํ•œ๋ฒˆ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์–ด๋…ธํ…Œ์ด์…˜ ์˜๋ฏธ
@NoArgsConstructor ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†๋Š” ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
@AllArgsConstructor ๋ชจ๋“  ํ•„๋“œ ๊ฐ’์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
@RequiredArgsConstructor final์ด๋‚˜ @NonNull์ธ ํ•„๋“œ ๊ฐ’๋งŒ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

1. @NoArgsConstructor

@NoArgsConstructor์€ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†๋Š” ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋กœ ํ•œ๋ฒˆ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

import lombok.NoArgsConstructor;

@NoArgsConstructor
public class TestConstructor {
    private String name;
    private int age;

    public TestConstructor(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    void out() {
        System.out.println("์ด๋ฆ„: " + name + " ๋‚˜์ด: " + age);
    }
}

class TestMain {
    public static void main(String[] args) {
        TestConstructor testConstructor = new TestConstructor();
        testConstructor.out();
    }
}

ํ˜„์žฌ TestConstructor์—๋Š” name๊ณผ age๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— ์›๋ž˜๋Œ€๋กœ๋ฉด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ name๊ณผ age ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„˜๊ฒจ์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ ์œ„ ์ฝ”๋“œ๋Š” ๋„ฃ์–ด์ฃผ์ง€ ์•Š์•˜์œผ๋‹ˆ  ์ปดํŒŒ์ผ ์—๋Ÿฌ๋ฅผ ๋ฑ‰์–ด๋ƒ…๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ @NoArgsConstructor ์• ๋…ธํ…Œ์ด์…˜ ๋•๋ถ„์— ์ปดํŒŒ์ผ ์—๋Ÿฌ ์—†์ด ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š๋”๋ผ๋„ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

2. @RequiredArgsConstructor

@RequiredArgsConstructor์€ final์ด๋‚˜ @NonNull์ธ ํ•„๋“œ ๊ฐ’๋งŒ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class TestConstructor {
    private String name;
    private int age;
    private String sex;

    void out() {
        System.out.println("์ด๋ฆ„: " + name + " ๋‚˜์ด: " + age);
    }
}

class TestMain {
    public static void main(String[] args) {
        TestConstructor testConstructor = new TestConstructor("์•ˆ์ฃผํ˜•", 25, "๋‚จ"); // ์ปดํŒŒ์ผ ์—๋Ÿฌ
        testConstructor.out();
    }
}

์œ„ ์ฝ”๋“œ๋Š” ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์–ด๋””๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์„๊นŒ์š”?

@RequiredArgsConstructor์€ final์ด๋‚˜ @NonNull์ธ ํ•„๋“œ ๊ฐ’๋งŒ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•œ๋‹ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ˜„์žฌ name, age, sex ๋ณ€์ˆ˜๋“ค์€ ์ „๋ถ€ final๋„, @NonNull๋„ ์•„๋‹Œ ์ผ๋ฐ˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ๊ทธ ์ž์ฒด์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ @NonNull ํ˜น์€ final๋กœ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ปดํŒŒ์ผ๋ฉ๋‹ˆ๋‹ค.

import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class TestConstructor {
    @NonNull
    private String name;
    private final int age;
    private final String sex;

    void out() {
        System.out.println("์ด๋ฆ„: " + name + " ๋‚˜์ด: " + age);
    }
}

class TestMain {
    public static void main(String[] args) {
        TestConstructor testConstructor = new TestConstructor("์•ˆ์ฃผํ˜•", 25, "๋‚จ");
        testConstructor.out();
    }
}

 

3. @AllArgsConstructor

@AllArgsConstructor์€ ๋ชจ๋“  ํ•„๋“œ ๊ฐ’์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

import lombok.AllArgsConstructor;

@AllArgsConstructor
public class TestConstructor {
    private String name;
    private int age;
    private String sex;

    void out() {
        System.out.println("์ด๋ฆ„: " + name + " ๋‚˜์ด: " + age);
    }
}

class TestMain {
    public static void main(String[] args) {
        TestConstructor testConstructor = new TestConstructor("์•ˆ์ฃผํ˜•", 25, "๋‚จ");
        testConstructor.out();
    }
}

๋”ฐ๋กœ ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์ง€ ์•Š์•„๋„ @AllArgsConstructor ์–ด๋…ธํ…Œ์ด์…˜๋งŒ ์ ์šฉํ•˜๋ฉด ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ๊ฐ€ ์ž‘์„ฑ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฒˆ๊ฑฐ๋กœ์šด ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ ์ž‘์„ฑ ์ž‘์—…์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ฐธ๊ณ 

https://projectlombok.org/features/constructor

๋Œ“๊ธ€