Spring

Spring - Event Handling in Spring

Spring - Event Handling in Spring

The center of Spring is the ApplicationContext, which deals with the total life pattern of the beans. The ApplicationContext distributes particular sorts of occasions when stacking the beans. For instance, a ContextStartedEvent is distributed when the setting is begun, and ContextStoppedEvent is distributed when the setting is halted. Occasion taking care of in the ApplicationContext is given through the ApplicationEvent class and ApplicationListener interface. Consequently, assuming a bean carries out the ApplicationListener, each time an ApplicationEvent gets distributed to the ApplicationContext, that bean is advised. Spring gives the accompanying standard occasions − 

Spring Built-in Events with the Description 

ContextRefreshedEvent 

This occasion is distributed when the ApplicationContext is either instated or revived. This can likewise be raised utilizing the revive() technique on the ConfigurableApplicationContext interface. 

ContextStartedEvent 

This occasion is distributed when the ApplicationContext has begun utilizing the beginning() strategy on the ConfigurableApplicationContext interface. You can survey your data set or restart any halted application in the wake of getting this occasion. 

ContextStoppedEvent 

This occasion is distributed when the ApplicationContext is quite utilizing the stop() technique on the ConfigurableApplicationContext interface. You can accomplish the required housekeeping work after getting this opportunity. 

ContextClosedEvent 

This occasion is distributed when the ApplicationContext is shut utilizing the nearby() technique on the ConfigurableApplicationContext interface. A shut setting arrives at its finish of life; it can't be revived or restarted. 

RequestHandledEvent 

This is a web-explicit occasion to let all beans know that an HTTP demand has been overhauled. 

Spring's occasion dealing is single-strung, so if an occasion is distributed unless every one of the beneficiaries gets the message, the cycles are hindered, and the stream won't proceed. Consequently, care should be taken when planning your application if the occasion taking care of is to be utilized.

Paying attention to Context Events

To pay attention to a setting occasion, a bean should carry out the ApplicationListener interface, with only one technique onApplicationEvent(). So let us compose a guide to perceive how the occasions increase and how you can put your code to do required undertaking dependent on specific occasions.

package demo;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage(){
      System.out.println(message);
   }
}

CStartEventHandler.java code

package demo;

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

CStopEventHandler.java Code:

package demo;

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

MainApp.java Code:

package demo;

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

      context.start();
	  
      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();

      context.stop();
   }
}

xml code:


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

   <bean id = "cStartEventHandler" class = "demo.CStartEventHandler"/>
   <bean id = "cStopEventHandler" class = "demo.CStopEventHandler"/>

</beans>