Event Handling requires 3 Things

  1. Event Publisher
  2. Event Listener
  3. Event

CustomEvent.java(Event)

import org.springframework.context.ApplicationEvent;

public class CustomEvent extends ApplicationEvent
{
	public CustomEvent(Object source) 
        {
		super(source);
	}	

	public String toString()
	{
		return "Custom Message from Custom Event";
	}
}

CustomEventListener.java(Event Listener)

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class CustomEventListener implements ApplicationListener
{
	@Override
	public void onApplicationEvent(ApplicationEvent event) {		
		System.out.println(event.toString());
	}
}

Circle.java(Event Publisher)

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

@Component
public class Circle implements Shape, ApplicationEventPublisherAware
{			
	private ApplicationEventPublisher publisher;
	
	@Override
	public void drawShape() {	
		CustomEvent objCustEvent = new CustomEvent(this); 
		publisher.publishEvent(objCustEvent);
	}
	
	@Override
	public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
		this.publisher = publisher;
	}

	public ApplicationEventPublisher getPublisher() {
		return publisher;
	}

	public void setPublisher(ApplicationEventPublisher publisher) {
		this.publisher = publisher;
	}	
}	

Comments are closed.