Spring Tutorial 23 - Event Handling in Spring

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to this tutorial the previous tutorial we saw one advantage of using application context over the bean Factory and that was for using messaging and internationalization features in this tutorial we're going to look at a second advantage second feature the application context object provides which is event handling there are three core elements when it comes to event handling are not just spring but generally any event handling consists of three core components one is the event publisher the entity which publishes the event second is the event listener the entity which is actually listening for the event and the third eye' entity is the event itself you know it's a class that contains information about the event which the publisher publishes and the listener listens to so in the case of spring event handling there are interfaces so the spring provides an application listener interface so if you are writing a class that needs to listen for events it has to implement the application listener interface then you also have the application event publisher interface so if you're writing a class that publishes events you want to use the application event publisher interface and then the third thing is the class application event so if you're writing your own events custom events then you would want to extend this application event and you can write additional functionality or additional metadata about the event so we'll start off by writing a class that listens to all the spring events that are published and later we'll want to create our own event and we'll learn how to publish that event so the first thing we need to do is let's say I want to write a class to listen to all the events what I do is let me create a new class I'll call this the my my event listener finish now my eventlistener needs to implement the application listener interface so the application of this mo interface is provided by spring it is in the spring framework context package now when I implement the application listener interface I need to implement a method and the method is on application event so this method is called by spring then any application event is published and this method takes in a parameter which is the application event object itself and now spring is going to send the application event object to this method so what I need to do is you know whatever code I write over here will get executed upon every event that is published in the spring framework and of course the event itself is passed on every call so in order to test this what I will do is I will just write a simple system dot out dot println and I will just print the event itself so I will see event dot to string so okay this is the event here I'll call it even so whatever event is being published this method is called right so the event itself is passed as a parameter to this method call so I will be taking this event object I will do a to string and I will print that into the output so let's save this now of course as with all other beans I will have to declare this as a spring bean and that's all I need to do to register this as a valid event listener so can you spring XML but now that we have learned annotations I will use the ad component annotation to define this as a spring beam and of course since I have this inside this are Kaushik java brains package and this package has been registered to do component scanning this will be registered as a component just because of this competent tag so let me save this now I don't need to do anything else what this class will do as of now is to catch all the framework related events we have not published any events of our own so it will catch all the framework related events and it will print them to string so let's run this now and see what happens so here you can see there is an event call that has been printed here you see there is this context refresh event that has been published and our event listener class has listened to this and it has printed a two string so here you see the startup date and the root of the context hierarchy so basically what this message is saying is this message is the two strength of the context refreshed event so the context has been created and refreshed and that event has been published by the spring framework and our listener class has listened to that event to scored that event and it is gonna do string and it has printed that out so this is a simple way in which we can listen to events but now what if you want to publish your own event so there are two things we need to do if you have to publish your own event first we need to have an event class we need to write your own event class or you can of course use an existing event but in this example I'll show you how to write your own event class and then after that you need to have that even published in one of your other beans so let's say in my triangle being our let me take a circle here in my circle bean now let's say in this a draw method I want to publish an image and let me call that event as a drive in C so the first step is I have to create a dry wind class the second step is I need to publish that in my draw method so let's do that now I will first create a new class I'll call this the draw event and the draw event has to extend a class called application event so the application event is the base event class that spring provides so whatever custom even see you write it has to extend this application event class so I will extend application event get the import done now when I do the extend I will have to add a constructor so in the constructor we'll make sure that the super is called so the constructor of the parent application event is called we don't need to do anything here but what I'll do is I'll override the two string and I'll print my own custom message so that our listener can catch it because we are doing an event or two string in the listener so what I'll do here is I will override the two string I'll write my own true string in the draw method I mean in the draw event class and then I will see if that string gets published and it's caught by the listener so I will write a public string to string and return let me say draw event occurred okay so this is my event class it's as simple as that so it has an ordered into a string which I will you know I have a custom message here and I look for this message when I'm listening so let me close this okay so now that we have an event we will have to publish statement so I intend to do this in the circle as I wanted he told you I have a draw method here and I will be publishing the seventh in the draw method so this is where I should publish so in order to publish an event I need to have an event publisher spring provides an interface called application event publisher and we can use that interface in order to publish events so I'll create a member variable private application event publisher publisher so I will import the application event publisher so this publisher has a method to publish events so I can do a pub sure dot publish event and this takes a parameter as the application event and I can publish my draw event here so what I Louise alphas create a new event see draw event equals new drive event and of course I have to give this class which is the class in which the event has been created because you can see the drive and takes in the source as a parameter for the constructor okay so now that I have this draw event I can publish this drive event by passing it to the publisher that publish event and then this even gets published but now the question is this we have a local and variable called publisher which we have defined here now how does this value get set we have to ask spring to set the publisher do this so that we can use the publisher in order to publish shipments now how do we get a handle to the publisher so in order to get a handle to the publisher what we do is whatever class is the source of even publishing we implement an interface called application event publisher aware I will import this so what this tells Spring is I have this class here which needs a handler on the publisher object so that I can publish events so when it sees when spring container sees any beans that have implemented the application event publisher aware interface then it provides the publisher object to such beans and the way it does that is by because I'll have to add the unemployment add methods there is only one method here which is the set application event publisher and this is the method that spring is going to use to give us the publisher so let me call this publisher and the code here all I have to do is I already have a publisher member variable so whatever publisher I get from spring I just need to allocate that to my local variables so I will just say this dot publisher equals publisher so basically the concept is simple when we have a bean that implements the application event publisher aware interface it can be guaranteed that spring is going to call this method and it's going to provide a publisher handle and what we need to do is just take that publisher object and assign it to a local variable so that we can use that publisher object to publish as many number of events as we want and actually behind the scenes what's happening is this publisher that spring is going to give us is actually the application context you see the application context is an object that implements an application event publisher so when we say the publisher that publish event we are actually calling a method of the application context you could as well do application context art publisher and then get this done but what we are doing by using the application event publisher interface highlights one of the best practices we've already talked about coding to interfaces we need to use interfaces for all our dependencies and spring actually implements this philosophy throughout all the various design components and the internal workings of spring follow this concept as well so as you can see here the application event publisher functionality is implemented as an interface and you don't have to worry about the core object that actually contains the code to run this all we are worried about is using the publisher interface to publish an event we don't have to know this but this is what's happening behind the scenes it's actually the application context that's handed over to us in the guise of a publisher but we don't have to worry about it we just assume that it's an object that implements the application event publisher interface and then it we can use that object to use the publish event method and we can publish our own event so now this is all it takes to publish our own events now we have you know a piece of code in our beam in a spring beam that publishes a custom event this is an event that we have written and we also have an event listener that's listening for all events so earlier we got you know a container event when the you know the application context will initialize but now we are publishing our own event in our you know spring beam now let's have a look at how this works if I run this well there you can see drive in the code message is printed so what's actually happening is the context is initialized okay so the context message comes up over here then our code runs it's drawing a circle and then it's printed out this message so this is the contents of the draw method what we've also added to the draw method is an event publishing code so we have published the dry wind were here and then what happens is our event listener is going to listen for that as well so this method is called and the dry event is passed over here and then we are doing an event dot two string which means that this method is called and our custom code is executed in our custom event and that you know that message is output over here so this is an overview of how we can use the event handling functionality of spring and we can catch events we can listen to events which are published in the container and we can also write our own events and publish them has been required
Info
Channel: Java Brains
Views: 230,931
Rating: undefined out of 5
Keywords: spring, framework, dependency, injection, inversion, control, build, apache, tutorial, technology, computers, java, free, j2ee, ee, educational, eclipse, tomcat, javabrains, brains, koushik
Id: ZUe1Xz7DAcY
Channel Id: undefined
Length: 16min 16sec (976 seconds)
Published: Fri Jul 29 2011
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.