• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Spring : @EventListener in Spring 4.2

August 8, 2015 //  by Krishna Srinivasan//  Leave a Comment

This tutorial highlights the advantage of using @EventListener in Spring 4.2 for listening to the events.

One of the most important feature added to the Spring 4.2 is support for annotation driven event listeners. Now you can enable/mark a method with annotation @EventListener.

also read:

  • Spring Tutorials
  • ContextRefreshedEvent And ApplicationListener In Spring

@EventListener is the core annotation for enabling and registering the ApplicationListener to your applications. There is no other configurations really required to make this work. This tutorial explains @EventListener with simple example to understand.

EventListener in Spring 4

In the below example I am creating order and sending notification upon every new order creation. We have to create three classes to implement the notification mechanism.

  1. Create OrderEvent which extends the ApplicationEvent class.
  2. Create OrderService implements ApplicationEventPublisherAware interface. You have to declare this class in XML configuration file as a bean so that the container can identify the bean as an event publisher because it implements the ApplicationEventPublisherAware interface.
  3. Create OrderNotification class which actually uses the @EventListener for the methods that will be trigger for the registered events. This class also to be declared in the XML configurations.

If you look at the below example program, it will help you to understand how to use @EventListener in your project. This is a simple example to demonstrates the API usage.

OrderEvent.java

package javabeat.net.spring.core;
import org.springframework.context.ApplicationEvent;
public class OrderEvent extends ApplicationEvent{
   private static final long serialVersionUID = 1L;
   private int orderId;
   public OrderEvent(Object source, int id){
      super(source);
      this.orderId = id;
      System.out.println("Order Event Created!!");
   }
   public int getOrderId() {
      return orderId;
   }

    public void setOrderId(int orderId) {
      this.orderId = orderId;
    }
}

OrderService.java

package javabeat.net.spring.core;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
public class OrderService implements ApplicationEventPublisherAware{
   private ApplicationEventPublisher publisher;
   public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
      this.publisher = publisher;
   }
   public void createOrder(int orderId){
      System.out.println("Order Created");
      OrderEvent event = new OrderEvent(this, orderId);
      System.out.println("Publishing Order Event");
      publisher.publishEvent(event);
   }
}

OrderNotification.java

package javabeat.net.spring.core;
import org.springframework.context.event.EventListener;
public class OrderNotification {
   private int notificationId;
   public int getNotificationId() {
       return notificationId;
   }
   public void setNotificationId(int notificationId) {
       this.notificationId = notificationId;
   }
   @EventListener
   public void processOrderEvent(OrderEvent event) {
        System.out.println("OrderEvent for Order Id : " + event.getOrderId());
   }
}

applicationContext.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" xmlns:websocket="http://www.springframework.org/schema/websocket"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/websocket
 http://www.springframework.org/schema/websocket/spring-websocket-4.2.xsd">
   <context:component-scan base-package="javabeat.net.spring.core" />
   <bean id="orderService" class="javabeat.net.spring.core.OrderService"/>
   <bean id="orderNotifier" class="javabeat.net.spring.core.OrderNotification"/>
</beans>

MainApp.java

package javabeat.net.spring.core;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
     public static void main(String args[]){
           ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationConte          xt("applicationContext.xml");
          OrderService orderService = (OrderService)applicationContext.getBean("orderService")           ;
          orderService.createOrder(001);
          applicationContext.close();
 }
}

Output for the above program will be:

Order Created
Order Event Created!!
Publishing Order Event
OrderEvent for Order Id : 1

Category: Spring FrameworkTag: Spring 4.2 Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « FusionCharts : Beautifying the Column Charts
Next Post: Java 9 : Use Process API to Get Process Information Java 9»

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact