Event Handling in Spring


You have found in every one of the parts that the center of Spring is the ApplicationContext, which deals with the total life cycle of the beans. The ApplicationContext distributes specific 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 stopped.

Event taking care of in the ApplicationContext is given through the ApplicationEvent class and ApplicationListener interface. Consequently, on the off chance that a bean executes the ApplicationListener, at that point each time a ApplicationEvent gets distributed to the ApplicationContext, that bean is notified.

Spring gives the accompanying standard occasions −

Sr.No. Spring Built-in Events & Description
1

ContextRefreshedEvent

This occasion is distributed when the ApplicationContext is either introduced or invigorated. This can likewise be raised utilizing the revive() strategy on the ConfigurableApplicationContext interface.

2

ContextStartedEvent

This occasion is distributed when the ApplicationContext is begun utilizing the begin() strategy on the ConfigurableApplicationContext interface. You can survey your database or you can restart any halted application subsequent to getting this event.

3

ContextStoppedEvent

This occasion is distributed when the ApplicationContext is quit utilizing the stop() technique on the ConfigurableApplicationContext interface. You can do required housekeep work in the wake of accepting this event.

4

ContextClosedEvent

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

5

RequestHandledEvent

This is a web-explicit occasion telling all beans that a HTTP ask for has been serviced.

Spring's occasion taking care of is single-strung so if an occasion is distributed, until and except if every one of the collectors get the message, the procedures are blocked and the stream won't proceed. Henceforth, care ought to be taken when planning your application if the occasion taking care of is to be used.

Listening to Context Events

To tune in to a setting occasion, a bean should execute the ApplicationListener interface which has only one technique onApplicationEvent(). So given us a chance to compose a guide to perceive how the occasions proliferates and how you can put your code to do required assignment dependent on certain events.

Let us have a working Eclipse IDE set up and find a way to make a Spring application −

Step Description
1 Create a task with a name SpringExample and make a bundle com.javacodegeeks under the src envelope in the made project.
2 Add required Spring libraries utilizing Add External JARs choice as clarified in the Spring Hello World Example chapter.
3 Create Java classes HelloWorld, CStartEventHandler, CStopEventHandler and MainApp under the com.javacodegeeks package.
4 Create Beans setup document Beans.xml under the src folder.
5 The last advance is to make the substance of all the Java records and Bean Configuration document and run the application as clarified below.

Here is the substance of HelloWorld.java file

package com.javacodegeeks; 

public class HelloWorld { 

private String message; 

public void setMessage(String message){ 

this.message = message; 

} 

public void getMessage(){ 

System.out.println("Your Message : " + message); 

} 

}

Following is the substance of the CStartEventHandler.java file

package com.javacodegeeks; 

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

} 

}

Following is the substance of the CStopEventHandler.java file

package com.javacodegeeks; 

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

} 

}

Following is the substance of the MainApp.java file

package com.javacodegeeks; 

import org.springframework.context.ConfigurableApplicationContext; 

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class MainApp { 

public static void main(String[] args) { 

ConfigurableApplicationContext setting = 

new ClassPathXmlApplicationContext("Beans.xml"); 

//Let us raise a begin event. 

context.start(); 

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

obj.getMessage(); 

//Let us raise a stop event. 

context.stop(); 

} 

}

Following is the setup record Beans.xml

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

<beans xmlns = "http://www.springframework.org/outline/beans" 

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation = "http://www.springframework.org/outline/beans 

http://www.springframework.org/outline/beans/spring-beans-3.0.xsd"> 

<bean id = "helloWorld" class = "com.javacodegeeks.HelloWorld"> 

<property name = "message" value = "Hello World!"/> 

</bean> 

<bean id = "cStartEventHandler" class = "com.javacodegeeks.CStartEventHandler"/> 

<bean id = "cStopEventHandler" class = "com.javacodegeeks.CStopEventHandler"/> 

</beans>

Once you are finished making the source and bean setup records, let us run the application. On the off chance that all is well with your application, it will print the accompanying message −

ContextStartedEvent Received 

Your Message : Hello World! 

ContextStoppedEvent Received 






© Javacodegeeks 2018 -2019
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3