Springs - SPLessons

Chapter 16

Spring Event Handling

Spring Event Handling

Spring Event Handling, The major part of Spring is the ApplicationContext, Application context gives a method for determining instant messages and give a non specific approach to load file assets and it can distribute events to beans that are registered as listeners. The events of Spring as follows.

Context Events Listening:

A bean should implement anApplicationListener interface to listen a context event. It will be implemented when the application is terminated or started.

HelloWorld.java

package splessons;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }

   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
}

CStartEventHandler

package splessons;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;

public class CStartEventHandler 
   implements ApplicationListener<ContextStartedEvent>{

   public void onApplicationEvent(ContextStartedEvent event) {
      System.out.println("ContextStartedEvent Received");
   }
}

Here the class is implementing ApplicationListener, ContextStartedEvent passed on when the ApplicationContext is started using the start() method on the ConfigurableApplicationContext interface. Here all the beans in the life cycle will be restarted .
CStopEventHandler

package splessons;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;

public class CStopEventHandler 
   implements ApplicationListener<ContextStoppedEvent>

   public void onApplicationEvent(ContextStoppedEvent event) {
      System.out.println("ContextStoppedEvent Received");
   }
}

This is the code for the stopping events, It passed on when the ApplicationContext is terminated utilizing the stop() method on the ConfigurableApplicationContext interface.
MainApp.java

package splessons;

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

      // Let us raise a start event.
      context.start();
	  
      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

      obj.getMessage();

      // Let us raise a stop event.
      context.stop();
   }
}

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="helloWorld" class="splessons.HelloWorld">
      <property name="message" value="Hello World!"/>
   </bean>

   <bean id="cStartEventHandler" class="splessons.CStartEventHandler"/>

   <bean id="cStopEventHandler" class="splessons.CStopEventHandler"/>

</beans>

Output:
Output will be as follows.

ContextStartedEvent Received
Your Message : Hello World!
ContextStoppedEvent Received

Custom Events in Spring

Custom events are used to create own events. These are user flexible events and compared to default events provides user friendly property. Example to be taken while proceeding with custom events are as follows.

CustomEvent.java

package splessons;

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 splessons;

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

all event classes must extend the ApplicationEvent class. Now any bean, which implements ApplicationEventPublisherAware interface, can use ApplicationEventPublisher‘s publishEvent() method to send event to listeners. For a bean to listen to certain events, it must implement the ApplicationListener interface and handle the events in the onApplicationEvent() method.
CustomEventHandler.java

package splessons;

import org.springframework.context.ApplicationListener;

public class CustomEventHandler 
   implements ApplicationListener<CustomEvent>{

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

}

Spring uses ApplicationEvent as means of communication between container managed beans.A bean which wants to let other beans know about the occurrence of a certain event will act as publisher to publish the event. Likewise, a bean which is interested to know about the event will implement ApplicationListener and become the listener.The bean doesn’t have to explicitly subscribe itself. All it needs to do is implement ApplicationListener and register itself in the context XML. Once the application context publishes the event, spring will automatically publish the event to all the ApplicationListener(s)
MainApp.java

package splessons;

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">/beans

Output

My Custom Event
My Custom Event

Summary

  • Spring Event Handling – Custom events are used to create own events.
  • Spring Event Handling – To listen a context event, a bean ought to implement the ApplicationListener interface.