Skip to content

Commit

Permalink
HandlerExecution 내부에서 리플렉션으로 인스턴스를 생성하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
jeomxon committed Sep 14, 2023
1 parent e05d4f6 commit 3f80788
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import org.reflections.Reflections;
import org.slf4j.Logger;
Expand All @@ -17,21 +16,19 @@ public class AnnotationHandlerMapping {

private static final Logger log = LoggerFactory.getLogger(AnnotationHandlerMapping.class);

private final Object[] basePackages;
private final Object[] basePackage;
private final Map<HandlerKey, HandlerExecution> handlerExecutions;

public AnnotationHandlerMapping(final Object... basePackages) {
this.basePackages = basePackages;
public AnnotationHandlerMapping(final Object... basePackage) {
this.basePackage = basePackage;
this.handlerExecutions = new HashMap<>();
}

public void initialize() {
log.info("Initialized AnnotationHandlerMapping!");
for (final Object basePackage : basePackages) {
final Reflections reflections = new Reflections(basePackage);
final Set<Class<?>> controllerClazzSet = reflections.getTypesAnnotatedWith(Controller.class);
circuitClasses(controllerClazzSet);
}
final Reflections reflections = new Reflections(basePackage);
final Set<Class<?>> controllerClazzSet = reflections.getTypesAnnotatedWith(Controller.class);
circuitClasses(controllerClazzSet);
}

private void circuitClasses(final Set<Class<?>> controllerClazzSet) {
Expand All @@ -52,20 +49,11 @@ private void mapHandler(final Class<?> clazz, final Method method, final Request
final String requestUrl = requestMappingAnnotation.value();
final RequestMethod requestMethod = requestMappingAnnotation.method()[0];
final HandlerKey handlerKey = new HandlerKey(requestUrl, requestMethod);
final Object controller = getControllerInstance(clazz);
final HandlerExecution handlerExecution = new HandlerExecution(controller, method);
final HandlerExecution handlerExecution = new HandlerExecution(clazz, method);
handlerExecutions.put(handlerKey, handlerExecution);
}
}

private Object getControllerInstance(final Class<?> clazz) {
try {
return clazz.getDeclaredConstructor().newInstance();
} catch (final Exception e) {
throw new NoSuchElementException("인스턴스를 찾을 수 없습니다.");
}
}

public Object getHandler(final HttpServletRequest request) {
final String requestURI = request.getRequestURI();
final RequestMethod requestMethod = RequestMethod.valueOf(request.getMethod());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

public class HandlerExecution {

private final Object controller;
private final Class<?> clazz;
private final Method method;

public HandlerExecution(final Object controller, final Method method) {
this.controller = controller;
public HandlerExecution(final Class<?> clazz, final Method method) {
this.clazz = clazz;
this.method = method;
}

public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
return (ModelAndView) method.invoke(controller, request, response);
final Object controllerInstance = clazz.getDeclaredConstructor().newInstance();
return (ModelAndView) method.invoke(controllerInstance, request, response);
}
}

0 comments on commit 3f80788

Please sign in to comment.