웹 환경 추가
웹 스코프는 웹 환경에서만 동작하므로 web환경이 동작하도록 라이브러리를 추가하자.
이제 hello.core.CoreApplication
의 main메서드를 실행하면 웹 애플리케이션이 실행되는 것을 확인할 수 있다.
| 참고 : spring-boot-starter-web
라이브러리를 추가하면 스프링 부트는 내장 톰켓 서버를 활용해서 웹 서버와 스프링을 함께 실행시킨다.
| 참고 : 스프링 부트는 웹 라이브러리가 없으면 우리가 지금까지 학습한 AnnotationConfigApplicationContext
을 기반으로 애플리케이션을 구동한다. 웹 라이브러리가 추가되면 웹과 관련된 추가 설정과 환경들이 필요하므로 AnnotationConfigServletWebServerApplicationContext
를 기반으로 애플리케이션을 구동한다.
만약 기본 포트인 8080 포트를 다른곳에서 사용중이어서 오류가 발생하면 포트를 변경해야 한다. 9090 포트로 변경하려면 다음 설정을 추가하자.
request 스코프 예제 개발
동시에 여러 HTTP요청이 오면 정확히 어떤 요청이 남긴 로그인지 구분하기 어렵다.
이럴때 사용하기 딱 좋은것이 바로 request 스코프이다.
다음과 같이 로그가 남도록 request 스코프를 활용해서 추가 기능을 개발해보자.
package hello.core.common;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.UUID;
@Component
@Scope(value = "request")
public class MyLogger {
private String uuid;
private String requestURL;
public void setRequestURL(String requestURL) {
this.requestURL = requestURL;
}
public void log(String message) {
System.out.println(" [ " + uuid + " ] " + " [ " + requestURL + " ] " + message);
}
@PostConstruct
public void init(){
uuid = UUID.randomUUID().toString();
System.out.println(" [ " + uuid + " ] request scope bean create "+ this);
}
@PreDestroy
public void close(){
System.out.println(" [ " + uuid + " ] request scope bean close "+ this);
}
}
MyLogger
클래스이다.@PostConstruct
초기화 메서드를 사용해서 uuid를 생성해서 저장해둔다. 이 빈은 HTTP요청 당 하나씩 생성되므로 , uuid를 저장해두면 다른 HTTP 요청과 구분할 수 있다.@PreDestory
를 사용해서 종료 메시지를 남긴다.requestURL
은 이 빈이 생성되는 시점에는 알 수 없으므로, 외부에서 setter로 입력 받는다. LogDemoController 추가
package hello.core.web;
import hello.core.common.MyLogger;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequiredArgsConstructor
public class LogDemoController {
private final LogDemoService logDemoService;
private final MyLogger myLogger;
@RequestMapping("log-demo")
@ResponseBody
public String logDemo(HttpServletRequest request){
String requestURL = request.getRequestURI().toString();
myLogger.setRequestURL(requestURL );
myLogger.log("controller test");
logDemoService.logic("testID");
return "OK";
}
}
http://localhost:8080/log-demo
|참고 : requestURL을 MyLogger에 저장하는 부분은 컨트롤러 보다는 공통 처리가 가능한 스프링 인터셉터나 서블릿 필터 같은 곳을 활용하는 것이 좋다. 여기서는 예제를 단순화하고, 아직 스프링 인터셉터를 학습하지 않은 사람을 위해 컨트롤러를 사용했다. 스프링 웹에 익숙하다면 인터셉터를 사용해 구현해보자.
LogDemoService 추가
package hello.core.web;
import hello.core.common.MyLogger;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class LogDemoService {
private final MyLogger myLogger;
public void logic(String id) {
myLogger.log("service id =" + id);
}
}
스프링 애플리케이션을 실행 시키면 오류가 발생한다. 메시지 마지막에 싱글톤이라는 단어가 나오고 스프링 애플리케이션을 실행하는 시점에 싱글톤 빈은 생성해서 주입이 가능하지만, request 스코프 빈은 아직 생성되지 않는다. 이 빈은 실제 고객의 요청이 와야 생성할 수 있다!
저도 개발자인데 같이 교류 많이 해봐요 ㅎㅎ! 서로 화이팅합시다!