Springs - SPLessons

Chapter 17

Spring Custom Events

Spring Custom Events

Spring Custom Events, As of now Splessons taught some concepts in Spring, As every one know that ApplicationContext is the core concept of Spring to control the life cycle of beans. ApplicationContext will publish events when loading the beans. For example, a ContextStartedEvent will work when context is started and ContextStoppedEvent will work when context is stopped. ApplicationEvent class and ApplicationListener interface are utilized to provide event handling in the ApplicationContext. Following is an example which describes more about this concept.

Following is an example which describes Spring Custom Events in the code.
CustomEvent.java

package splesson;

import org.springframework.context.ApplicationEvent;

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

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

This class must define a default constructor which should inherit constructor from ApplicationEvent class.
CustomEventPublisher.java

package splesson;

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);
   }
}

ApplicationEventPublisherAware, the developer will likewise need to pronounce this class in XML setup document as a bean so that the container can recognize the bean as an event publisher since it executes the ApplicationEventPublisherAware interface.
CustomEventHandler.java

package splesson;

import org.springframework.context.ApplicationListener;

public class CustomEventHandler 
   implements ApplicationListener<CustomEvent>{

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

}

The EventClassHandler which implements ApplicationListener interface and implements onApplicationEvent method for the custom event.
MainApp.java

package splesson;

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();
   }
}

The Application Context is spring’s more best in class holder. Like BeanFactory it can stack bean definitions, wire beans together and administer beans upon solicitation. Also it includes more enterprise-specific usefulness, for example, the capacity to determine literary messages from a properties document and the capacity to distribute application events to interested event listeners. This container is characterized by the org.springframework.context.ApplicationContext interface.
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="splesson.CustomEventHandler"/>

   <bean id="customEventPublisher" 
      class="splesson.CustomEventPublisher"/>

</beans>

Output
When compile the code following is the output in the console.

My Custom Event
My Custom Event

Summary

  • Spring Custom Events – Spring’s event handling is single threaded.
  • Spring Custom Events – ApplicationListener needs to be implemented by beans to listen a context event.