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:
@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.
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.
- Create OrderEvent which extends the ApplicationEvent class.
- 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.
- 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