본문 바로가기
Web Programing!/JAVA / JSP

[Spring boot] @RequiredArgsConstructor?

by 어설픈봉봉이 2024. 7. 11.
반응형
SMALL

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

@RequiredArgsConstructor는 초기화 되지않은 final 필드나, @NonNull 이 붙은 필드에 대해 생성자를 생성해 줍니다.

@Autowired 필드, 생성자, 메소드 모두 주입가능하게 해주는 어노테이션이다.

 

@RequiredArgsConstructor

generates a constructor with 1 parameter for each field that requires special handling.

https://projectlombok.org/features/constructor

 

@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor

 

projectlombok.org

 

@ Autowired

Marks a constructor, field, setter method, or config method as to be autowired by Spring's dependency injection facilities. All non-initialized final fields get a parameter, as well as any fields that are marked as @NonNull that aren't initialized where they are declared.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Autowired.html

 

Autowired (Spring Framework 6.1.10 API)

Marks a constructor, field, setter method, or config method as to be autowired by Spring's dependency injection facilities. This is an alternative to the JSR-330 Inject annotation, adding required-vs-optional semantics. Autowired Constructors Only one cons

docs.spring.io

 

반응형