8.HTTP 요청 처리
DispatcherServlet이 Web 요청을 처리하기 위해서는 클라이언트 브라우저로부터 전달받은 요청 경로에 해당하는 처리를 수행할 적절한 핸들러가 있는지 먼저 찾아야 함
이러한 핸들러에 대한 정보는 HandlerMapping이 담당
2. Controller 클래스에 Annotation 기반으로 핸들러를 맵핑하는 몇 가지 방법
2-1. @Copmonent Annotation 기반
Spring 초기 버전에서 사용하던 방식
처리 담당 클래스
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
log4j2.xml 로그 레벨 변경
log4j2.xml
<logger name="org.springframework.web">
<!-- info -> trace -->
<level value="trace" />
</logger>로그 확인
IDE 콘솔
TRACE: org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping - Mapped [/first-controller] onto '/first-controller'
DEBUG: org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping - Detected 1 mappings in 'org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping'2-2. @Controller Annotation 기반
@Controller는 @Component Annotation의 스테레오타입이기 때문에,
본질적으로는 @Component이자 Spring Bean이라고 볼 수 있음
구체적인 네이밍(Naming)을 통해 해당 클래스의 역할을 보다 직관적으로 파악할 수 있음
HomeController.java
// @Controller Annotation 패키지 경로
import org.springframework.stereotype.Controller;
@Controller // 클래스 레벨에 작성(`class`키워드 위에 작성한 Annotation)
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}내부적으로 처리를 담당하는 클래스
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.java
web.servlet 로그 확인
IDE 콘솔
TRACE: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping -
d.m.s.s.SecondController:
{GET [/s01/second-controller]}: handleRequest()
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - 1 mappings in 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping'beans 패키지 log4j2.xml 로그 레벨 변경
log4j2.xml
<logger name="org.springframework.beans">
<!-- info -> trace -->
<level value="debug" />
</logger>bean 생성 로그 확인
@Controller annotation이 붙은 클래스는 스프링이 자동으로 Bean으로 등록
servlet-context.xml에
servlet-context.xml
<!-- ... -->
<annotation-driven />IDE 콘솔
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean '/s01/first-controller'
TRACE: org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping - Mapped [/s01/first-controller] onto '/s01/first-controller'Last updated on