해결된 질문
작성
·
1.3K
·
수정됨
0
안녕하세요! 강의 너무 잘 듣고있습니다~~
...
public class HandlerConfig {
public void configureHandlerMappingMap(Map<String, Object> handlerMappingMap) {
handlerMappingMap.put("/front-controller/v5/v3/members/new-form", new MemberFormControllerV3());
handlerMappingMap.put("/front-controller/v5/v3/members/save", new MemberSaveControllerV3());
handlerMappingMap.put("/front-controller/v5/v3/members", new MemberListControllerV3());
handlerMappingMap.put("/front-controller/v5/v4/members/new-form", new MemberFormControllerV4());
handlerMappingMap.put("/front-controller/v5/v4/members/save", new MemberSaveControllerV4());
handlerMappingMap.put("/front-controller/v5/v4/members", new MemberListControllerV4());
}
public void configureHandlerAdapters(List<MyHandlerAdapter> handlerAdapters) {
handlerAdapters.add(new ControllerV3HandlerAdapter());
handlerAdapters.add(new ControllerV4HandlerAdapter());
}
}
...
@WebServlet(name = "frontControllerServletV5", urlPatterns = "/front-controller/v5/*")
public class FrontControllerServletV5 extends HttpServlet {
private final Map<String, Object> handlerMappingMap = new HashMap<>();
private final List<MyHandlerAdapter> handlerAdapters = new ArrayList<>();
public FrontControllerServletV5() {
HandlerConfig handlerConfig = new HandlerConfig();
handlerConfig.configureHandlerMappingMap(handlerMappingMap);
handlerConfig.configureHandlerAdapters(handlerAdapters);
}
...
위와 같이 HandlerConfig
클래스 내부의 메서드에서 handlerMappingMap
과 handlerAdapters
를 매개변수로 전달받아 값을 세팅해 주도록 코드를 작성해 봤습니다. 이런 방식 보다는 아래의 방식이 더 나을까요??
아래가 좀 더 '주입'이라는 느낌에 가까운 것 같아서 어떤 구조가 좋은 구조인지 고민됩니다..!
...
public class HandlerConfig {
public Map<String, Object> handlerMappingMap() {
Map<String, Object> handlerMappingMap = new HashMap<>();
handlerMappingMap.put("/front-controller/v5/v3/members/new-form", new MemberFormControllerV3());
handlerMappingMap.put("/front-controller/v5/v3/members/save", new MemberSaveControllerV3());
handlerMappingMap.put("/front-controller/v5/v3/members", new MemberListControllerV3());
handlerMappingMap.put("/front-controller/v5/v4/members/new-form", new MemberFormControllerV4());
handlerMappingMap.put("/front-controller/v5/v4/members/save", new MemberSaveControllerV4());
handlerMappingMap.put("/front-controller/v5/v4/members", new MemberListControllerV4());
return handlerMappingMap;
}
public List<MyHandlerAdapter> handlerAdapters() {
List<MyHandlerAdapter> handlerAdapters = new ArrayList<>();
handlerAdapters.add(new ControllerV3HandlerAdapter());
handlerAdapters.add(new ControllerV4HandlerAdapter());
return handlerAdapters;
}
}
...
@WebServlet(name = "frontControllerServletV5", urlPatterns = "/front-controller/v5/*")
public class FrontControllerServletV5 extends HttpServlet {
private final Map<String, Object> handlerMappingMap;
private final List<MyHandlerAdapter> handlerAdapters;
public FrontControllerServletV5() {
HandlerConfig handlerConfig = new HandlerConfig();
this.handlerMappingMap = handlerConfig.handlerMappingMap();
this.handlerAdapters = handlerConfig.handlerAdapters();
}
...
항상 좋은 구조로 코드를 짜고싶지만.. 역시 쉬운 게 아닌 거 같네요 ㅜ 답변 감사합니다 영한님!!