Spring Custom Event – How to Design Customised Events

Keeping you updated with latest technology trends, Join DataFlair on Telegram

1. Objective

In our last tutorial, we discussed Spring Event Handling and here, we will learn about Spring Custom Event handling and the steps on how to design your own event handlers for Spring Framework. This session will help you make your own customized events by illustrating a working example on Eclipse IDE. An event-driven program is the one that responds to user events or similar type inputs. Basically, it is the one that is designed to react to the inputs.
So, let’s start Spring Custom Event Handling.

Spring Custom Event - How to Design Customised Events

Spring Custom Event – How to Design Customised Events in Spring Framework

2. Spring Custom Event Handling

Here you will be learning to design your own custom events for your application. There are several steps which are required to make and publish your own customized events in Spring Framework.
The following steps will help you design, publish and handle your Spring custom Events:
i. Create your project with name SpringEx and a package com.example. This should be under the src folder of your created project.
Let’s revise Spring IoC Containers – Types of Spring Container
ii. Add the Spring Libraries that are required using the Add External JARs options.
iii. Once you are done create an event class named CustomEvent extending ApplicationEvent. This must define default constructor inheriting constructor of ApplicationEvent class.
iv. Now you can publish your event from any of the classes.

  • Let us take an example of a class EventClassPublisher which implements ApplicationEventPubisherAware.
  • This EventClassPublisher class should be declared in XML config file as a bean. This should be done so that the container can identify the bean as an event publisher.

v. A published event can be handled in class.

  • Let us take an example, a class EventClassHandler which implements ApplicationListener interface.
  • It also implements the method onApplicationEvent().

vi. Define config file Bean.xml under src.
vii. Finally, write code for all Java files and Bean config file and run the application as described.
Follow this link to know more about Java Programming Language

3. Example on Eclipse IDE

After going through the above steps, you will see a working of Spring custom event by this example on Eclipse IDE.

a. CustomEvent.java

package com.example;
import org.springframework.context.ApplicationEvent;
public class CustomEvent extends ApplicationEvent
{
public CustomEvent(Object source)
{
super(source);
}
public String toString()
{
return "My Custom Event";
}
}

b. CustomEventPublisher.java:

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

c. CustomEventHandler.java

package com.example;
import org.springframework.context.ApplicationListener;
public class CustomEventHandler implements ApplicationListener<CustomEvent> {
public void onApplicationEvent(CustomEvent event)
{
System.out.println(event.toString());
}
}

d. Now the MainApp.java is described 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");
CustomEventPublisher cvp =
(CustomEventPublisher) context.getBean("customEventPublisher");
cvp.publish();
cvp.publish();
}
}

Do you know Why Spring Framework is Popular

e. The config file 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 = "com.example.CustomEventHandler"/>
<bean id = "customEventPublisher" class = "com.example.CustomEventPublisher"/>
</beans>

Finally, after creating the source files and bean configuration files, if everything is fine the following output message can be seen:
y Custom Event
y Custom Event
So, this was all about Spring Custom Events. Hope you like our explanation.

4. Conclusion

Hence, after this session on Spring Custom Event Handling, you got the basic knowledge of the event handling in Spring Framework and how to customize your event to your need in an application. You also got step by step learning of making your own events with the help of working example on Eclipse IDE. Now, you are able to make your own event handlers for your Spring Application.
Related Topic- Spring Batch
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.