AOP 적용 시 발생한 에러 - No thread-bound request found

sarah·2023년 3월 27일
0

AOP

목록 보기
1/1

기존 프로젝트에 AOP 적용 업무를 맡아서 하게 되었다.
다른 프로젝트에 있는 Aspect와 동일하게 적용하면 되어서 그대로 들고와서 Pointcut 만 수정하였다.

폴더구조가 달라서 기존 프로젝트에 맞게 Pointcut을 수정했는데,
Pointcut context에 대해 익숙치 않아 Controller 패키지 뿐만 아니라 스케줄러 패키지에도 적용이 된것이다.

스케줄러가 1분마다 돌아가기에, 프로젝트를 실행하고 스웨거로 api 호출도 하기전에 아래와 같은 exception이 발생하였다.

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

원인

실질적인 request 가 존재하지 않거나, thread-safe 하지 않은 메서드에서 실행시 발생하는 에러라고 한다.

본 프로젝에 적용한 Aspect 클래스에서 아래와 같은 코드 때문에 발생한 에러인것이다.

@Autowired
private HttpServletRequest request;

HttpServletRequest를 autowired 해줘야 하는데, 스케줄러인 경우 request 가 존재하지 않아 위와 같은 에러가 발생한 것이다.

해결

Pointcut을 잘못 설정한 것이기에, Controller 패키지만 적용되도록 변경하였다.
(실수로 인해 새로운 것을 하나 또 알게 되었다....)

0개의 댓글