일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
- RDS
- 컴프리헨션
- 프로그래머스
- API
- 조건연산
- 프리온보딩
- 함수
- numpy
- 집계함수
- 파이썬
- 파이써닉코드
- 코딩테스트
- PYTHON
- cerbot
- mock server
- Postman
- 백엔드 인턴십
- 토이프로젝트
- todo project
- 람다함수
- self
- sqlalchemy
- yaml
- Jar배포
- Comprehension
- Django
- EC2
- spring boot
- 행렬곱
- 클래스
- Today
- Total
build my life
웹 스코프 본문
웹 스코프는 웹 환경에서만 동작
프로토 타입과 다르게 스프링이 해당 스코프의 종료 시점까지 관리 -> 종료 메서드가 호출
웹 스코프 종류
1. request
- HTTP 요청 하나가 들어오고 나갈 때 유지되는 스코프
- 각 요청마다 별도의 빈 인스턴스가 생성 관리
2. session
- HTTP session과 동일한 생명 주기를 가지고 있음
3. application
- 서블릿 컨텍스트와 동일한 생명주기
4. websocket
- 웹 소켓과 동일한 생명주기
웹 라이브러리 추가 -> build.gradle
// 웹 라이브러리 추가
implementation 'org.springframework.boot:spring-boot-starter-web'


request 스코프
- 동시에 여러 HTTP 요청이 오면 정확히 어떤 요청이 남긴 로그인지 구분하기 어려움
- 이럴 때 사용하는 스코프
실습
1. 로거가 잘 작동하는지 확인하는 테스트 컨트롤러

HttpServletRequest : 요청 URL을 받는다.
myLogger에 요청 URL을 저장해서 다른 요청과 섞이는 것을 해결
2. 비즈니스 로직이 있는 서비스 계층

웹과 관련된 부분은 controller 까지만 사용해야 하고 서비스 계층은 웹 기술에 종속되지 않고 순수하게 유지하는 것이 유지보수에 좋다.
오류
Caused by: org.springframework.beans.factory.support.ScopeNotActiveException: Error creating bean with name 'myLogger': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is 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.
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:383) ~[spring-beans-5.3.27.jar:5.3.27]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.27.jar:5.3.27]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.27.jar:5.3.27]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1391) ~[spring-beans-5.3.27.jar:5.3.27]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) ~[spring-beans-5.3.27.jar:5.3.27]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887) ~[spring-beans-5.3.27.jar:5.3.27]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ~[spring-beans-5.3.27.jar:5.3.27]
스프링 컨테이너가 뜨고 controller라 빈으로 등록되면서 의존관계 주입을 받는다.
MyLogger를 내놓으려고 하는데 request가 없는거야
request 스코프는 http 요청이 들어오고 나갈 때까지 존재하는데 컨테이너가 뜨는 시점에서 요청이 들어오지 않아서 오류가 나는 거야
해결방법
1. Provider 사용
http 요청이 오면 getObject하는 시점에 mylogger가 생성이 된다.

잘 실행 되는 것을 확인할 수 있음

'Spring' 카테고리의 다른 글
스코프와 프록시 (0) | 2023.07.14 |
---|---|
[Spring] 의존 관계 주입 방법 (0) | 2023.06.14 |
[Spring] 스프링 컨테이너 (0) | 2023.06.14 |
[Spring] AppConfig를 통한 의존관계 주입 (0) | 2023.06.02 |
[Spring Boot] Spring Initializr (0) | 2023.04.07 |