• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

SimpleLimitExceptionHandler()를 Bean으로 만들어야 하는 이유가 뭔가요?

22.01.07 19:50 작성 조회수 198

1

Repeat강의에서 SimpleLimitExceptionHandler를 설명해주실 때(43:29),

setExceptionHandler()에 SimpleLimitExceptionHandler객체를 Bean으로 만들어서 넣어줘야 정상 동작을 한다고 설명을 해주셨는대요.

Bean이 아닌 일반 객체로 넣어서, Process가 호출 될 때마다 새롭게 객체가 생성이 된다고 하더라도 limit이 3으로 들어가는건 변함이 없을 것 같은데,  limit 이 0이 되는 이유가 잘 이해가 안되네요.

답변 1

답변을 작성해보세요.

4

네 

이 부분은 Bean 의 라이프 사이클에서 내부적인 처리가 별도로 진행이 되어야 하는 점이 차이가 있습니다.

SimpleLimitExceptionHandler 의 소스를 보면 아래와 같은 구문이 있습니다.

@Override
public void afterPropertiesSet() throws Exception {
if (limit <= 0) {
return;
}
Map<Class<? extends Throwable>, Integer> thresholds = new HashMap<>();
for (Class<? extends Throwable> type : exceptionClasses) {
thresholds.put(type, limit);
}
// do the fatalExceptionClasses last so they override the others
for (Class<? extends Throwable> type : fatalExceptionClasses) {
thresholds.put(type, 0);
}
delegate.setThresholds(thresholds);
}

즉 빈의 라이프 사이클 중에 afterPropertiesSet() 를 호출하게 되는데 여기서 예외가 발생할 경우 처리해야 하는 초기화 설정을 하게 됩니다.

이 처리가 되지 않으면 limit 값이 제대로 설정이 되지 않습니다.

그래서 SimpleLimitExceptionHandler 를 빈으로 정의해야만 afterPropertiesSet() 가 호출되기 때문에 그렇습니다. 

일반 객체로 생성하면 빈이 아니기 때문에 afterPropertiesSet() 가 호출이 되지 않아서 limit 값이 0 으로 되어 버립니다.

내부적으로 처리하는 부분이라 참고 하시면 될 것 같습니다.