java reflection - 디스패쳐 필터 생성- 메소드 검색- 어노테이션으로 검색- 주소 매핑
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package com.example.reflect.anno;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.METHOD}) // 어노테이션 동작위치
|
||||
@Retention(RetentionPolicy.RUNTIME) // 어노테이션 동작시기
|
||||
public @interface RequestMapping {
|
||||
String value();
|
||||
}
|
||||
@@ -1,11 +1,30 @@
|
||||
package com.example.reflect.controller;
|
||||
|
||||
import com.example.reflect.anno.RequestMapping;
|
||||
|
||||
public class UserController {
|
||||
|
||||
public void join(){
|
||||
@RequestMapping("/join")
|
||||
public String join(){
|
||||
System.out.println("join() 함수 호출");
|
||||
return "/";
|
||||
}
|
||||
public void login(){
|
||||
|
||||
@RequestMapping("/login")
|
||||
public String login(){
|
||||
System.out.println("login()함수 호출");
|
||||
return "/";
|
||||
}
|
||||
|
||||
@RequestMapping("/user")
|
||||
public String user(){
|
||||
System.out.println("user()함수 호출");
|
||||
return "/";
|
||||
}
|
||||
|
||||
@RequestMapping("/hello")
|
||||
public String hello(){
|
||||
System.out.println("hello()함수 호출");
|
||||
return "/";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
package com.example.reflect.filter;
|
||||
|
||||
import com.example.reflect.anno.RequestMapping;
|
||||
import com.example.reflect.controller.UserController;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class Dispatcher implements Filter {
|
||||
|
||||
@@ -21,12 +25,46 @@ public class Dispatcher implements Filter {
|
||||
// System.out.println("전체주소 : " + request.getRequestURL());
|
||||
|
||||
String endPoint = request.getRequestURI().replaceAll(request.getContextPath(), "");
|
||||
|
||||
System.out.println("엔드포인트 : " + endPoint);
|
||||
UserController userController = new UserController();
|
||||
if(endPoint.equals("/join")){
|
||||
userController.join();
|
||||
}else if(endPoint.equals("/login")){
|
||||
userController.login();
|
||||
// if(endPoint.equals("/join")){
|
||||
// userController.join();
|
||||
// }else if(endPoint.equals("/login")){
|
||||
// userController.login();
|
||||
// }
|
||||
//
|
||||
// Method[] methods = userController.getClass().getMethods(); // 모든 메소드만( 상속 된 메소드 포함)
|
||||
// 리플렉션 -> 메소드를 런타임 시점에서 찾아내서 실행
|
||||
Method[] methods = userController.getClass().getDeclaredMethods(); // 그 파일의 메소드만
|
||||
// for(Method method : methods) {
|
||||
//// System.out.println(method.getName());
|
||||
// if(endPoint.equals("/"+method.getName())) {
|
||||
// try {
|
||||
// method.invoke(userController);
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
for (Method method : methods) { // 메소드 갯수만큼 반복
|
||||
Annotation annotation = method.getDeclaredAnnotation(RequestMapping.class);
|
||||
RequestMapping requestMapping = (RequestMapping) annotation;
|
||||
System.out.println(requestMapping.value());
|
||||
|
||||
if(requestMapping.value().equals(endPoint)) {
|
||||
try {
|
||||
String path = (String) method.invoke(userController);
|
||||
|
||||
RequestDispatcher dis = request.getRequestDispatcher(path);
|
||||
// 내부에서 실행하기 때문에 필터 안탐
|
||||
dis.forward(request, response);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user