반응형

Custom Events in Spring


(원문 위치 : http://www.tutorialspoint.com/spring/custom_events_in_spring.htm )

Custom event를 작성하고 발생시키기 위해서는 몇가지 단계가 있다. Custom Spring Event를 작성하고 발생시키고 처리(handle)하기 위해 이번 강좌에서 보여주는 지시(instruction)에 따른다.

[003] HelloWorld Example 강좌를 참고하여 프로젝트를 생성한다.
(--> custom event처리를 위해 상속받아 처리하는 부분이 나와 있어 원문의 프로젝트 생성 단계를 아래에 함께 표시한다.)
StepDescription
1Create a project with a name SpringExample and create a package com.tutorialspoint under the src folder in the created project. All the classes will be created under this package.
-> 이건 프로젝트 생성으로 처리됨.
2Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
-> 이것도 처리됨.
3

Create an event class, CustomEvent by extending ApplicationEvent. This class must define a default constructor which should inherit constructor from ApplicationEvent class.
-> event를 생성하는 class인 'CustomEvent'를 ApplicationEvent를 상속받아 생성해라. 이 Class는 ApplicationEvent class로부터 상속된 생성자를 기본 생성자로 정의해야 한다.

4

Once your event class is defined, you can publish it from any class, let us say EventClassPublisher which implements ApplicationEventPublisherAware. You will also need to declare this class in XML configuration file as a bean so that the container can identify the bean as an event publisher because it implements the ApplicationEventPublisherAware interface.
-> event class가 정의된 후, 어떠한 class에서도 event를 발생시킬 수 있다. ApplicationEventPublisherAware를 구현한 class를 EventClassPublisher라고 하자. ApplicationEventPublisherAware interface를 구현하였으므로 container가 event publisher로써 bean을 확인할 수 있도록 bean으로써 XML설정파일에 이 class를 선언해야 한다.

5

A published event can be handled in a class, let us say EventClassHandler which implements ApplicationListener interface and implements onApplicationEvent method for the custom event.
-> 발생된 event는 class내에서 처리될 수 있다. ApplicationListener interface를 구현한 class를 EventClassHandler라 하고 custom event를 위한 onApplicationEvent함수를 구현한다.

6

Create beans configuration file Beans.xml under the src folder and a MainApp class which will work as Spring application.
-> 이건 프로젝트 생성을 참조하면 처리된다. 원문과는 위치가 다르다.(Maven을 쓰기 때문에..)

7

The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.
-> 아래 기술된 파일 내용 채워넣기.

CustomEvent.java:

package com.tutorialspoint;

import org.springframework.context.ApplicationEvent;

public class CustomEvent extends ApplicationEvent{
   
   public CustomEvent(Object source) {
      super(source);
   }

   public String toString(){
      return "My Custom Event";
   }
}

CustomEventPublisher.java:

package com.tutorialspoint;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

public class CustomEventPublisher 
   implements ApplicationEventPublisherAware {
   
   private ApplicationEventPublisher publisher;

   public void setApplicationEventPublisher
              (ApplicationEventPublisher publisher){
      this.publisher = publisher;
   }

   public void publish() {
      CustomEvent ce = new CustomEvent(this);
      publisher.publishEvent(ce);
   }
}

CustomEventHandler.java:

package com.tutorialspoint;

import org.springframework.context.ApplicationListener;

public class CustomEventHandler 
   implements ApplicationListener<CustomEvent>{

   public void onApplicationEvent(CustomEvent event) {
      System.out.println(event.toString());
   }

}

MainApp.java:

package com.tutorialspoint;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      ConfigurableApplicationContext context = 
      new ClassPathXmlApplicationContext("Beans.xml");
	  
      CustomEventPublisher cvp = 
      (CustomEventPublisher) context.getBean("customEventPublisher");
      cvp.publish();  
      cvp.publish();
   }
}

Beans.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="customEventHandler" 
      class="com.tutorialspoint.CustomEventHandler"/>

   <bean id="customEventPublisher" 
      class="com.tutorialspoint.CustomEventPublisher"/>

</beans>

실행 결과는 아래와 같다.

y Custom Event
y Custom Event



반응형

+ Recent posts