How to write custom event in Spring ?

Spring’s ApplicationContext provides the functionality to support events and listeners in code. We can create beans that listen for events which are published through our ApplicationContext. This is achieved via the ApplicationEventPublisher interface.

There are a few simple guidelines to follow:

  • The event should extend ApplicationEvent
  • The publisher should inject an ApplicationEventPublisher object
  • The listener should implement the ApplicationListener interface

Ex – Password changed event like whenever password will change email should trigger on registered mail id.

Lets Implement

Project structure be like below image

5

add a dependency on pom.xml file –

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>4.3.4.RELEASE</version>
</dependency>

bean class “Greetings.java

package com.spring.core;
public class Greetings {
    private String message;
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

UserDefinedEvent

package com.spring.core.event;
import org.springframework.context.ApplicationEvent;
public class UserDefinedEvent extends ApplicationEvent {
    private static final long serialVersionUID = -4264109794317707068L;
    public UserDefinedEvent(Object source) {
        super(source);
    }
    @Override
    public String toString() {
        return "user defined event";
    }
}

UserDefinedEventHandler

package com.spring.core.event;
import org.springframework.context.ApplicationListener;
public class UserDefinedEventHandler implements ApplicationListener<UserDefinedEvent> {
    @Override
    public void onApplicationEvent(UserDefinedEvent event) {
        System.out.println(event);        
    }
}

UserDefinedEventPublisher

package com.spring.core.publisher;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import com.spring.core.event.UserDefinedEvent;
public class UserDefinedEventPublisher implements ApplicationEventPublisherAware {
    private ApplicationEventPublisher publisher;
    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }
    public void publish() {
        UserDefinedEvent userDefinedEvent = new UserDefinedEvent(publisher);
        publisher.publishEvent(userDefinedEvent);
    }
}

beans.xml be like

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
    <bean id="greet" class="com.spring.core.Greetings">
        <property name="message" value="Hello One"></property>
    </bean>
    <bean id="eventHandler" class="com.spring.core.event.UserDefinedEventHandler"></bean>
    <bean id="eventPublisher" class="com.spring.core.publisher.UserDefinedEventPublisher"></bean>
</beans>

Output –

Dec 28, 2016 1:06:02 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@300ffa5d: startup date [Wed Dec 28 13:06:02 IST 2016]; root of context hierarchy
Dec 28, 2016 1:06:02 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Hello One
user defined event

For more detail, Please watch below video –

Thanks for reading 🙂

Please Subscribe our you tube channel Almighty Java

Leave a comment