-
스프링 이벤트 프로그래밍 -ApplicationEventPublisherSPRING 2020. 4. 19. 18:21
ApplicationEventPublisher는 스프링에서 이벤트 프로그래밍에 필요한 인터페이스를 제공한다.
ApplicationContext가 이미 ApplicationEventPublisher를 상속하고 있기 때문에 ApplicationContext에서도 사용이 가능하다.
실습 : AppRunner에 이벤트를 발생시킨다.
아래와 같이 publishEvent 메서드를 이용하여 이벤트를 발생시킬 수 있다.
123456789101112131415161718192021222324252627282930313233package com.example.demo;import java.util.Locale;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationEventPublisher;import org.springframework.context.MessageSource;import org.springframework.stereotype.Component;@Componentpublic class AppRunner implements ApplicationRunner {@AutowiredApplicationEventPublisher publishEvent;@Overridepublic void run(ApplicationArguments args) throws Exception {// TODO Auto-generated method stubpublishEvent.publishEvent(new MyEvent(this,100));}}http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color ScripterMyEvent 생성
12345678910111213141516171819202122232425package com.example.demo;import org.springframework.context.ApplicationEvent;public class MyEvent extends ApplicationEvent{private int data;public MyEvent(Object source) {super(source);// TODO Auto-generated constructor stub}public MyEvent(Object source, int data) {super(source);this.data = data;}public int getData() {return data;}}http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter1. 스프링 4.2 버전 부터는 아래와 같이 ApplicationEvent를 상속받지 않아도
이벤트로 사용이 가능하다. 아래와 같이 사용하면 2번과 같이 EventHandler를 별도로 작성
할 필요가 없다.
12345678910111213141516package com.example.demo;import org.springframework.stereotype.Component;@Componentpublic class AnotherEvent {@EventListenerpublic void handle(MyEvent myEvent) {System.out.println(Thread.currentThread().toString());System.out.println("Another " + myEvent.getData());}}http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter2. MyEvent의 경우에는 아래와 같이 EventHandler를 작성하여 사용한다.
12345678910111213141516171819202122package com.example.demo;import org.springframework.context.ApplicationListener;import org.springframework.stereotype.Component;@Componentpublic class MyEventHandler implements ApplicationListener<MyEvent>{@Override@Order(Ordered.HIGHEST_PRECEDENCE + 2)public void onApplicationEvent(MyEvent event) {// TODO Auto-generated method stubSystem.out.println(Thread.currentThread().toString());System.out.println("이벤트 받았습니다. 데이터 : " + event.getData());}}http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter3. @Order 어노테이션
여러 핸들러를 추가하고 난 후 그 Handler의 이벤트를 수신할 시 순서를 정하고 싶을 떄 Order 어노테이션을 사용하면
된다. HIGHEST_PRECEDENCE의 값이 낮을 수록 우선순위가 높기 때문에 먼저 실행이 된다.
'SPRING' 카테고리의 다른 글
Spring Converter / Formatter를 이용한 데이터 바인딩 (0) 2020.04.26 ResourceLoader 관련 정리 (0) 2020.04.19 스프링 MessageSource (0) 2020.04.15 스프링 Properties 관련 정리 (0) 2020.04.15 스프링 프로파일 (Profile) (0) 2020.04.14