Home > Spring > ๐Ÿƒ[Spring] `@AllArgsConstructor`์€ ๋ฌด์—‡์ผ๊นŒ์š”?

๐Ÿƒ[Spring] `@AllArgsConstructor`์€ ๋ฌด์—‡์ผ๊นŒ์š”?
Spring Framework

๐Ÿƒ[Spring] @AllArgsConstructor์€ ๋ฌด์—‡์ผ๊นŒ์š”?

๐Ÿ“Œ Intro.

  • โ†˜๏ธŽ @AllArgConstructor๋Š” Lombok์—์„œ ์ œ๊ณตํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๋ชจ๋“  ํ•„๋“œ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฐ–๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด ์คŒ.

โœ…1๏ธโƒฃ ์ฃผ์š” ํŠน์ง•.

1. ๋ชจ๋“  ํ•„๋“œ๋ฅผ ํฌํ•จํ•œ ์ƒ์„ฑ์ž ์ถ”๊ฐ€.

  • โ†˜๏ธŽ ํด๋ž˜์Šค์— ์„ ์–ธ๋œ ๋ชจ๋“  ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•จ.
  • โ†˜๏ธŽ ์ˆ˜๋™์œผ๋กœ ์ƒ์„ฑ์ž๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋˜๋ฏ€๋กœ ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง.

2. ์ฃผ๋กœ ๊ฐ์ฒด ์ƒ์„ฑ์— ์‚ฌ์šฉ.

  • โ†˜๏ธŽ ์˜์กด์„ฑ ์ฃผ์ž…, DTO ํด๋ž˜์Šค ์ƒ์„ฑ ๋“ฑ ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒฝ์šฐ์— ์œ ์šฉํ•จ.

3. Builder ํŒจํ„ด๊ณผ ํ˜ธํ™˜.

  • โ†˜๏ธŽ @AllArgsConstructor๋Š” ์ฃผ๋กœ Builder ํŒจํ„ด๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜์–ด ๊ฐ์ฒด ์ดˆ๊ธฐํ™” ๋ฐฉ์‹์„ ์œ ์—ฐํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

โœ…2๏ธโƒฃ ์‚ฌ์šฉ ์˜ˆ์ œ.

1. ๊ธฐ๋ณธ ์‚ฌ์šฉ.

import lombok.AllArgsConstructor;

@AllArgsConstructor
public class MyClass {
    private String name;
    private int age;
}
  • โ†˜๏ธŽ ์œ„ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•จ.
    public MyClass(String name, int age) {
      this.name = name;
      this.age = age;
    }
    

2. ์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค์—์„œ์˜ ์‚ฌ์šฉ.

  • โ†˜๏ธŽ JPA์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ๋งคํ•‘๋˜๋Š” ์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค์—์„œ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ์œ ์šฉํ•จ.
    ```java
    import jakarta.persistence.Entity;
    import lombok.AllArgsConstructor;

@Entity
@AllArgsConstructor
public class User {
private Long id;
private String username;
private String email;
}

- โ†˜๏ธŽ ์œ„ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์‰ฝ๊ฒŒ ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ์Œ.
```java
User user = new User(1L, "Alice", "alice@example.com");

3. DTO ๊ฐ์ฒด ์ƒ์„ฑ์— ํ™œ์šฉ.

  • โ†˜๏ธŽ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ ์‘๋‹ตํ•  DTO ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋จ.
    ```java
    import lombok.AllArgsConstructor;

@AllArgsConstructor
public class UserDTO {
private String username;
private String email;
}


- โ†˜๏ธŽ `new UserDTO("Alice", "alice@example.com")`์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Œ.

## โœ…3๏ธโƒฃ `@AllArgsConstructor`์™€ ๋‹ค๋ฅธ Lombok ์–ด๋…ธํ…Œ์ด์…˜.
### 1. `@NoArgsConstructor`
- โ†˜๏ธŽ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑ.

### 2. `@RequiredArgsConstructor`
- โ†˜๏ธŽ `final` ๋˜๋Š” `@NonNull`๋กœ ์„ ์–ธ๋œ ํ•„๋“œ๋งŒ ํฌํ•จํ•˜๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑ.

### 3. `@Builder`์™€ ์กฐํ•ฉ.
- โ†˜๏ธŽ Builder ํŒจํ„ด์„ ํ†ตํ•ด ๊ฐ€๋…์„ฑ๊ณผ ์œ ์—ฐ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Œ.
```java
import lombok.AllArgsConstructor;
import lombok.Builder;

@AllArgsConstructor
@Builder
public class MyClass {
    private String name;
    private int age;
}
  • โ†˜๏ธŽ MyClass.builder().name("Alice").age(25).builder(); ํ˜•ํƒœ๋กœ ๊ฐ์ฑ„ ์ƒ์„ฑ ๊ฐ€๋Šฅ.

โœ…4๏ธโƒฃ ์žฅ์ .

1. ์ฝ”๋“œ์˜ ๊ฐ„๊ฒฐํ™”.

  • โ†˜๏ธŽ ์ƒ์„ฑ์ž๋ฅผ ์ˆ˜๋™์œผ๋กœ ์ž‘์„ฑํ•  ํ•„์š” ์—†์ด ๋ชจ๋“  ํ•„๋“œ๋ฅผ ํฌํ•จํ•˜๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด์คŒ.

2. ์œ ์ง€๋ณด์ˆ˜์„ฑ ํ–ฅ์ƒ.

  • โ†˜๏ธŽ ํด๋ž˜์Šค ํ•„๋“œ๊ฐ€ ๋ณ€๊ฒฝ๋  ๊ฒฝ์šฐ ์ƒ์„ฑ์ž ์ฝ”๋“œ๋„ ์ž๋™์œผ๋กœ ๋ณ€๊ฒฝ๋˜๋ฏ€๋กœ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Œ.

3. ๊ฐ์ฒด ์ดˆ๊ธฐํ™” ์šฉ์ด.

  • โ†˜๏ธŽ ๊ฐ์ฒด๋ฅผ ํ•œ ๋ฒˆ์— ์ดˆ๊ธฐํ™”ํ•˜๋Š”๋ฐ ์œ ์šฉํ•˜๋ฉฐ DTO, ์—”ํ‹ฐํ‹ฐ, VO ํด๋ž˜์Šค ๋“ฑ ๋‹ค์–‘ํ•œ ๊ณณ์—์„œ ํ™œ์šฉ ๊ฐ€๋Šฅ.

โœ…5๏ธโƒฃ ์ฃผ์˜์ .

1. ํ•„๋“œ ๊ฐœ์ˆ˜ ์ฆ๊ฐ€ ์‹œ ๊ฐ€๋…์„ฑ ์ €ํ•˜.

  • โ†˜๏ธŽ ํ•„๋“œ๊ฐ€ ๋งŽ์€ ๊ฒฝ์šฐ ์ƒ์„ฑ์ž ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆœ์„œ๋ฅผ ์ž˜๋ชป ์‚ฌ์šฉํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Œ.
    • โ†˜๏ธŽ ์ด๋Ÿฐ ๊ฒฝ์šฐ @Builder ์‚ฌ์šฉ์„ ๊ถŒ์žฅ.

      2. JPA์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๋•Œ.

  • โ†˜๏ธŽ JPA ์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค์—์„œ @AllArgsConstructor๋Š” ๋ชจ๋“  ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋ฏ€๋กœ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž(@NoArgsConstructor)๋„ ํ•จ๊ป˜ ์ถ”๊ฐ€ํ•ด์•ผ ๋ฆฌํ”Œ๋ž™์…˜๊ณผ ํ˜ธํ™˜๋จ.
    @AllArgsConstructor
    @NoArgsConstructor
    public class User
    

๐Ÿš€ ์ •๋ฆฌ

  • โ†˜๏ธŽ @AllArgsConstructor๋Š” ๋ชจ๋“  ํ•„๋“œ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜์—ฌ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ด.
    • โ†˜๏ธŽ ์ฃผ๋กœ DTO, ์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด ์ดˆ๊ธฐํ™”์— ์‚ฌ์šฉ๋˜๋ฉฐ, Builder ํŒจํ„ด๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ๋”์šฑ ํšจ๊ณผ์ ์ž„.