Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 재정의
- hashcode
- StringBuilder
- SQL
- VUE
- 테스트 코드
- docker
- jpa
- lambda
- Spring
- equals
- select_type
- 생성자 주입
- 필드 주입
- 조합
- AOP
- Test
- java
- redis
- KEVISS
- 바이너리 카운팅
- 인덱스
- DDL
- stream
- MSA
- 열 속성
- static
- DI
- cache
- jwt
Archives
- Today
- Total
백엔드 개발자 블로그
mybatis @Builder 주의사항 본문
문제 상황
mybatis 사용시 아래와 같은 에러가 발생했다.
// case 1
Caused by: java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
at java.base/java.util.Objects.checkIndex(Objects.java:359)
at java.base/java.util.ArrayList.get(ArrayList.java:427)
...
// case 2
Cause: java.lang.IndexOutOfBoundsException: Index 2 out of bounds for length 2
...
원인 분석
- resultType에 사용하는 class 모델에 @Builder어노테이션을 사용했을 가능성이 높다
- 빌더패턴은 이펙티브자바에서 지양하는 패턴으로 immutable하게 인스턴스를 생성하기 위해 종종 사용한다
롬복 @Builder를 사용시 주의사항
/**
* The builder annotation creates a so-called 'builder' aspect to the class that is annotated or the class
* that contains a member which is annotated with {@code @Builder}.
* <p>
* If a member is annotated, it must be either a constructor or a method. If a class is annotated,
* then a package-private constructor is generated with all fields as arguments
* (as if {@code @AllArgsConstructor(access = AccessLevel.PACKAGE)} is present
* on the class), and it is as if this constructor has been annotated with {@code @Builder} instead.
* Note that this constructor is only generated if you haven't written any constructors and also haven't
* added any explicit {@code @XArgsConstructor} annotations. In those cases, lombok will assume an all-args
* constructor is present and generate code that uses it; this means you'd get a compiler error if this
* constructor is not present.
- 작성된 document를 살펴보면 컴파일러는 생성자가 없으면 에러를 발생한다고 되어 있다.
- 모든 클래스는 디폴트로 빈 생성자를 기본으로 가지고 있다
- 그런데 롬복의 @Builder 어노테이션을 정의하면서 모든 인수가 있는 생성자를 기본으로 정의한다
- 그렇기 때문에 mybatis에서 리턴하는 모델은 빈 인스턴스를 생성한후 에트리뷰트를 바인딩해주는 과정에서 오류가 발생한 것이다
해결안 : 빈 생성자를 정의해주자
@Builder
@NoArgsConstructor
public class ReplyMessage {
...
}