Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
263 Members
Join this group to post and comment.
Soubhik Maji
Soubhik Maji
Computer Science
05 Feb 2019

Event Handling in Spring

Event handling in spring -

Every event handling has three core components ..

1.Event publisher -The entity that publishes the event .

2.Event Listener -The entity that listens for the event 

3.Event -The events itself , its a class that contains information about the event which the publisher publishes and the listener listens to .

2.To implement Event listener create a class and implements ApplicationListener interface and you have to implement a OnApplicationEvent  ...which will be called when the event is published ... The event is passed as an argument to this method ,  we can just print the event.toString() you are getting to check  whether this method is getting called when the event is published .(here event is the name of the passed  ApplicationEvent ).The OnApplicationEvent takes ApplicationEvent event as a parameter .

 Put @component  over the class defination-declaration  ...whatever create a Bean of Your Event Listener .

Make sure you have a component scan in place to tell spring to check for Java Beans created by both reading the XML as well as by annotations ....(if you don't what all those things are all about do some preeliminary reading on spring framework )

SO WHEN YOU RUN ...the above code you  will get printed all the ready made events that are getting published or triggered by spring  ..on running your application .

Now let's create a event ...//the numbers are according to above (1.2.3. )Numbers of core event handling components 

3.Event  ... 

  I.create a class extending ApplicationEvent class 

 Ii. When you do the extending you have to put a constructor ..eclipse will do the job for you ...Make sure will receive an object of generic  type object ... Make sure to call the constructor of parent passing the object you received ..calling super(source); //assuming source is the name of the object you received .

Iii. We create a toString method  and return a string that " The event occoured" or you can write the name of the event ...generally we create the class name of the event on the the name of the event itself here we can return that name of the class/event  ...has occurred as a string ..

Now , that we have the Event as well as the EventListener in place let's publish the event ...

1. Event Publishing .. Suppose you got a class and you want the event to get published when a method of that class is called so what you do is ......

i. You create a private variable of type ApplicationEventPublisher  ...suppose you name it publisher 

...import the required classes and packages and libraries ..whatever ...eclipse will do the job for you ..

ii. Inside the method where you want the event to be published create a new instance of the event you want to create ...and pass 'this' as parameter to let it known who is publishing the event .....'this' is the class in which the event has been created well your event class whatever you named it takes source(name ..can be anything ...its only that the class which is calling/publishing the event) as a parameter for its constructor

Iii. Using your publisher object call the publishEvent of the publisher ...


And pass the instance of the Event you just created .

Iii.Now you have a local variable called publisher how does it gets set  ..I.e. Step number i.... the solution is ... You implement a interface called ApplicationEventPublisherAware ...

And you will have to have a setter for your ApplicationEventPublisher class type local variable that we have named here publisher ... Or eclipse will just do that for us ...

Now , whever spring finds a class that implements ApplicationEventPublisherAware interface call the setter for the ApplicationEventPublisher with a publisher ...

this publisher that spring is  setting 

Or providing is actually a ApplicationContext ..... ApplicationContext is actually an object that implements ApplicationEventPublisher interface .. So this is a good example of coding to interfaces/abstraction rather than coding to concrete classes  ..which is a good practice ..Why ? We will have to talk about that later ...   So spring uses this coding to interfaces thing ...extensive .. You will see ...while working with Spring ...

This is how to use the Event Handling functionality of Spring we can catch events /we can listen to events which are published in the container and we create our own Events and publish them as and when required.