Spring Event Handling – 5 Standard of Event Handlers

Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel

1. Objective

In our previous Spring tutorial, we learned Spring Custom Event. In this Spring Events Tutorial, we are going to learn about Spring Event Handling. Moreover, we will see the working of event handling in Spring Framework. You have seen that the ApplicationContext is the core of the Spring Framework which manages the lifecycle of Beans. There are certain events which ApplicationContext publishes which loading the Beans.
So, let’s start Spring Event Handling.

Spring Event Handling - 5 Standard of Event Handlers

Spring Event Handling – 5 Standard of Event Handlers

2. Spring Event Handling

Event Handling in Spring is provided with the ApplicationListener interface along with ApplicationEvent class. Thus, every time an ApplicationEvent gets published to ApplicationContext notification is sent to Bean.
Let’s Discuss Some Major Features in Spring Framework

a. A Standard of Event Handlers

There are several standard events in Spring Framework which are as follow:

i. ContextRefreshedEvent

This event gets published when ApplicationContext gets initialized or refreshed. Moreover, using refresh() on ConfigurableApplicationContext interface this event also can get published.

ii. ContextStartedEvent

This event gets published when ApplicationContext starts. It is due to using of the start() on ConfigurableApplicationContext.

iii. ContextStoppedEvent

This event gets published when ApplicationContext stops. It is due use of stop() on ConfigurableApplicationContext.

iv. ContextClosedEvent

This event publishes when ApplicationContext closes. It is due to use of close() on ConfigurableApplicationContext interface. Thus, when context reaches the end it can’t be redone or restarted.

v. RequestHandledEvent

It is a web-specific event which tells all Beans that HTTP request has been made.
Spring event handling is single threaded which is whenever an event publishes, all the receivers should get the message. Till that time all the processes are on halt and flow won’t continue. Hence, care should be taken while designing your application.
Read about Spring IoC Containers – Types of Spring Container

b. Creating Spring Application

To listen to a context event ApplicationListener interface should get implemented by a bean. With the working example on Eclipse IDE, you can see how event propagates. Also, how you can put your code to do the task based on events.
Use the steps below to create Spring application:

  1. Create your project with name SpringEx and a package com.example. This should be under the src folder of your created project.
  2. Add the Spring Libraries that are required using the Add External JARs options.
  3. Create HelloWorld.java, CStartEventHandler.java, CStopEventHandler.java, and MainApp.java under the above-made package.
  4. Define config file Bean.xml under src.
  5. Finally, write code for all Java files and Bean config file and run the application as described

c. Code of HelloWorld.java

package com.example;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}

d. Code for CStartEventHandler.java

package com.example;
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");
}
}

e. Code for CStopEventHandler.java

package com.example;
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");
}
}

f. MainApp.java defines as

package com.example;
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();
}
}

Let’s Revise Spring Java Based Configuration – How to Configure Spring Beans

g. The Beans.xml defined as

<?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 = "com.example.HelloWorld">
<property name = "message" value = "Hello World!"/>
</bean>
<bean id = "cStartEventHandler" class = "com.example.CStartEventHandler"/>
<bean id = "cStopEventHandler" class = "com.example.CStopEventHandler"/>
</beans>

After successfully creating source and bean config files run the application. The following message will be printed if everything goes fine:
ContextStartedEvent Received
Your Message: Hello World!
ContextStoppedEvent Received
So, this was all about Spring Event Handling. Hope you like our explanation.

3. Conclusion

Hence, in this Spring Events tutorial, you studied about the Spring Event Handling and how they work. You also saw several standards of Event Handlers. Also, the working example on Eclipse IDE will help you understand event handling better. In the upcoming article, you will see how you can make your own customized events in Spring.
Related Topic- Spring Transaction Management
For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.