springframework

o.s.w IoC 핵심기술. 프록시 기반의 AOP

Jungsoomin :) 2020. 9. 17. 21:37

본격적인 Spring-AOP의 프록시 기반 AOP를 알아본다.

목적은 스프링의 IoC 연동하여 엔터프라이즈 어플리케이션에서 가장 흔한 문제들에대한 해결책을 제공하는 것으로 만들어졌다.

 

 

 

프록시 객체가 Target을 참조, 즉 감싸서 클라이언트의 요청을 받아들이는 것이다.

쓰는 이유는 접근제어 및 부가기능 추가이다.


여담으로 빈을 주입받을떄에는 인터페이스 타입으로 받는게 가장좋다!


동적으로 프록시를 런타임에 만드는 기능도 있다.

 

이를 보강하여 Spring-Aop가 기반을 다졌다, 이는 빈의 생명주기에 동작하는 BeanPostProcessor , 빈이 등록되면 빈을 가공할 수 있는 인터페이스를 구현한 AutoProxyCreator 로 Target 빈을 감싸 프록시를 만들어 Target대신에 등록하는 방식이다.

 

AbstactAutoProxyCreator 가 언급한 BeanPostProcessor 를 구현하고 있다고 한다.

 


Target

public interface EventService {

    void createEvent();

    void publishEvent();

     void deleteEvent();
}

Proxy 테스트 클래스

@Service
public class SimpleEvent implements EventService {
    @Override
    public void createEvent() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Create an event");
    }

    @Override
    public void publishEvent() {

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Published an event");
    }

    public void deleteEvent(){
        System.out.println("Delete and event");
    }
}