여기를 참고

Observer Pattern

ApplicationEventApplicationListener를 통해 구성된다. 등록된 빈이 ApplicationLisenter를 설정하면, ApplicationEventApplicationContext에 publish 될때마다 빈이 이를 인지하고 설정한 동작을 수행한다.

가장 단순한 구성으로, Event 내용이 될 클래스를 설정하고, ApplicationListener 를 통해 event 내용을 활용하여 어떤 동작을 수행할 수 있다. listener가 이를 인지하기 위해서는 ApplicationEventPublisher가 publish를 해야 한다.

Event 설정하기

CustomEvent

내용이 될 event를 설정한다. event가 될 클래스는 ApplicationEvent를 상속해야 한다.

import lombok.Getter;
import org.springframework.context.ApplicationEvent;

public class CustomSpringEvent extends ApplicationEvent {

    @Getter
    private String message;

    public CustomSpringEvent(Object source, String message) {
        super(source);
        this.message = message;
    }

}

CustomEventListener