-
Notifications
You must be signed in to change notification settings - Fork 305
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
[MVC 구현하기 - 1단계] 비버(전인표) 미션 제출합니다. #337
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e5fabe4
test: ReflectionTest 작성
ingpyo 4fb3915
test: ServletTest 작성
ingpyo dd0e14a
feat: 패키지에 있는 클래스 handlerExecutions변수에 초기화
ingpyo 50d719a
refactor: 예외 처리를 위한 getDeclaredConstructor()메소드 추가
ingpyo cc6df27
refactor: 가독성을 위한 forEach문 제거
ingpyo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
package webmvc.org.springframework.web.servlet.mvc.tobe; | ||
|
||
import context.org.springframework.stereotype.Controller; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.reflections.Reflections; | ||
import web.org.springframework.web.bind.annotation.RequestMapping; | ||
import web.org.springframework.web.bind.annotation.RequestMethod; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public class AnnotationHandlerMapping { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(AnnotationHandlerMapping.class); | ||
|
||
private final Object[] basePackage; | ||
private final Map<HandlerKey, HandlerExecution> handlerExecutions; | ||
|
||
|
@@ -19,11 +21,40 @@ public AnnotationHandlerMapping(final Object... basePackage) { | |
this.handlerExecutions = new HashMap<>(); | ||
} | ||
|
||
|
||
public void initialize() { | ||
log.info("Initialized AnnotationHandlerMapping!"); | ||
getAnnotatedClasses().forEach(this::registerMethods); | ||
} | ||
|
||
private Set<Class<?>> getAnnotatedClasses() { | ||
Reflections reflections = new Reflections(basePackage); | ||
return reflections.getTypesAnnotatedWith(Controller.class); | ||
} | ||
|
||
private void registerMethods(final Class<?> clazz) { | ||
for (Method method : clazz.getDeclaredMethods()) { | ||
if (method.isAnnotationPresent(RequestMapping.class)) { | ||
registerMethod(clazz, method); | ||
} | ||
} | ||
} | ||
|
||
private void registerMethod(final Class<?> clazz, final Method method) { | ||
RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); | ||
final String value = requestMapping.value(); | ||
for (RequestMethod requestMethod : requestMapping.method()) { | ||
handlerExecutions.put( | ||
new HandlerKey(value, requestMethod), | ||
new HandlerExecution(clazz, method)); | ||
} | ||
} | ||
|
||
public Object getHandler(final HttpServletRequest request) { | ||
return null; | ||
final HandlerKey handlerKey = new HandlerKey(request.getRequestURI(), RequestMethod.valueOf(request.getMethod())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return handlerExecutions.keySet().stream() | ||
.filter(handlerKey::equals) | ||
.map(handlerExecutions::get) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("존재하지 않은 요청입니다.")); | ||
Comment on lines
+54
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예외처리 짱이네요. 저는 예외 하나도 잡지 않았거든요ㅋㅋ |
||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
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.
request mapping 어노테이션에 value로 정의되어 있으니 value라는 변수명으로 uri를 저장해도 괜찮다고 생각합니다.
주관적인 생각입니다! 취향 차이라 생각이 다르시다면 굳이 수정하지 않으셔도 괜찮습니당.
HandlerKey의 생성자 매개변수로 들어갈 변수이기 때문에 value보다 HandlerKey 필드에 맞는 uri가 어떨까 해요.
현재
registerMethod()
의 역할 중 하나는 HandlerKey 를 생성하는 것인데요. 이때 생성자의 인자를 받기 위해 value를 만들었잖아요. 그러면registerMethod()
에서 value 변수가 정의된 순간부터 value는 HandlerKey를 만들기 위한 부품이라고 생각해요. 이전의 "RequestMapping의 필드" 역할을 쭈욱 이어받는 것이 아니라용. 그래서 uri가 역할에 더 적합한 변수명이 아닐까 생각했습니다.