[6주차] 컨트롤러 확장2 - 정인철#126
Open
incheol789 wants to merge 21 commits into
Open
Conversation
…template-main 2주차 자바 웹 요청 추상화
- ApplicationContext: beans를 Map으로 변경하여 이름 기반 빈 관리 - @bean 애노테이션 추가 및 registerBeanMethods() 구현 - @RequestMapping 애노테이션 추가 - LectureController: @component, @RequestMapping("/lectures") 적용 - LectureApplication: 수동 컨트롤러 매핑 제거, getControllerMapping() 자동화
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
6주차 - Controller 매핑 및 실행 책임 분리
구현 내용
인터페이스 기반 Controller와 애너테이션 기반 Controller가 공존하는 상황에서,
DispatcherServlet이 두 방식을 구분 없이 처리할 수 있도록 매핑과 실행 책임을 분리했습니다.핵심 설계
요청 처리 흐름은 아래와 같습니다.
DispatcherServlet은 Controller가 인터페이스 기반인지 애너테이션 기반인지 알지 못하고,HandlerMapping과HandlerAdapter가 각자의 책임을 담당합니다.변경 사항
1.
ApplicationContext- URL 매핑 책임 제거ApplicationContext는 빈 생성·관리만 담당해야 합니다.기존에는
getControllerMapping()이 URL 매핑까지 처리하고 있어 웹 MVC 관심사가 IoC 컨테이너에 혼재되어 있었습니다.변경 전
변경 후
2.
SimpleUrlHandlerMapping- 빈 이름 기반 자체 초기화기존에는 외부(
ApplicationContext)에서 만든 Map을 생성자로 주입받았습니다.AnnotationHandlerMapping이initialize()로 스스로 초기화하는 것과 달리 일관성이 없었습니다.변경 전
변경 후
3.
AnnotationHandlerMapping- 파라미터 타입 통일getBeans()의 반환 타입이Map<String, Object>로 변경됨에 따라 파라미터 타입을 맞췄습니다.내부 로직은
.values()로 접근하므로 동일하게 동작합니다.이로써 두
HandlerMapping이 동일한 초기화 패턴을 갖게 됩니다.4.
WebConfig- 인터페이스 기반 Controller 등록@Bean의value에 URL을 지정해 빈 이름 자체가 URL이 되도록 등록합니다.Controller가@FunctionalInterface이므로 람다로 간결하게 구현했습니다.SimpleUrlHandlerMapping이 초기화 시 빈 이름/home을 URL 키로 등록합니다.두 방식 비교
SimpleUrlHandlerMapping(빈 이름 = URL)SimpleControllerHandlerAdapter(handleRequest()호출)AnnotationHandlerMapping(URL + HTTP Method)AnnotationHandlerAdapter(Method.invoke()호출)동작 확인
GET /lecturesPOST /lecturesGET /home/lectures