티스토리 뷰

협업하는 환경에서 공통부분을 @AllArgsConstructor로 만들어서 공유했는데, 협업하는 개발자 한명이 @RequiredArgsConstructor로 바꿔서 보냈다. 처음엔 큰 차이가 있을까 싶어 그냥 본인이 개발한 소스를 추가했는데, 테스트 하다보니 생성자 주입이 작동하지 않아 찾아본 내용을 기록한다.



정리



@RequiredArgsConstructor 초기화 되지 않은 final 필드와 @NonNull 어노테이션이 붙은 필드에 대한 생성자 생성

@AllArgsConstructor 모든 필드에 대한 생성자 생성.



@RequiredArgsConstructor




@RequiredArgsConstructor generates a constructor with 1 parameter for each field that requires special handling. 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. For those fields marked with @NonNull, an explicit null check is also generated. The constructor will throw a NullPointerException if any of the parameters intended for the fields marked with @NonNull contain null. The order of the parameters match the order in which the fields appear in your class.



@AllArgsConstructor



@AllArgsConstructor generates a constructor with 1 parameter for each field in your class. Fields marked with @NonNull result in null checks on those parameters.




댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함