스프링강의

JDK동적 프록시

lby132 2023. 6. 20. 23:32
@Test
void dynamicA() {
    AInterface target = new AImpl();

    TimeInvocationHandler handler = new TimeInvocationHandler(target); // 프록시를 호출하는 로직

    // jdk에서 지원하는 프록시 생성기술(newProxyInstance를 하면 동적으로 생성된다.) 파라미터를 살펴보면. 1.프록시가 어디에 생성될지 지정 2.어떤 기반의 프록시를 만들지 지정 3.프록시가 사용할 로직
    // 클래스로더를 지정해주는 이유는 자바는 클래스가 호출되면 클레스로더에 올라가게 되는데 그 위치를 지정해주기 위함이다.
    AInterface proxy = (AInterface) Proxy.newProxyInstance(AInterface.class.getClassLoader(), new Class[]{AInterface.class}, handler);

    proxy.call();

    log.info("targetClass={}", target.getClass());
    log.info("proxyClass={}", proxy.getClass());
}

'스프링강의' 카테고리의 다른 글

html엔티티  (0) 2022.06.24
getParameterNames와 getParameterValues  (0) 2022.06.20
MultiValueMap  (0) 2022.06.14
생성자 주입의 장점  (0) 2022.06.12
InputStream  (0) 2022.05.31