Springs - SPLessons

Spring Custom Events

Home > Lesson > 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. CustomEvent.java [java]package splesson; import org.springframework.context.ApplicationEvent; public class CustomEvent extends ApplicationEvent{ public CustomEvent(Object source) { super(source); } public String toString(){ return "My Custom Event"; } }[/java] This class must define a default constructor which should inherit constructor from ApplicationEvent class. CustomEventPublisher.java [java]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); publisher.publishEvent(ce); } }[/java] 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. CustomEventHandler.java [java]package splesson; import org.springframework.context.ApplicationListener; public class CustomEventHandler implements ApplicationListener<CustomEvent>{ public void onApplicationEvent(CustomEvent event) { System.out.println(event.toString()); } } [/java] The EventClassHandler which implements ApplicationListener interface and implements onApplicationEvent method for the custom event. MainApp.java [java]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"); 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 [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="customEventHandler" class="splesson.CustomEventHandler"/> <bean id="customEventPublisher" class="splesson.CustomEventPublisher"/> </beans>[/xml] Output When compile the code following is the output in the console. [java]My Custom Event My Custom Event[/java]

Summary

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.