Recent Posts

Sunday, 16 June 2019

Event Handling

Event Handling
     In general, in GUI Applications, when we click on a button, when select 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].
     In Event Handling, the main intention of Listeners is to listen the events raised by the GUI Components, handle that events and generating results back to the GUI Applications. Similarly, in spring applications, IOC Container is able to rise events when it was started, refreshed, stopped and closed. 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 the form of predefined classes in "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 "refresh()" method on ApplicationContext.
☀ Where ContextStartedEvent will be raised when we access "start()" method on ApplicationContext.
☀ Where ContextStoppedEvent will be rised when we access "stop()" method on ApplicationContext.
☀ Where ContextClosedEvent will be rised when we access "close()" method on ApplicationContext.
☀ Where RequestHandledEvent will be rised in web applications when request is handled in Spring web applications.
     To Handle all the above Events, Spring Framework has provided a Listener in the form of "org.springframework.context.ApplicationListener" interface and it is having the following to execute when the respective event is raised.
public void onApplicationEvent(XXXEvent e)
Note: ApplicationListener is able to listen all the events by default, if we want to filter the events then we have to provide the respective Event type as Generic parameter.
E.g.
ApplicationListener<ContextStartedEvent> is able to listen only ContextStartedEvent.      In Spring applications, if we want to implement event handling then 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 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

Next Tutorial : Internationalization in Spring

Previous Tutorial : Bean Validations in spring

1 comment: