-
Notifications
You must be signed in to change notification settings - Fork 47
Gway framework #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gway framework #29
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
구현 잘 했다. 잘 접근했다. 👍
지금 구현도 잘 했는데 Controller가 여러 개의 매핑을 처리할 수 있도록 개선해 보면 좋겠다.
|
||
@Controller | ||
@RequestMapping("/create") | ||
public class CreateUserController { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spring 프레임워크와 같이 한 controller가 여러 개의 requestmapping을 지원하도록 구현하고, 회원 관련된 기능을 한 Controller로 구현해 본다.
public class ControllerFactory { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ControllerFactory.class); | ||
private static Map<String, Object> controllers = new HashMap<String, Object>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이와 같이 구현할 경우 Controller 하나가 url 요청 하나 밖에 처리할 수 없다.
각 url이 method 정보를 가지는 Map을 생성하는 구조로 구현하면 Controller 하나에 여러 개의 url mapping이 가능하지 않을까?
// method를 실행하기 위한 정보를 가져야 한다.
// 즉, 실행할 method와 method의 인스턴스
public class HandlerExecution {
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response) throws Exception {
return null;
}
}
위와 같은 HandlerExecution 객체를 생성한 후 다음과 같은 map을 가진다면..
Map<String, HandlerExecution> controllers = new HashMap<>
if (clazz.isAnnotationPresent(Controller.class)) { | ||
addController(clazz); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/ronmamo/reflections 라이브러리를 쓰면 클래스패스에서 특정 애노테이션이 추가되어 있는 클래스를 쉽게 추출할 수 있다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
구현 잘 했다. 💯 멋지게 구현했네.
DI 프레임워크 구현에 한번 도전해 봐도 좋겠다.
레벨4 강의 문서의 "프레임워크 5단계 - DI1" 강좌에 요구사항이 있다. 이 요구사항을 만족하는 DI 프레임워크에 한번 도전해 봐라. 일단 구현에 도전해 보고 구현이 힘든 상황이 생기면 그 시점에 힌트를 주는 방향으로 할께.
gram과 riverway의 이름을 따서, gramway framework를 구현해보았습니다.
사실 이름만 거창합니다ㅋㅋ
구현 해 본 기능은 다음과 같습니다.
@controller 어노테이션을 붙이면 객체가 컨트롤러로 인식이 되도록 하였습니다.
컨트롤러 인식이 된 객체의 경우, @RequestMapping을 키값으로 하여 Map에 담기고,
요청받은 url과 비교하여 분기 처리 됩니다.
컨트롤러 내의 메서드의 파라미터에 @RequestParam 어노테이션이 붙은 경우,
스프링의 기능과 비슷하게 동작하도록 구현하였습니다.
ex)
public String (@RqeustParam("id") String id){}
내부적으로 id = request.getParameter("id") 와 같이 동작합니다.
컨트롤러 내의 메서드의 파라미터가 자바빈 규약에 따르는 클래스인 경우,
자동으로 해당 객체를 생성하여 request영역의 해당 값을 가지고,
setter메서드를 실행하여 반환하는 기능을 구현하였습니다.
ex)
public String (UserDto userdto){}
해당 타입이 자바빈 규약에 따르는지 확인 한 후,
내부적으로 userDto = new UserDto(); UserDto.setUserId(request.getParameter("userId")
와 같이 동작합니다.
컨트롤러 내부의 메서드의 return type이 String일 경우,
return "redirect:/" 해주면 직접 response 객체를 호출하지않아도,
response.sendRedirect 되도록 구현하였습니다.
소박한 기능들이지만 구현을 하면서, 리플렉션에 대해 제법 이해를 한것 같습니다.