Event Handling

Event Handling

In this tutorial, we are going to discuss event handling in the spring framework. In general, in GUI Applications, when we click on a button, when selecting an item in check boxes, radio buttons, List boxes, choice boxes, etc. automatically an event will be raised, where the generated event will not be handled by the respective GUI components, where the generated events are handled by some component internally called as “Listeners”[Handlers].

Event Handling

In Event Handling, Listeners’ main intention is to listen to the events raised by the GUI Components, handle those events, and generate results back to the GUI Applications. Similarly, IOC Container can raise events when it was started, refreshed, stopped, and closed in spring applications. In this context, to handle the generated events, Spring Framework has provided “Event Handling.”

In Spring Framework, Event Handling capability is available with Application Context only, not with BeanFactory. In Spring Framework Event Handling, all events are represented in predefined classes in the “org.springframework.context.event” package like below.

  1. ContextRefreshedEvent
  2. ContextStartedEvent
  3. ContextStoppedEvent
  4. ContextClosedEvent
  5. RequestHandledEvent
  • Where ContextRefreshedEvent will be raised when we start ApplicationContext or when we access the “refresh()” method on ApplicationContext.
  • Where ContextStartedEvent will be raised when we access the “start()” method on ApplicationContext.
  • Where ContextStoppedEvent will be raised when we access the “stop()” method on ApplicationContext.
  • Where ContextClosedEvent will be rised when we access “close()” method on ApplicationContext.
  • Where RequestHandledEvent will be raised in web applications when the request is handled in Spring web applications.

To Handle all the above Events, Spring Framework has provided a Listener in the form of the “org.springframework.context.ApplicationListener” interface. It has the following to execute when the respective event is raised.

public void onApplicationEvent(XXXEvent e)

Note

ApplicationListener can listen to all the events by default, and if we want to filter the events, we have to provide the respective Event type as a Generic parameter.

E.g.

ApplicationListener is able to listen only ContextStartedEvent. If we want to implement event handling in Spring applications, we have to use the following steps.

  1. Create implementation classes for ApplicationListener interface and provide implementation for onApplicationEvent(–) method.
  2. Configure all implementation classes as bean components in the spring configuration file.

Note

To perform Event Handling in spring applications we have to use “org.springframework.context.ConfigurableApplicationContext” container , it is a child interface to ApplicationContext interface.

package com.ashok.spring.core.bean.eventhandling.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Employee {
   private String empName;
   private String empId;
   private String empAddress;
   private double salary;

   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   public String getEmpAddress() {
      return empAddress;
   }

   public void setEmpAddress(String empAddress) {
      this.empAddress = empAddress;
   }

   public double getSalary() {
      return salary;
   }

   public void setSalary(double salary) {
      this.salary = salary;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", empAddress=" + empAddress + ", salary=" + salary
            + "]";
   }
}
package com.ashok.spring.core.bean.eventhandling.listeners;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
/**
 * 
 * @author Ashok Kumar
 *
 */
public class ContextClosedListenerImpl implements ApplicationListener {
   @Override
   public void onApplicationEvent(ContextClosedEvent e) {
      System.out.println("Application Context is Closed");
   }
}
package com.ashok.spring.core.bean.eventhandling.listeners;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class ContextRefreshedListenerImpl implements ApplicationListener {
   @Override
   public void onApplicationEvent(ContextRefreshedEvent e) {
      System.out.println("Application Context is Refreshed");
   }
}
package com.ashok.spring.core.bean.eventhandling.listeners;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class ContextStartedListenerImpl implements ApplicationListener {
   @Override
   public void onApplicationEvent(ContextStartedEvent e) {
      System.out.println("Application Context is Started");
   }
}       
package com.ashok.spring.core.bean.eventhandling.listeners;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class ContextStoppedListenerImpl implements ApplicationListener {
   @Override
   public void onApplicationEvent(ContextStoppedEvent e) {
      System.out.println("Application Context is Stopped");
   }
}
<?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 = "emp" class = "com.ashok.spring.core.bean.id.beans.Employee">
      <property name = "empName" value = "Ashok Kumar"/>
      <property name = "empId" value = "Emp0087"/>
      <property name = "empAddress" value = "Bhimavaram"/>
      <property name = "salary" value = "50000"/>
   </bean>
    <bean id = "contextRefreshedEvent" class = "com.ashok.spring.core.bean.eventhandling.listeners.ContextRefreshedListenerImpl"/>
    <bean id = "contextStartedEvent" class = "com.ashok.spring.core.bean.eventhandling.listeners.ContextStartedListenerImpl"/>
    <bean id = "contextStoppedEvent" class = "com.ashok.spring.core.bean.eventhandling.listeners.ContextStoppedListenerImpl"/>
    <bean id = "contextClosedEvent" class = "com.ashok.spring.core.bean.eventhandling.listeners.ContextClosedListenerImpl"/>
</beans>
package com.ashok.spring.core.bean.eventhandling.test;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ashok.spring.core.bean.id.beans.Employee;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class TestSpringApplication {
   public static void main(String[] args) {
      String configFile = "/com/ashok/spring/core/bean/eventhandling/config/applicationContext.xml";
      ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(configFile);
      Employee emp = (Employee) context.getBean("emp");
      System.out.println(emp);
      context.start();
      context.refresh();
      context.stop();
      context.close();
   }
}

Output

Application Context is Refreshed
Employee [empName=Ashok Kumar, empId=Emp0087, empAddress=Bhimavaram, salary=50000.0]
Application Context is Started
Application Context is Refreshed
Application Context is Refreshed
Application Context is Stopped
Application Context is Stopped
Application Context is Closed
Application Context is Closed
Event Handling
Scroll to top