Spring – Event HandlingYou have seen in all the chapters that the core of Spring is the ApplicationContext, which manages the complete life cycle of the beans. The ApplicationContext publishes certain types of events when loading the beans. For example, a ContextStartedEvent is published when the context is started and ContextStoppedEvent is published when the context is stopped. Event handling in the ApplicationContext is provided through theApplicationEvent class and ApplicationListener interface. Hence, if a bean implements the ApplicationListener, then every time an ApplicationEvent gets published to the ApplicationContext, that bean is notified. Spring provides the following standard events:
Listening to Context Events To listen to a context event, a bean should implement the ApplicationListenerinterface which has just one method onApplicationEvent(). So let us write an example to see how the events propagate and how you can put your code to do the required task based on certain events. Let us have a working Eclipse IDE in place and take the following steps to create a Spring application:
package com.jtc;
public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public void getMessage(){ System.out.println("Your Message : " + message); } } Following is the content of the CStartEventHandler.java file
package com.jtc;
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"); } } Following is the content of the CStopEventHandler.java file
package com.jtc;
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"); } } Following is the content of the MainApp.java file
package com.jtc;
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"); // Let us raise a start event. context.start(); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.getMessage(); // Let us raise a stop event. context.stop(); } } Following is the configuration 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="helloWorld" class="com.jtc.HelloWorld"> <property name="message" value="Hello World!"/> </bean> <bean id="cStartEventHandler" class="com.jtc.CStartEventHandler"/> <bean id="cStopEventHandler" class="com.jtc.CStopEventHandler"/> </beans> Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message:
ContextStartedEvent Received
Your Message : Hello World! ContextStoppedEvent Received If you like, you can publish your own custom events and later you can capture the same to take any action against those custom events. If you are interested in writing your own custom events, you can check Custom Events in Spring |