Reflection Example

iming·2022년 9월 18일
0

Spring-Basic

목록 보기
3/3

Spring이 private로 된 method를 Reflection API를 통해 call하는 예.

package com.iming.log01;

import java.lang.reflect.Method;

public class Main {
	public static void main(String[] args) throws Exception {
        //Reflection(java.lang.reflect)
        Class helloClass = Class.forName("com.iming.log1.Hello");
        Hello hello = (Hello)helloClass.newInstance();
        Method main = helloClass.getDeclaredMethod("main");
        main.setAccessible(true); //Hello의 main method를 호출가능하도록 함.
        
        main.invoke(hello);
    }
}
package com.iming.log01;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
private class Hello {
	@RequestMapping("/hello")
	public void main() {
		System.out.println("Hello");
	}
}

0개의 댓글