ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 스프링 이벤트 프로그래밍 -ApplicationEventPublisher
    SPRING 2020. 4. 19. 18:21

    ApplicationEventPublisher는 스프링에서 이벤트 프로그래밍에 필요한 인터페이스를 제공한다. 

    ApplicationContext가 이미  ApplicationEventPublisher를 상속하고 있기 때문에 ApplicationContext에서도 사용이 가능하다.

    실습 : AppRunner에 이벤트를 발생시킨다.

    아래와 같이 publishEvent 메서드를 이용하여 이벤트를 발생시킬 수 있다.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
     
     
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationEventPublisher;
    import org.springframework.context.MessageSource;
    import org.springframework.stereotype.Component;
     
    @Component
    public class AppRunner implements ApplicationRunner {
            
        @Autowired
        ApplicationEventPublisher publishEvent;
        
        @Override
        public void run(ApplicationArguments args) throws Exception {
            // TODO Auto-generated method stub
        
            publishEvent.publishEvent(new MyEvent(this,100));
     
        
        }
        
     
    }
     
    http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
     

    MyEvent 생성

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
     
    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 Scripter
     

    1. 스프링 4.2 버전 부터는 아래와 같이 ApplicationEvent를 상속받지 않아도

    이벤트로 사용이 가능하다. 아래와 같이 사용하면 2번과 같이 EventHandler를 별도로 작성

    할 필요가 없다.

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     
    import org.springframework.stereotype.Component;
     
    @Component
    public class AnotherEvent {
        
        @EventListener
        public 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 Scripter
     

     

    2. MyEvent의 경우에는 아래와 같이 EventHandler를 작성하여 사용한다.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     
    import org.springframework.context.ApplicationListener;
    import org.springframework.stereotype.Component;
     
    @Component
    public class MyEventHandler implements ApplicationListener<MyEvent>{
     
        @Override
        @Order(Ordered.HIGHEST_PRECEDENCE + 2)
        public void onApplicationEvent(MyEvent event) {
            // TODO Auto-generated method stub
            System.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 Scripter
     

     

    3. @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
Designed by Tistory.