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

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

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

๐Ÿ“Œ Intro.

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

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

1. final ํ•„๋“œ๋งŒ ํฌํ•จ.

  • โ†˜๏ธŽ final๋กœ ์„ ์–ธ๋œ ํ•„๋“œ๊ฐ€ ๋Œ€์ƒ์ด ๋จ.
  • โ†˜๏ธŽ final์ด ์•„๋‹Œ ํ•„๋“œ๋Š” ์ƒ์„ฑ์ž์— ํฌํ•จ๋˜์ง€ ์•Š์Œ.

2. @NonNull ํ•„๋“œ ํฌํ•จ.

  • โ†˜๏ธŽ @NonNull ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํ•„๋“œ๋„ ํฌํ•จ๋จ.
  • โ†˜๏ธŽ @NonNull์ด ๋ถ™์€ ํ•„๋“œ๋Š” ์ƒ์„ฑ์ž์—์„œ null ์ฒดํฌ๋ฅผ ์ž๋™์œผ๋กœ ์ถ”๊ฐ€ํ•จ.

3. ์ฃผ๋กœ ์˜์กด์„ฑ ์ฃผ์ž…์— ์‚ฌ์šฉ.

  • โ†˜๏ธŽ Spring์—์„œ ์ƒ์„ฑ์ž ๊ธฐ๋ฐ˜ ์˜์กด์„ฑ ์ฃผ์ž…(Constructor-based Dependency Injection)์— ์ž์ฃผ ์‚ฌ์šฉ๋จ.

โœ…2๏ธโƒฃ ์ƒ์„ฑ์ž ์ž๋™ ์ƒ์„ฑ ์˜ˆ์ œ.

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

import lombok.RequiredArgsConstructor;

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

2. @NonNull ํ•„๋“œ ์‚ฌ์šฉ.

import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class MyService {
    @NonNull
    private final MyRepository myRepository; // @NonNull null ์ฒดํฌ ์ถ”๊ฐ€.
    private final String name; // ์ƒ์„ฑ์ž์— ํฌํ•จ.
    private int age; // ์ƒ์„ฑ์ž์— ํฌํ•จ๋˜์ง€ ์•Š์Œ.
}
  • โ†˜๏ธŽ ์ƒ์„ฑ๋œ ์ƒ์„ฑ์ž๋Š” @NonNull ํ•„๋“œ์— ๋Œ€ํ•ด null ์ฒดํฌ๋ฅผ ํฌํ•จํ•จ.
    public MyService(MyRepository myRepository, String name) {
      if (myRepository == null) {
          throw new NullPointerException("myRepository is marked @NonNull but is null");
      }
      this.myRepository = myRepository;
      this.name = name;
    }
    

3. Spring๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ.

๐Ÿ“Œ Spring์—์„œ๋Š” @RequiredArgsConstructor๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ƒ์„ฑ์ž ์ฃผ์ž… ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Œ.

  • โ†˜๏ธŽ ์ผ๋ฐ˜์ ์ธ ์ƒ์„ฑ์ž ์ฃผ์ž…
    @Service
    public class MyService {
      private final MyRepository myRepository;
        
      public MyService(MyRepository myRepository) {
          this.myRepository = myRepository;
      }
    }
    
  • โ†˜๏ธŽ @RequiredArgsConstructor ์‚ฌ์šฉ
    @Service
    @RequiredArgsConstructor
    public class MyService {
      private final MyRepository myRepository;
    }
    
  • โ†˜๏ธŽ Spring์ด ์ž๋™์œผ๋กœ MyRepository๋ฅผ ์ฃผ์ž…ํ•จ.
  • โ†˜๏ธŽ ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง€๊ณ , ์˜์กด์„ฑ ์ฃผ์ž… ์‹œ ๋” ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์ง.

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

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

  • โ†˜๏ธŽ ํ•„์ˆ˜ ์ƒ์„ฑ์ž๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•  ํ•„์š”๊ฐ€ ์—†์–ด์ง.

2. ๋ถˆ๋ณ€ ๊ฐ์ฒด ์ƒ์„ฑ์— ์ ํ•ฉ.

  • โ†˜๏ธŽ final ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ํ•„๋“œ๋ฅผ ๋ถˆ๋ณ€์œผ๋กœ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Œ.

3. Spring๊ณผ์˜ ํ˜ธํ™˜์„ฑ.

  • โ†˜๏ธŽ ์ƒ์„ฑ์ž ๊ธฐ๋ฐ˜ ์˜์กด์„ฑ ์ฃผ์ž…์—์„œ ํ™œ์šฉ๋„๊ฐ€ ๋†’์Œ.

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

1. final ํ•„๋“œ๋งŒ ํฌํ•จ.

  • โ†˜๏ธŽ ์ƒ์„ฑ์ž์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” ํ•„๋“œ(final์ด ์•„๋‹Œ ํ•„๋“œ)์— ๊ฐ’์„ ์„ค์ •ํ•˜๋ ค๋ฉด Setter ๋˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ํ•จ.

2. @NonNull๋กœ null ์ฒดํฌ ํ•„์š”.

  • โ†˜๏ธŽ @NonNull ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด null ๊ฐ’์ด ๋“ค์–ด๊ฐ€๋„ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ.

๐Ÿš€ ์ •๋ฆฌ.

  • โ†˜๏ธŽ @RequiredArgsConstructor๋Š” ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ•˜๊ณ , ๋ถˆ๋ณ€์„ฑ์„ ์œ ์ง€ํ•˜๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์คŒ.
  • โ†˜๏ธŽ Spring๊ณผ Lombok์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๋•Œ ๊ฐ€์žฅ ๋งŽ์ด ์“ฐ์ด๋Š” Lombok ์–ด๋…ธํ…Œ์ด์…˜ ์ค‘ ํ•˜๋‚˜์ž„.