정리하고기록하자

@RequiredArgsConstructor 본문

Spring

@RequiredArgsConstructor

정리하고기록하자 2023. 9. 18. 13:43
반응형

@RequiredArgsConstructor 란?

RequiredArgsConstructor는 Lombok으로 스프링에서 DI(의존성 주입)의 방법 중에 생성자 주입을 임의의 코드 없이 자동으로 설정해 주는 어노테이션 이다.

새로운 필드를 추가 할 때 다시 생성자를 만들어서 관리해야 하는 번거로움을 없애준다.

즉 @Autowired를 사용하지 않고 의존성을 주입 하는 것이다.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface RequiredArgsConstructor {
	/**
	 * If set, the generated constructor will be private, and an additional static 'constructor'
	 * is generated with the same argument list that wraps the real constructor.
	 * 
	 * Such a static 'constructor' is primarily useful as it infers type arguments.
	 * 
	 * @return Name of static 'constructor' method to generate (blank = generate a normal constructor).
	 */
	String staticName() default "";
	
	/**
	 * Any annotations listed here are put on the generated constructor.
	 * The syntax for this feature depends on JDK version (nothing we can do about that; it's to work around javac bugs).<br>
	 * up to JDK7:<br>
	 *  {@code @RequiredArgsConstructor(onConstructor=@__({@AnnotationsGoHere}))}<br>
	 * from JDK8:<br>
	 *  {@code @RequiredArgsConstructor(onConstructor_={@AnnotationsGohere})} // note the underscore after {@code onConstructor}.
	 * 
	 * @return List of annotations to apply to the generated constructor.
	 */
	AnyAnnotation[] onConstructor() default {};
	
	/**
	 * Sets the access level of the constructor. By default, generated constructors are {@code public}.
	 * 
	 * @return The constructor will be generated with this access modifier.
	 */
	AccessLevel access() default lombok.AccessLevel.PUBLIC;
	
	/**
	  * Placeholder annotation to enable the placement of annotations on the generated code.
	  * @deprecated Don't use this annotation, ever - Read the documentation.
	  */
	@Deprecated
	@Retention(RetentionPolicy.SOURCE)
	@Target({})
	@interface AnyAnnotation {}
}

@RequiredArgsConstructor 사용 하지 않았을 경우 예시

@RestController
@RequestMapping("/home")
public class RequiredArgsConstructorTest {

  private final AService aService;
  private final BService bService;
  private final CService cService;
  
  @Autowired
  public RequiredArgsConstructorTest(AService aService, BService bService, CService cService) {
    this.aService = aService;
    this.bService = bService;
    this.cService = cService;
  }
}

위에 코드 처럼 @Autowoired로 의존성 주입 코드 작성을 해야 한다.


@RequiredArgsConstructor 사용한 경우 예시

@RestController
@RequiredArgsConstructor
@RequestMapping("/home")
public class RequiredArgsConstructorTest {

  private final AService aService;
  private final BService bService;
  private final CService cService;
  
  ...
}

위에 코드 처럼 @RequiredArgsConstructor가 자동으로 생성자 주입에 대한 코드를 생성해준다.


마지막으로 DI(의존성 주입) 방식에는 3가지 방식이 있다.1. 필드 주입 (Field Injection)2. 수정자 주입 (Setter Injection)3. 생성자 주입 (Constructor Injection)이 중에서 가장 권장하는 DI(의존성 주입)은 생성자 주입 방식이다.

 

생성자 주입을 위한 코드를 작성 하는 부분에서 번거로움이 있기 때문에 Lombok에 @RequiredArgsConstructor을 많이 사용한다.

반응형

'Spring' 카테고리의 다른 글

DB 생성하기  (0) 2023.11.28
Spring-boot 신규 프로젝트 만들기 (2)  (0) 2023.11.28
Spring-boot 신규 프로젝트 만들기 (1)  (2) 2023.11.28
Redis  (0) 2023.09.22