여기를 참고
ApplicationEvent
와 ApplicationListener
를 통해 구성된다. 등록된 빈이 ApplicationLisenter
를 설정하면, ApplicationEvent
가 ApplicationContext
에 publish 될때마다 빈이 이를 인지하고 설정한 동작을 수행한다.
가장 단순한 구성으로, Event 내용이 될 클래스를 설정하고, ApplicationListener
를 통해 event 내용을 활용하여 어떤 동작을 수행할 수 있다. listener가 이를 인지하기 위해서는 ApplicationEventPublisher
가 publish를 해야 한다.
내용이 될 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;
}
}