4-4. MVC 시대 (실습 2)

박은서's avatar
Jan 29, 2026
4-4. MVC 시대 (실습 2)

실습 2. 리플렉션으로 단일진입점 만들기

[참고] 리플렉션이란?

리플렉션 : 동적으로 클래스를 분석하는 기술 (런타임 시)
💡
스프링 입장에서는 개발자가 어떤 메서드를 만들지 알 수 없고, 개발자가 메서드를 추가할 때마다 스프링 코드를 수정해야 함. 그래서 동적으로 처리하기 위한 리플렉션이 필요함.
 
동적 분석을 할 때 가장 쉬운 방법은 깃발(어노테이션)을 사용하는 것

1) BoardController

package ex01; public class BoardController { public void insert(){ System.out.println("insert 호출됨"); } public void delete(){ System.out.println("delete 호출됨"); } public void update(){ System.out.println("update 호출됨"); } }

2) App (main) 기본

package ex02; import java.lang.reflect.Method; public class App { public static void main(String[] args) { String uri = "/select"; BoardController con = new BoardController(); // 1. 동적 클래스 분석 Method[] methods = BoardController.class.getDeclaredMethods(); for (Method m : methods) { System.out.println(m); } } }

2-1) 결과

notion image

3) invoke() 함수

invoke(new된 객체) - new된 객체의 모든 메서드 출력
package ex02; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class App { public static void main(String[] args) { String uri = "/select"; BoardController con = new BoardController(); // 1. 동적 클래스 분석 Method[] methods = BoardController.class.getDeclaredMethods(); for (Method m : methods) { // System.out.println(m); try { m.invoke(con); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } } }

3-1) 결과

notion image

[참고] Method[] methods = BoardController.class.getDeclaredMethods();

💡
Method[] methods = BoardController.class.getDeclaredMethods();
➡️ 자바 리플렉션(Reflection)을 사용해서 BoardController 클래스에 선언된 모든 메서드 정보를 가져오는 코드
BoardController.class
  • BoardController의 Class 객체
  • JVM에 로딩된 클래스의 메타데이터
Class<?> clazz = BoardController.class;
getDeclaredMethods()
  • 해당 클래스에 직접 선언된 메서드 전부
  • private, protected, public 전부 포함
  • 상속된 메서드는 제외
Method[] methods;
Method
  • 자바 리플렉션 API의 클래스
  • 메서드의 이름, 파라미터, 리턴 타입, 어노테이션 정보 포함

4) 메서드 이름 출력

package ex02; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class App { public static void main(String[] args) { String uri = "/select"; BoardController con = new BoardController(); // 1. 동적 클래스 분석 Method[] methods = BoardController.class.getDeclaredMethods(); for (Method m : methods) { // System.out.println(m); System.out.println(m.getName()); try { m.invoke(con); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } } }

4-1) 결과

notion image

5) 특정 메서드 호출

package ex02; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class App { public static void main(String[] args) { String uri = "/select"; BoardController con = new BoardController(); // 1. 동적 클래스 분석 Method[] methods = BoardController.class.getDeclaredMethods(); for (Method m : methods) { // System.out.println(m); // System.out.println(m.getName()); try { if (m.getName().equals("update")) { m.invoke(con); } } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } } }

5-1) 결과

notion image

6) 메서드 추가해서 해당 메서드 호출

package ex02; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class App { public static void main(String[] args) { // 1. 키보드 입력 자리 String uri = "/join"; // 2. 다름 사람이 만든 코드 uri = uri.replace("/", ""); BoardController con = new BoardController(); // 2-1. 동적 클래스 분석 Method[] methods = BoardController.class.getDeclaredMethods(); for (Method m : methods) { // System.out.println(m); // System.out.println(m.getName()); try { if (m.getName().equals(uri)) { m.invoke(con); } } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } } }
package ex02; public class BoardController { public void insert(){ System.out.println("insert 호출됨"); } public void delete(){ System.out.println("delete 호출됨"); } public void update(){ System.out.println("update 호출됨"); } public void select(){ System.out.println("select 호출됨"); } public void join(){ System.out.println("join 호출됨"); } }

6-1) 결과

notion image

7) 필기

notion image
Share article