Spring Framework - ApplicationEvent Examples

Spring Framework 

Showing how create custom event by extending ApplicationEvent and autowiring ApplicationEventPublisher for firing the custom event.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.EventListener;

public class CustomEventWithApplicationEvent {

AListenerBean listenerBean () {
return new AListenerBean();

MyEvenPublisherBean publisherBean () {
return new MyEvenPublisherBean();

public static void main (String[] args) {

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
MyEvenPublisherBean bean = context.getBean(MyEvenPublisherBean.class);
bean.sendMsg("A test message");


private static class MyEvenPublisherBean{
ApplicationEventPublisher publisher;

public void sendMsg(String msg){
publisher.publishEvent(new MyEvent(this, msg));

private static class AListenerBean {

public void onMyEvent (MyEvent event) {
System.out.print("event received: "+event.getMsg());
System.out.println(" -- source: "+event.getSource());

private static class MyEvent extends ApplicationEvent{
private final String msg;

public MyEvent (Object source, String msg) {
this.msg = msg;

public String getMsg () {
return msg;
Original Post

See Also