Springs - SPLessons

Spring Custom Events

Chapter 18

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Spring Custom Events

Spring Custom Events

shape Description

Spring Custom Events, As of now Splessons taught some concepts in Spring, As every one know that ApplicationContext is the core concept of Spring to control the life cycle of beans. ApplicationContext will publish events when loading the beans. For example, a ContextStartedEvent will work when context is started and ContextStoppedEvent will work when context is stopped. ApplicationEvent class and ApplicationListener interface are utilized to provide event handling in the ApplicationContext. Following is an example which describes more about this concept.

shape Example

Following is an example which describes Spring Custom Events in the code.

package splesson;

import org.springframework.context.ApplicationEvent;

public class CustomEvent extends ApplicationEvent{
   public CustomEvent(Object source) {

   public String toString(){
      return "My Custom Event";

This class must define a default constructor which should inherit constructor from ApplicationEvent class.

package splesson;

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

ApplicationEventPublisherAware, the developer will likewise need to pronounce this class in XML setup document as a bean so that the container can recognize the bean as an event publisher since it executes the ApplicationEventPublisherAware interface.

package splesson;

import org.springframework.context.ApplicationListener;

public class CustomEventHandler 
   implements ApplicationListener<CustomEvent>{

   public void onApplicationEvent(CustomEvent event) {


The EventClassHandler which implements ApplicationListener interface and implements onApplicationEvent method for the custom event.

package splesson;

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

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.

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

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

   <bean id="customEventHandler" 

   <bean id="customEventPublisher" 


When compile the code following is the output in the console.

My Custom Event
My Custom Event


shape Key Points

  • Spring Custom Events – Spring’s event handling is single threaded.
  • Spring Custom Events – ApplicationListener needs to be implemented by beans to listen a context event.