Springs - SPLessons

Spring Event Handling

Home > Lesson > Chapter 17
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Spring Event Handling

Spring Event Handling

shape Description

Spring Event Handling, The major part of Spring is the ApplicationContext, Application context gives a method for determining instant messages and give a non specific approach to load file assets and it can distribute events to beans that are registered as listeners. The events of Spring as follows.

Context Events Listening:

shape Description

A bean should implement anApplicationListener interface to listen a context event. It will be implemented when the application is terminated or started.

shape Example

HelloWorld.java [java]package splessons; public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public void getMessage(){ System.out.println("Your Message : " + message); } }[/java] CStartEventHandler [java]package splessons; 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"); } }[/java] Here the class is implementing ApplicationListener, ContextStartedEvent passed on when the ApplicationContext is started using the start() method on the ConfigurableApplicationContext interface. Here all the beans in the life cycle will be restarted . CStopEventHandler [java]package splessons; 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"); } }[/java] This is the code for the stopping events, It passed on when the ApplicationContext is terminated utilizing the stop() method on the ConfigurableApplicationContext interface. MainApp.java [java]package splessons; 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(); } }[/java] 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. Beans.xml [java]<?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="splessons.HelloWorld"> <property name="message" value="Hello World!"/> </bean> <bean id="cStartEventHandler" class="splessons.CStartEventHandler"/> <bean id="cStopEventHandler" class="splessons.CStopEventHandler"/> </beans>[/java] Output: Output will be as follows. [java]ContextStartedEvent Received Your Message : Hello World! ContextStoppedEvent Received[/java]

Custom Events in Spring

shape Description

Custom events are used to create own events. These are user flexible events and compared to default events provides user friendly property. Example to be taken while proceeding with custom events are as follows. CustomEvent.java [java]package splessons; import org.springframework.context.ApplicationEvent; public class CustomEvent extends ApplicationEvent{ public CustomEvent(Object source) { super(source); } public String toString(){ return "My Custom Event"; } }[/java] CustomEventPublisher.java [java]package splessons; 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); } }[/java] all event classes must extend the ApplicationEvent class. Now any bean, which implements ApplicationEventPublisherAware interface, can use ApplicationEventPublisher‘s publishEvent() method to send event to listeners. For a bean to listen to certain events, it must implement the ApplicationListener interface and handle the events in the onApplicationEvent() method. CustomEventHandler.java [java]package splessons; import org.springframework.context.ApplicationListener; public class CustomEventHandler implements ApplicationListener<CustomEvent>{ public void onApplicationEvent(CustomEvent event) { System.out.println(event.toString()); } }[/java] Spring uses ApplicationEvent as means of communication between container managed beans.A bean which wants to let other beans know about the occurrence of a certain event will act as publisher to publish the event. Likewise, a bean which is interested to know about the event will implement ApplicationListener and become the listener.The bean doesn’t have to explicitly subscribe itself. All it needs to do is implement ApplicationListener and register itself in the context XML. Once the application context publishes the event, spring will automatically publish the event to all the ApplicationListener(s) MainApp.java [java]package splessons; 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(); } }[/java] 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. Beans.xml [java] <?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">/beans[/java] Output [java]My Custom Event My Custom Event[/java]

Summary

shape Key Points

  • Spring Event Handling - Custom events are used to create own events.
  • Spring Event Handling - To listen a context event, a bean ought to implement the ApplicationListener interface.