Learn Spring Events step-by-step | ApplicationListener | ApplicationEventPublisher | @EventListener

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome back and in this lesson we're gonna learn about spring events well this is gonna be a very simple topic and yet a very basic topic and we should be uh having some knowledge on this topic as a spring developer so in this episode let's try uh talking about you know how to create events in spring what is subscribers what is listeners uh what is what we call that uh publishers and all these things okay so this lesson that the one that i'm gonna create right now this is gonna be a very beginner friendly lesson so if you never heard about events you never worked with listeners maybe this is one of the video you can get started with that but if you have already have some work experience in some other framework like javascript or maybe if you have already worked with angular or react so maybe listener is a very common thing maybe you already have heard about it in that case please keep this video and watch the next video for some more in-depth knowledge but i'm creating this particular video as a prerequisite for my spring security course well there is no spring security knowledge is required to watch this particular video uh only the spring core knowledge is well enough to watch this video maybe if you only know what we call that dependence injection and auto wearing okay uh if you know what is the ioc container what is dependence injection what is uh uh you know uh what is uh what we call that how to create a configuration file in spring that's well enough to get started with this particular video uh so well i should have made this particular video during my uh spring core course but you know that the spring core course i have created around two three years back and of course at that time i didn't want to make things complicated but right now this is the time to give you some knowledge on this particular topic because we're gonna talk about some more and more advanced thing in spring security in the coming days and obviously this eventing or event is one of the most important chapter uh for for implement those kind of advanced uh thing uh when we're gonna implement that in our spring security course so hopefully you guys are excited don't want to talk much about it so if you are a complete beginner to spring uh events then just get started with that else you just skip to the next video in the list okay well i hope you have decided so let's get started all right and also i'll make sure that i am going to start the zoom recording so record to the cloud recording in progress okay i have started the recording as well okay let me get started all right so let me get started with the spring events okay why do we need this spring event and what is this spring event is all about okay um maybe you know so actually i was in a call and you know i did not get much time to you know what uh make some slides for you so i thought like you know how i'll be teaching you this thing and i just came up with an idea and just created few slides for you okay anyhow i have created for your slides and i'll be giving you a walkthrough about all these slides today and tomorrow and uh first of all okay let's not start from those spring events let's start from the events in what we call in ui so basically um let me open one of my text editor maybe i'll go for brackets and uh first of all we'll see events just right here okay so if any one of you have worked in any of any of the you know front-end framework um or in html or in javascript or in angular you know you would have a pretty much hour about the event but still i want to you know just give you an example let's imagine like we'll have a html page maybe i'll just create a new file and want to save it i will name it as event thought events.html enter and imagine i'll have a html tag and we also have a buddy and we also have a sorry this zoom stuff is popping out on my screen on one second so right now here we'll have a head tag okay and things like you know we have a head tag and body tag right so uh imagine we have something in the body for an example i'll be creating a label and there i want to create a text box let's say enter your name here okay and basically um let's say i'll be giving a text box here i'll be writing input okay and i'll be writing i'll be ending my input just right here and let's say type equal to text or let's say type equal to text because i want to create a text box and i want to give a placeholder just like you know your name okay and let's say let me give an id to this particular input box let me say you know name box okay ctrl s so now if i'm gonna run this so okay i have something on my screen so you guys can see that i have a text box let's say in the ui what do you mean by event okay think about like you know if i'm gonna you know click over here there's an event if i'm gonna moving my mouse this is an event if i'm gonna select this text this is an event if i'm gonna click over here that's an event let's say if i'll go to any of the website let's say i'm going to selenium express and let's say if i'm going to the selenium express website and let's say i'm selecting some text on the ui there's an event if i'm gonna click on the course that's an event if i'm gonna click on a button that's an event if i'm gonna click on this close box that's an event okay basically whatever we are doing in a particular moment there's an event i remember back in 2015 when i started working on some ui framework i asked my friend that okay what do you mean by event then he said um i think you know the people who understand hindi uh i mean you can you can get this line he said to me in a particular moment okay that's an event and i still remember this in a particular moment what what happened think like you know you are going somewhere right and you know you are attending an event that means there is an event happening somewhere some in a particular moment there is something happening there that's why we are calling it an event just like that whenever let's say i'm gonna click cover i'm gonna type here something and i am clicking on somewhere that's an event okay i think you are getting it if i'm selecting my text here this is an event so when that event is happening listen and user is typing something here and just clicking outside of the box there's an event is happening and at that time i want to do something okay so when that event happened what i want to do okay so we will see that for an example right now i'm entering some text and i'm clicking over here so basically one second there is some noise outside let it pass by there is some kind of announcement going on on the outside maybe because of the corona and all or something uh just give me a moment i because i'm recording this and you know i don't want this voice to be coming right in the recording maybe i can edit that out okay it's gone okay are you guys hearing any record i mean uh any background noise from me no no all right oh it's completely good perfect okay so one can i ask you one thing i know it's too early to ask but just i thought uh so you are explaining even though when playing if something happened that we should consider event or even simply we are clicking on this screen and nothing is happening it's not important okay as of now there is no so for an example i'm clicking over here that's an event you are correct i'm selecting this text that's an event right i i'm going to show you something right now let's say why i'm talking about it yeah yeah i wanted to check what is today's topic basically okay okay today's topic is about spring event okay and the reason why i'm telling you because i want to let you know about some of the spring default events and some some of the events that spring bob list and so later on the day i can also show you some implementation maybe about maybe um some logging event or some you know spring security event or some spring context event and things like that okay so you know first of all you know i just want to start the event stuff um i just want to let you know what the event in the ui is i think most of you should familiar with that so that's why i'm just starting from the ui example and that's the only reason for an example i've just created a text box here and for an example i have a requirement whenever you are typing something just right here and you're clicking somewhere outside of the box you want whatever you typed here that should be converted to uppercase so you want whenever the user is typing something when it's clicking outside of the box something to happen let's say i click over here then i want something to happen and how can i do that for an example i'll go over here and i want when someone is gonna enter something in the text box and want to right um do a click let's say on click okay i can fire an event on mouse over i can find an event on mouse move i can find an event on change i can find an event like you know whenever the text changed i want to find an event let's let's go with on change okay i just want to show you a simple thing imagine there is a text box and there is some text getting changed on that and whenever some text changed happen i want to fire an event and i want to handle that event in javascript if you don't know even javascript i'm just showing you this example just to make you familiar what event is then i'll go to the server side for example here imagine in the on chains i have a function let's say my function okay and let's say i want to create this my function inside a script so i'll come over here i'll just write script and i will say i'll just create a function called my function okay and this is gonna be a method or function how do i define functions in javascript i'll just write function here okay so what i want whenever some changes will be done inside this text box i want this my function to be get called and here inside this function i want to get this particular name box whatever the value that i'm typing uh imagine whatever the value that i'm typing just right here i want to capture that value and i have to change that value to uppercase let's say someone is changing the value here he's clicking over here and on change i want to convert this value to the upper case so the way we handle that in the javascript is first of all in this text box there is something happening so maybe this the id of the text boxes name box i'm gonna copy that and i'm gonna do document dot why i am not um getting any suggestion document because you haven't included yes no that's not the problem i think i should get over here uh document okay let me type it document dot in the head tag only i have given the script um one second okay so document okay now i'm getting can i okay there we go document dot get element by id and i want to get the element the ui element called name box which is my text box i want to get the value that i have inside that and i want to store this inside a variable let's say a var let's say a name okay so what i want and uh whatever the name that i got here i want to change it to uppercase so the way i'll be doing it is slim dot value and i want to initialize this name that value to name dot value dot to uppercase okay so uh right now what i want if you're gonna go to your page right now if you're gonna refresh the space let's say i'm gonna type something in i'm gonna click over here see the name or whatever i have typed is changed to uppercase let's say i'm writing obvious in small small case and that over here and takes change to the upper case so whenever you are writing something just right here and whenever you are clicking outside of the box the text changed happened and an event is getting fired and there you go all right and your text is getting changed so that's how you can imagine like this text box whatever the text box and the text that you are entering just right over here this is some kind of object and whenever there is some changes happening to the object and whenever you are clicking somewhere else and an event is getting fired and there is a function who is listening to that event and that function is my function which is defined just right here and this my function is doing what it is just changing the text to the upper case so a event is happening for an example i'm just typing something over here then whenever i click on somewhere i'm gonna do a click over here then the browser is publishing that event right whenever i click somewhere the browser detect my movement and publishing that particular event and there is a listener who is listening to this this function you can treat this as a listener is listening to that event and changing the value to the upper case okay so this is an ui and you know i think that a lot of you can you know relate to this thing maybe there is so much probability that you have done some kind of things just like this before and just want to bring this one just for yourself like you know for your understanding but right now okay why do we need events in the server side okay and what is the benefit of server-side events okay so let me just give you an example so i'll just do what um uh okay let me give you a very simple example the slides that i have created so imagine okay i'm a fan of this okay the big bang theory okay for an example this is an event okay this event happens okay where this event happens so this is an event because this is a show comes in a particular time and imagine like you know there is some event is happening and that's that is big bang theory and there is a guy who published this event and that's your broadcaster for an example you know in india most probably you know the big bang theory i know that's my the stream in uh g cafe all right so g cafe is the publisher which is publishing that event okay on a particular time let's say 8 30 to 9 30 in the evening the big bang theory will come on tv so the g cafe is publishing that event and we are the listeners right because we have subscribed to that event we we want to listen to that event which is happening and basically we are just right here listening to our publisher which is g cafe whenever it will publish that event we will go and listen to those event okay it's pretty simple so big bang theory is our event okay we want to watch this so and to watch this so we need a publisher which is going to be publishing this event and for that we have the g cafe sd which is going to be our event publisher and we are the listeners who are the fan of big bang theory and want to watch that or listen to that okay whenever the publisher is going to publish the event list at 9 30 this is going to be publishing that event so a lot of people are listening to that event and as soon as they will be publishing that event will be starting uh will be uh will be starting uh listening to that particular event okay hopefully it's making sense and there are so many technical stuffs are there and i'm gonna come back to all this stuff tomorrow but let's let's first try and understand what the heck this event stuff is and what is the use case of this why do we why do you i mean why should i use this okay what the heck is this and i mean what is the benefit of using this particular even stuff okay so first of all let me go to my eclipse okay here comes your spring stuff okay this is all to set you off like like okay just to create an impression like what is the event and more about this event will be understanding tomorrow and the people who are attending this session okay make sure you're gonna practice the same thing today and tomorrow whenever i'll be doing some more advanced stuff you'll be able to understand everything so i'll be just taking a very simple example the example that we have seen the big bang theory which is an event g cafe which is our publisher and we are the people let's say anu let's say uh willie let's say akil let's say aditya let's say a village we are the listeners okay who is listening to that particular event okay i'm going to show you an example okay so first of all i have created a project maven project just to save some time and um i have a pom.xml i don't have anything just right here even i don't need junit you can just remove this mavin has automatically created this for me and um we will be doing the same thing in springboot so whoever i think almost all of you who are attending this session we we have springboard covered in a batch so we'll be doing the same thing in springboot also but i will be also using this recording from my for my other batches and i'll also make sure to make this video freely available everywhere because this is basically a very very simple and very straightforward topic and which we should be covered in spring core okay so that's why i can see the only dependency that i have here is spring context okay so the spring context dependency is just right here and this spring context dependency will get you all the spring course charts that you will be needing for an example spring context aop bean score you know you know all spring expression and the other stuff okay the more though i only care about the spring context and spring beans jar okay because i just want to do some very very simple thing okay now let's try to understand imagine let me create a new package okay in this pack is imagine i have something called publisher okay um publisher package okay so inside this publisher let's say there is a channel called you know g cafe let's say g cafe publisher okay i'm calling i'm just telling it a publisher because a lot of store comes in inside the g cafe publisher inside g cafe channel there a lot of different show comes in okay and gcap is basically publishing those shows or streaming those shows and that's why i'm just writing here publisher here is just creating information for you that okay i'm treating g cafe as a publisher okay so if i'll be going to this g cafe publisher imagine we have a method called public point uh stream big bang theory big fang theory okay stream or broadcast big bang theory okay and uh or i can say publish big bang theory whatever you want to say that's fine imagine it's gonna taking a you know what we call uh a episode number which episode you want to run let's say episode okay episode number maybe you know what uh it will just give you the i mean you'll just pass in the episode number and it is gonna say it's just out okay starting ours um g cafe okay uh streaming or starting pbt stands for big bang theory and the episode number starting bbt episode or i'll just put the episode number directly this right here okay a simple method called gcat publisher got a method called stream big bang theory and we got ss out here called you know starting big bang theory episode number whatever you passed in and imagine this is a component okay this is a very very uh you know very simple spring component i think you all are good with spring right now you understand whatever i have done here now imagine i have a config package i'm gonna create a class and this is gonna be my config class so maybe i'm gonna click over here do a right click new and class and i want to create a um package called config and let's say there is a class called app okay let's say app config that will be that will be better okay ctrl s now hover over here rename the thing okay now here i don't want to do anything i just want to make it a configuration file and also i want to make it i want to make sure that i have enabled the component scanning so component scan and i want to scan for all the packages so i'll be just writing here com all right there we go all right so basically right now our app config is gonna component scan for all the packages it's gonna start with com as simple i mean as simple as that okay perfect so right now what i'm gonna do i'm gonna close everything okay and i will go to one of my package i have a simple classical app created by mavin and right here what i'm gonna do i am going to load my container so i'll say application application context which is just a spring container and i'll say context is equal to new class path or my you know i'm i'm using annotation best stuff let's say annotation annotation config annotation config application context i think that's the right one and i want to register for my app dot config class app config.class okay there we go so right now i have just started my container here and i'm gonna say context dot get bean and i want to get the bean of gcap a publisher and that's my component so i'll be going to gcape publisher just right here and this is my component let me copy that this is my component name by default you know that if i if i'll not be mentioning the component name the class name is gonna be the component name with the first character as small so i'm gonna make the first character as small and this is gonna get me a bin called g cafe publisher and dot class there we go let me just maximize this and that's my bean and i'm going to do a control one and send the statement to a local variable and there you go i have the gcappy publisher object just right here now i can just do being dot you know stream big bang theory let's say a big bang right now g cafe is streaming the big bang theory with the episode number let's say ep stands for episode and let's say zero zero four it's just streaming the episode number zero zero four okay so now think about this if i'm gonna run this uh how i'm gonna run this can i do right click run is java application okay there you go nothing fancy here g cafe starting big bang theory episode number one or episode number four you understand that right now g cafe which a publisher is publishing i mean is trying it has a method and it's basically streaming the big bang theory right now let's say we have a lot of people here let's say avilash is a fan of big bang theory and he's a listener or he's a subscriber of the big bang theory so what he's gonna do let's say i will go to one one of the packets and i'll create a class okay and this that that that will be let's say a village okay subscriber or i can say of the last listener okay let's say obviously listener and this is gonna be the this is gonna be i'll be putting everything inside a package called uh listener okay i'll do a finish let's say avilash is a listener who is going to listen to the event that g cafe is publishing right for example right now i have subscribed to g cafe and i have a method called public void watch bbt or big bang theory watch big bang okay or what's or watch big bang whatever you call that okay imagine right now i have a method here called sis out and let's say i'm going to say watch big bang let's say it is also taking episode number string ep number and let's say i have a simple print challenges right here let's say watching um let's say watching bbt plus episode number okay let's say right now velaz is watching the episode number uh something watching bbt episode number and let's say this is another says out sis out let's say avilas started watching okay um bbt okay big bang theory okay and he's watching the let's say not watching let's say playing bbt okay who is playing the last is playing so let me give avilash playing bbt and avila started watching bbt okay perfect so now imagine this is one of our listener or one of our subscriber who who has subscribed to the g cafe just to watch the big bang theory okay so right now the big bang theory whenever it is going to be started streaming it is going to notify a village that hey the big bang theory has been started so that avilas can start watching that particular episode okay and if you want to do that for an example right now tell me the g cafe okay just right here how it is going to notify or how it is going to call this watch big bang method okay how it is going to notify a village listener that hey that we have started streaming it you may start watching it just right now can anybody know how how basically i can implement that imagine this is a simple component called publisher gcafe publisher i want to call this particular method called was big bang just right here how i'll be doing it anyone can auto wear that perfect that's fine we have to automatically first of all to auto will auto wear this component i have to make this a component i i will be letting spring to manage this particular object and then i'll come to the g cafe i'll say let's say a village listener or whatever avilas listener and there is a typo there okay that's just fine i think you guys won't judge me and i'll say let's say obvi um let's say subscriber or listener let me just copy this paste it over here make it a small case and there we go obvious listener and i can simply make it auto weird and i can make it a private variable and i can just to right now i can just notify a velocity just like avila's listener dot you know watch big bang and i can also pass in the episode number that we are having over here to here there you go right now if i'm gonna go ahead to my main application which is after java i'm gonna run this nothing fancy here you know i will as well start watching that particular thing uh so g cafe starting big bang episode zero zero four avila started watching bbt and playing bbt ep004 okay because that's the episode the gcf has started streaming okay imagine i have one more subscriber okay or one more listener i am copying this let's say my next listener is willy okay let's say willie listener okay now let's say he's also a fan of bbt and right now let's say watching big bang let's say right now this willy listener started watching bbt and he started playing bbt episode number something okay so right now what i have to do let's say i have one more listener right now really you can only answer let's say i have one more listener here right now again i want to notify this listener to watch big bang whenever this publisher whenever this gcap publisher is publishing this or streaming this big bang theory so what i have to do here what did you want me to do i'll stop also imagine imagine right now we have we have this watch big bang method and this method is inside the willy listener okay and this is a component so it's registered with your spring application context right now i want let's say you are also a subscriber to big bang i am also a subscriber to big bang okay so when my publisher which is g cap a publisher is publishing big bang event or streaming the big bank event i want you to notify it or g cafe is one to notify you so the way he has notified me the same way it can notify you as well isn't it making sense what i'm trying to say really or is it okay there is a design pattern called observer design pattern i don't want to get into that but let me just score this and then i'll ask you again let's say i want to notify willie listener and what i'll do i'll create an object just right here holy listener let's say copy this paste it over here and make it a private variable and let me make it really listener make it auto weird and now i'm gonna do a ctrl shift or to import it copy this and paste it over here dot watch big bang and pass in the episode number that i'm getting from here to here ctrl v ctrl s run this application go to the update java here i'm creating a bean of g publisher and right now this with this bean i'm just calling this stream big bang theory in the stream big bang theory in the inside my g cap a publisher it is basically you know starting the bbt you know i just have a lock imagine like i start the episode and it's notified the user village and it's notified the user really to watch the big bang theory right now if i'm gonna go to the application.java and if i'm gonna make it run so you can see in my console i'll be having a village user started watching bbt and playing the episode zero zero four and willie user is started watching bbt and playing the episode zero zero four is that making sense right now what i have done here so far yeah that makes sense okay uh what about you guys well as of now we guys are on the same page what i what i have done here and what i'm trying to do you guys are okay with this what i have done here coding wise you are fine i'm gonna come back don't don't think about the event publisher subscriber listener and all these things whatever the code that i have done is that making sense is simple spring core coding yes okay yes what about other i just want to hear it from everyone yes sir all right perfect perfect so okay right now another time i'll be asking you one more thing okay imagine right now anu is one more let's say tomorrow anu has taken the subscription or yosh has taken the subscription okay so what you will be doing you will be creating one more class right here right let's say let's say aditya listener okay and inside the aditya listener let's say he has also subscribed to this then again you have to make it a component let's say i'm going to change this name right here to aditya started watching bbt and as it just started playing bbt episode number whatever then again this this is a listener who is going to listen to that particular event and so that i have to go to that publisher and i have to again hard code it isn't it otherwise how i'll be notifying that particular guy who has taken the subscription so i will be just writing here aditya listener and let me copy this and paste it just right here and we'll make it auto where okay and i'm gonna say a detail listener okay and i'm gonna call that method i did the listener dot watch big bang and i'll pass in the episode number now you imag you just think about the problem it's just not i mean just try to notice the problem what is the problem which is happening here okay let's say i'm gonna put episode number here and if i'm gonna run my application then obviously it's gonna be started running and it is gonna notify all of the all of the you know listener like avilas philly and aditya all are going to get a notification and they started watching the episode let's say he's watching willie is watching of the last is watching making sense so far and just try to try to understand the problem here every time we are adding a new listener just like a village aditya willy okay all these listeners i i have to go to my publisher and my publisher is right now knowing about my listeners okay and that's how when this particular method is getting started i'm just running something and inside this method it is tried calling my listener method okay for an example this is a listener that i have just created a listener called a village listener i'm just calling it as a listener you don't get confused with the name the name is only listener think about avilas is the subscriber and right now he is watching big bang and you know there are some blogs just right here so this is the method called was big bang this method is getting called from the publisher just right here because you have subscribed or you are a fan of big bang and you have subscribed to g cafe to watch big bang that's why when gcape started streaming big bang theory it is calling the watch big bang method of publish listener was big bang method of the willie listener and was big bang of the alita listener okay but the problem here is that there can be so many listeners isn't it unless there are thousands of listener who has taken the subscription of g cafe just to watch the big bang theory and if you as a publisher let's say g cafe the publisher is started uh doing things like this it will be so much tight coupling and every time a new listener is going to be added we have to put that entry just right here we have to make it auto where and we have to start calling that particular method isn't it and that's how the that's how there is a you know design pattern called observer design pattern okay and i mean that has been implemented with the spring framework as well and i don't want to get into the design pattern the observer design pattern if you know about this that's a different thing but i don't want to get into that i will show you a very simple thing okay right now to get rid of this particular problem okay the publisher is directly having the dependency of the listeners okay like the subscribers available in aditya and he is notifying all the subscribers by you know by itself and that's what i don't want to do just right here this is why i have removed all the tight coupling that i have done previously i will do a control shift o to you know get rid of this particular problem right now to to solve this particular problem because there can be many listeners okay so do can i do something so this all these listeners that you have here like aditya like really like avilash right all these listeners will keep listening for an event okay the g cafe publisher will say hey there is an event happening called big bang theory whenever whenever this particular method will be called it is going to publish an event and all these listeners will a villas aditya will be keep listening to that event that gcape is publishing and all will be started watching automatically and how can i implement that for an example you know i'll just show you an very simple thing right now so to create an event in spring framework what you have to do you have to create a class let me create a class over here let me put everything inside a package called event or events let's say i want to create an event called the big bang theory event big bang theory event okay let's say i have an event here called big bang theory right now what i'm gonna do i'm gonna do a finish it's gonna be a simple pojo and if i want to make it an event i have to extend to a class called application event there you go application event okay so application event is a class if you want to create an event in spring framework then you have to you know extend to this particular abstract class and in latest version of spring even you don't need to extend to this particular class but i want to come from the very beginning so that any kind of code that you will be seeing in future it will make sense to you okay let's say application event there is something just right here and um you know we have to just extend to this particular class which is the abstract class so i'll be closing this i'll be going to my big bang theory event because i'll be creating an event right now called big bang theory and this extends to application application event and imagine there is a variable here called private int what we call that let's say let's say private string episode number okay and i'll have the getter and setter for this create category and setter basically i don't need the setter because let's initialize this episode number with a constructor so i'll hover over here first of all i'll add the constructor okay there is a there is a constructor i mean inside the application event there is one constructor called application event which takes a source right now i'll be telling you what is what is the meaning of the source and what do we need that but there is something called i mean there is something called source and that's what it stacks a text as a parameter which is an object type and basically um the logic you know is basically called the super source method and it's going to set that particular source object i really don't need to care about that right now i have just click over here over it in this particular constructor and that's it so i won't be doing anything just right here okay so right now i will be creating one more constructor here to initialize this particular episode number okay so imagine i have this variable and i want one more constructor so i'll do right click source generate constructor using field and i'll be clicking over here to generate and it's going to create me one more constructor which takes source as a parameter which is coming from the super class and the string episode number just care about this right now i'll be coming back to this object source what it does so right now this episode number that i have to this episode number like you know whatever episode number i'll be passing it is gonna initialize that episode number this episode number value it will be initializing so i'll be setting the episode number value by using this particular constructor and that's it okay i have a variable and i have a constructor which takes two parameter which is called object source and this source will be called i mean it will be calling the super class constructor it will be clicking over here it will be going to the super class constructor called application event and there it will take the source and it will again call the super class constructor that's the framework stuff and we'll be coming back to that later and then one more thing is that we have one variable here called episode number that episode number will be passing and we'll be initializing to this episode number that's it right now what i want i'll be closing this and i have already created my application event or an event in spring so this is how we create event in spring we just need to extends to a class called application event and that's it your class is automatically eligible for eventing let me close that okay now i'll go to the publisher and what i want right now this publisher to do whenever someone is going to call this method right now let's say in the app.java we are creating the g cafe publisher bean and we are creating that instance and right now we are calling this stream big bang theory method if i do control click over here it is coming to g cafe right we are we are inside this method called stream big bang theory right now whenever this method will be called i want this particular method should publish an event okay how i'll be doing it i'll be for that i'll be just writing a uh reference here called application um application event publisher and i'll say application event publisher and there you go and i'll make it auto where this object will be by default will be given by spring okay so there is a new uh you know um class right now that we have seen sorry an interface that we have seen called application event publisher and spring will give inject this particular object to here right now what i want when someone is going to call this stream stream big bang theory method what i'll be doing i'll be doing application event publisher dot publish okay publish event and you can see that i have a particular constructor vista application event as an argument right so if you if you remember the event that we have just created what is that control shift r star event big bang theory event this is a type of application application event right so right now this is an event right so what i want whenever the cafe or g cafe publisher you know want to notify all the people who are going to subscribe to this particular you know big bang theory episode all those people will be notified okay so the publisher is going to do what it is going to publish an event and it's gonna say that hey we have published an event right now so if anyone wants to listen to this event you guys can listen to this okay that's a different thing first of all let's publish the event what is our event our event name is new what is that uh the big bang theory event the big bang theory event which text two parameter the first parameter source i told you that i'll be come back to this for now i'll be just passing in the current object which is this and the episode number whatever the episode number will be getting from here we'll be just giving it to here then this constructor will take this to parameter the source will be set to here the current object we are passing over here called this and episode number we are giving it four or five or something that will be initialized to this episode number pretty simple so that's that's it so right now that's my code inside my publisher here i'm doing what i'm publishing an event and that event is my big bang theory event which is my application event that i have just created okay so right now you will see and magic right so i have not done anything the g cap a publisher right now publishing an event and event of big bang theory right now let's say this willy listener he has subscribed g cafe to uh to watch big bang theory so he will do what he will implement an interface called application application listener it is going to listen to an event and you can see the application listener is extends i mean the type is exchange to event so any class or any in any class which extends to application event uh basically we can put that particular reference here it's generic type right so what what i'll do here i'll come back to here and i'll just write here inside the application event so what is the event this really listener wants to listen to it wants to listen to an event called the big bang theory event okay there we go so right now this particular uh method i mean interface has a method called if i'll be implementing this right here okay that method name is called on application event which is gonna get you the event the event like you know the big bang theory event is right now our event that this guy wants to listen to so this object will come over here and this is an event the event is the variable okay so now what i'll be doing automatically whenever an event will be fired right now you can imagine like the z-cap a publisher whenever it is going to publish the event the listener which is willie who is a subscriber to the big bang theory he is i mean this particular listener will be automatically start listening to that event that hey gcape has published that event so right now this method will be executed and if this method will be executed i can basically do what i can i can just call this method just right here okay watch big bang and right now here this watch big bang text say episode number so from this event so this is our big bang theory event right here we'll get the big bang theory object that particular event object will do event dot get episode number and here the complete access to that particular object the event object will have and from that i'm just getting the episode number right control s to that right now we have one more listener called aditya listener and here also i'll be doing the same thing let's say he is also subscribed to big bang theory so let's say i'll say implements let's say um application listener the addition also going to listen to the big bang theory event so the big bang theory event i have to override the method unimplemented method here i'll get the event called big bang theory event whenever that event will be published by the publisher that event i can hear it just right now and i will be doing here let's say i'll be calling this method only so this method will be automatically executed and inside this method i want to call the watch bigbang method and i'll be passing in the episode number this episode number i'll be getting from here so i'll be writing event and i will be just writing here you know dot get episode number so right now similarly i i think you can understand this right whenever this z gcap publisher is publishing an event it is creating a new event and this event is the the big bang theory event and here it is passing in the source which is a current object and also it is passing in the episode number and this episode number we are getting it from here so whenever imagine in the app.java someone i mean the g kappa publisher is publishing the event right now and here it is giving the episode number zero zero four now the stream big bang theory method will be called it will be going over here it will get the episode number right here and with this episode number i'll be passing to the event so right now the big bang theory event will be initialized and this event will be occurred and once this event will be occurred the episode number right now whatever we passed there it's gonna be ep004 and zero zero four and this object right now we'll be getting with all our listener our listener will be notified that the event has already been occurred or published by the publisher now the willy listener can access that event object just right here that event object has already been initialized and i i'm just getting the episode number from that event same thing inside the aditya listener it is also implementing the application listener which is called the big bang theory event and right now um that object i am getting over here this object has already been initialized and i am just getting the episode number right here okay and the same thing i'll be just doing let's say obilash listener let's say i'm also a fan of big bang theory so i'm gonna do implements and what is that it is going to be a listener or subscriber so i'm gonna say application listener and also it is going to listen to the big bang theory event and if it is the case then the listener need to be implement an unimplemented method and here that object will become once the event has been triggered that event object i'll have access to here and right now i want to call this watch bigbang method control b okay and this was big bang method needs an episode number as an argument so that episode number i can get from the event get episode number okay how why i'm able to get the episode number from this event from this d big bang theory event because this the big bang theory event has already been triggered here the object we have already created here and we have created this object with this uh variables and the episode number we already got because in the app.java whenever we are streaming the big bang theory we are passing this episode number and with this episode number only the object is getting created of the the big bang theory event and once this event has been published the listeners are right now keep listening to that particular event the big bang theory event willie listener is listening to aditya listener is listening to the big bang theory event the you know a village listener is also listening to the big bang theory event okay right now if you see something if i'll go to the g cap a publisher see i don't have anything right here and i i will be doing right now what i'll be going to after java i'll do a right click run is and java application and you will be seeing something is happening so available available started watching bbt episode number four willy has started watching bbt episode number four what about aditya because aditya is right now not you know saved this particular event has i mean i have i have not saved this particular file let me go to update java let me run this particular application again and there you go all your subscribers willie aditya villas all are started listening to the event that the gcap publisher is right now publishing right now see it has a simple method called stream big bang theory it is it has it is doing something let's say i have a simple log here called starting big bang theory and right now it is publishing an event it is not doing anything else it is just publishing the big bang theory event right now each job is done the publisher does not need to know who has subscribed to him i do not know how many people have subscribed to me right a newspaper agency doesn't know how much people have subscribed to the newspaper getting it so right now as a publisher we are not we are not making any tight coupling the way we used to do previously we used to put all our listeners objectives right now so right now all our all our i mean my my publisher is just publishing the event and the listeners are just listening to the event which is getting triggered okay this is what i want to say so so far how much quality you have 20 percent 30 percent 40 percent i mean yeah i have some doubt on that um publisher like this i'm not getting what values this is containing yeah this this will contain a value of application context right now okay with that uh okay i will be coming back to that but for if you have a curiosity in the app.java here we have the application context object this is containing annotation config application context right this is the source so we'll be getting i'll be coming back to that don't worry about it but this this will contain the application context object just right here okay but we will be just removing we will be removing it yeah i'll be telling you i because i have not come i have not discussed this yet i'll be coming back to this and also we'll be removing this this we don't need this this in latest version of spring you if you want to i just want to show you one thing in the latest version of spring if you want to make an event if you want to make something an event for an example the big bang theory if you want to convert this an even i'll make it even more simpler for you i mean you don't if you don't want to you know if you don't want to make it application event okay if you if you don't want to extend to this if you are extending to this that's where we're getting the source right i'll be removing this extent i'll it'll be even more simpler right now so i'll be removing this constructor i'll have let's say one variable okay i don't need this guy now it looks good this looks easy ctrl shift o now see simple pojo only we have we have an episode number here okay and we have a a constructor okay which initialize this episode number and we have a get episode number which is going to return the value of this episode number okay simple pojo and now we can also fix this zcap a publisher what problem we have here okay right now the problem here is that the the big bang theory event right now we don't have this parameter we are just simply passing the episode number this is also fixed the next thing is the listeners are giving me a problem let's go to the listeners okay right now this is listener okay this is implementing the application context listener and obviously i told you the application context listener if you see over here this extends i mean we have to pass in a type and this type should be a should be extends to application context event and right now if you see here the the big bang theory is not extends to application context event and that's sorry application event and that's why this is this listener is right now complaining i can simply remove this okay and the way right now i i right now if i have removed this right now this is no longer a listener right if i want to make it a listener i'll be doing what i'll be over i'll be just remove this overwrite and i will use the annotation called listen event listener that's it right now you can make the method name as anything that you want whatever name you want to give you can give it just right here now the thing is that this method will listen to the event called the big bang theory event okay now control shift o this will look even more simpler to you we'll come back to this again willie listener right now let me go to willy listener it doesn't need to implement this application uh listener it will be more simple even if you want to make it a listener just put it at event listener there we go and also we have one more listener called avilas listener no one needs to implement this application listener if you don't want to okay so that you know um you can make this thing even more simplified by writing event listener okay same work this is also going to do as i mean as the interface i'll do control shift o to format this now the code is even more simpler anyhow i'll be talking about that source because that is important what do you mean by that but right now for now you just think like the gcap let's come from after java again okay we have an app we're creating our application context okay we are getting the bean of gcap a publisher okay and the gcapper publisher is right now streaming big bang theory episode number four right now we are going to stream big bang theory it's a simple method okay now it is doing something imagine it's doing some business logic just right here now we are just publishing the event we're just saying application event publisher which is the uh which is the interface that we need to publish an event in spring okay right now this event publisher is publishing an event called the big bang theory event and is passing in the episode number that we are sending from uh up from after java just this episode number we're just getting right here now this big bang theory event has been initialized to episode number zero zero four right now this g cappy publisher has published an event and right now all the listeners like avilash listener who is listening to that event right now this particular method will be triggered and here whatever the work that you want to do this work you can keep doing this right here okay for example the watch big bang this method is going to be get triggered here okay hopefully it's making sense the same thing it will go to the willie listener he is also listening to the event you can method you can let's say listen to event okay i mean you can just give any name to this particular method but you will get that event object i mean whatever the event object you are expecting give that event object just right over here you will get that event object right here so that whenever watch big bang is going to be called if you want to extract some value from this event just like the watch big bang um this particular method needs an episode number you can pass in the episode number just right here okay and do a control shift or to remove unnecessary imports and edit the listener also you know this method name can be anything let's say listen to event okay something okay right now okay this is it is it looking good the core simple okay right now okay now let me go to app.java let me do right click run is job application now you see what happened okay willie aditya and avilas all these people are listening to the event let's say tomorrow one more listener one more subscriber that i have right now this gcap publisher doesn't know who that particular subscriber is it is just going to publish the event and then let's say one more subscriber is there let's say i'm going to copy this editor the input parameters for the line number 19 for the publish even method what are the that that's an event that's the event that you want to fire here our event will be now that that will be the event object yes and right now for example right now i want to make it a kill listener and i'll make it okay listener okay i'll go to okay listener and i'll go over here and right now everything will be remain same and i don't have to do anything okay listener i'll just change this name to octo so that control c control v control v there you go okay one more new subscriber has been added so one more new listener or one one more new subscriber is there who is also a panel big bank so there you go the same method just created one listener added that with event listener there you go i'll get the event and he'll watch the same episode let me go to the gcape publisher okay it is just going to publish the event it really doesn't know how many subscribers it does have so right now if i'll go to after java and if i'm gonna run this the things will run fine okay now you see willie is watching akil is watching episode four adita is watching or village is working i mean watching the episode so how much comfortable you are with this eventing stuff so far i'll just take 20 minutes more i'm expecting an answer alphabetically right yeah the ordering you were saying okay yeah uh the ordering i'll come back to that uh yes i will just been very initial stays even i am not assuming that you you guys understand all the things that i'm telling you know i'll be giving you a lot of different things after this session as well tomorrow day after tomorrow we'll be talking about this if you guys want to join from any different batch you can join the same call and we'll be talking about eventing only okay adita you are good really there yeah i'm good can i see your uh event class one more time yeah sure yeah so big bang theory event that's a simple pojo nothing nothing more right there i haven't even uh extends to the application event okay that's the old way of doing it in the newer spring version just as spring pojo can do the eventing work for you okay let's see okay uh okay we'll we'll just you know do one more thing for an example this gcape publisher has one more show okay for an example public voyage stream tell me one more show i love community circus community circus okay unless there is one more so it has okay the g capital publisher has one more show and in this so let's say i have a sis out just right here copy this paste it over here and i'll give it a value here i'm gonna say the value as let's say string episode number okay and i'm gonna pass in this episode number right here as well okay so right now there we go i have one more method so how can i call this particular bean very simple i'll go to app.java and i'm gonna do bean dot you know what is that stream committee circus and i'm going to pass an episode number called ep and 0 0 7 let's say the episode number which is currently streaming for committee's hackers is 0 0 7. if you're gonna run this application i think it is also going to fire the cafe uh starting bbt okay sorry so inside the committee circus let's say starting comedy circus okay and episode something right now you go to your application run this and you see that okay gcap here starting committee circles episode zero zero seven right now let's say whenever uh there are some people who have subscribed to the committee circles event okay they want to they want to uh they want to know when the committee circus is getting started they all want to watch committee circus all right so if that's the thing that i want to do i i will be doing what i won't be hard-coding anything i will be just publishing an event just right here for an example i'll go to my event class event package where is that even i'm going to create a new event here called um what is that committee circus event i'll do a finish it will be a simple pojo and i'll just write private string um let's say episode number okay i'll just create a getter and setter for this control s create get a render okay and i will be also creating a constructor right click source um where is that constructor stuff constructed using field i'll do generate there we go okay that's it i have an event right now okay so now this simple pojo i i will be doing what i'll be going to my gcap a publisher and whenever this method will be called i also want to publish an event so i'll be just doing how i'll be publishing an event by using this application event publisher copy that paste it over here sorry where did i go paste it right here and you just do dot publish event okay and here we'll be able to publish an event and you can see right now if i'm gonna do a control space right now it takes two parameter i mean two two constructor we have one stack say application event right now obviously our event classes are not extending to application event so we will be using this constructor right which takes a simple object okay and right now you know that every classes are the parent of the object class so right now this event that i'll be firing the new committee circus event okay this event that i'll be firing right now is obviously can be stored inside the object i can write object obj qt new committee circuits of new committee circus event isn't it every class is extends to the object class and they can store that particular object and obviously whatever the episode number i'm getting i'll be just giving this right here and there you go i'm done with publishing the event right now as a publisher my job is done now the people who will be listening to the committee circus event let's say there are few people have subscribed to comedy circus let me uh go to one um let me just copy paste one class of last listener copy and paste and change it to let's say you know let's say you know just team um what i'll be giving the name one second uh listener ctrl c did i copy this class only i think so let me just change the name to josh 10 listener and there you go and this listener imagine this listen listener that we have created let's say right now here we'll change the method name let's say watch big bang let's say watch comedy circus okay and let's say right now justin copy that has started watching comedy circus so i'm gonna copy this name comedy circus copy and justin has started watching comedy circus and playing justin is playing committee circus episode number whatever pass to this and right here the event listener will be changed right now now which event we want to listen to now this guy this listener want to listen to the event which happens when the committee circles i mean when the committee circles event get triggered by the gcape publisher and now the gcappy publisher is you know publishing this event now he want to listen to this event he doesn't want to listen to this event he's a fan of this committee circus now i'll be copying to this one i'll be going to justin listener and here i'll be listening to the committee circles event i'll do a ctrl shift or to have the import and right now this method will be a listener the method name can be anything you can just say listen to event whatever you want to say uh so that's fine and now here what i want to do i want to just call this particular method okay and there you go you will get the data from this event because this when this will be initialized the data will be there you'll just get the episode number and there you go okay now you can go to your update java do a control s and run this application okay and there you go now gcafe is also starting the committee circus right now only one guy is listening to that event and you know that guy kept listening if you have more events okay you can create more classes or more listener classes for community circles and they will be listening to the committee circus event you just need to make sure that this this event listener you have to annotate with this you have to create a class you have to make it a component because this need to be a spring component and the listener need to be registered with your application context and then you have to create a method the method name could be anything you have to annotate that with the event listener then whatever the event you want to listen to put the class name just right here and you know you can also get the data from this event whatever the event specific data you want to capture in this case inside this community circus event we have an episode number and this data i want to capture so if i'll be going back i'm capturing the data from this event variable right here making sense yes cool what about others yes perfect i mean you you are okay okay this is good perfect okay so i'm happy then so so right now this is all about eventing right this is something you know very very simple thing i have done i've just created multiple events and we are just listening to that and tomorrow we'll be looking at some default events that spring has and also uh once you guys are feeling a little more comfortable with this eventing stuff i'll be going to you know what i'll be going to the um i'll be letting you know how how you can implement two-factor authentication how you can implement account locking in your spring security a lot of things you can do right you can just keep listening to that event when that exception is occurring when the transaction is getting failure okay for an example think like you know think like a scenario i have written over here so i'm here here imagine you know whenever someone is you know ordering a new course okay i at the same time he's ordering the new course a message or an email should come to me and i mean and notify i should be notified the instructor whenever a new course is getting ordered from the website the instructor should be notified okay as soon as the money is getting credited though the user should be notified with an email and an sms right when someone subscribes to your channel send them an welcome email let's say someone is subscribing i mean as soon as the subscription happen trigger the listener should send the email to the to the subscriber the listener there should be another listener who should be sending an sms to the subscriber okay so that can be many listeners imagine like whenever a transaction failure happened whenever you're doing some bank transaction and some transaction failure happened emails would be automatically triggered and that that email should be a listener we should be keep listening to the transaction failure to happen and once that happened it will be triggered an email to you okay so that kind of thing tomorrow will be doing and tomorrow we'll be taking it forward okay and i'll be telling you some more interesting thing and then i'll be telling you the exact reason why i'm telling you about about the vending stuff right now because i want you guys to implement those complex spring security stuff and this is the prerequisite and also if you know about the observer design pattern this will be very very simple for you but i don't want to get into that right now okay cool so if you guys are okay yeah in i can go back to the code once and show the config class hey config class nothing is there okay i just have a couple of lines of code no no no no not this config i mean from there you were initializing that from the context one after java yeah yeah so because in this line number 18 like uh i remember like it was very like uh in the beginning we used to see in the spring like we are annotating now now i know you got matured and springboard and all kafka yeah so i was getting confused nothing i'm just loading it i'm i'm just loading it uh okay i'm just loading it right i know we have not been used all these things for a long long time and we are learning some advanced stuff right now but yeah just wanted to make it simple so that everyone can understand it's not springboard specific the events are spring events right why should i use springboot here maybe we can implement in spring mvc application when we can implement it in in any any kind of application right it's a spring concept only so we just want to do that i'm just initially i'm just getting the configuration from the update config so app.config will scan for this particular package and all the packages it will be scanned like you know all the packages we started from com all the packages will be scanned and there be those let's say in the listener it will be scanned this particular package it has a component it is gonna you know initialize this particular class and it's gonna create the object of our village initial uh will last listener and we'll put this particular object inside the application context so all the classes which is have having the component or you know any kind of component for now just think like component annotations those objects will be created and will be maintained inside the application context and i'm starting my container just right here and i'm telling my container that hey go ahead and look for this config file do a component scan and get those get those spring been initialized inside the container itself and obviously it's a configuration file and i'm making it at configuration just right here okay what about others uh karthik you are good no question so far karthik there ravi obvious i have a small question yes okay okay if we complete that uh youtube course of bringing security of yours that is sufficient right for this that is sufficient i'm posting the paid videos only your video is only uh from evening batch videos of you say okay that is sufficient you just keep watching that uh sessions uh obviously from the number one i'll be posting some more videos and one in a point i'll be stop posting okay because all the advanced topic you know that we'll be using on microservices and springboard i haven't really covered it on youtube so that won't be going to youtube right now but all the basics whatever i've given that is pretty much enough for now okay only few topics are remaining and i mean those things will be covered here okay don't worry about it okay so i'll be posting another seven hour videos pretty soon and that will cover uh the entire the spring security internals okay so you keep watching from the number one okay and before before i end this session i have a simple you know uh idea if i'm gonna copy this particular bean in my uh in my application context and let's say i will be past this bin again and i will be getting the g capital publisher again from the context and i'm going to change to bin one unless the bin one being one and now let's say i want to stream the episode five and i want to stream the episode eight then what will happen then again it is gonna trigger your you know your events so if i'm gonna do run this application you can see in your console okay uh see right now um g cafe started uh starting the bbt episode four then start um i mean this right now the g cafe has been started the episode for right now uh here we are making a call to stream big bang theory and here we have this log g cafe uh starting bbt episode number something and that has been started right now and you can see although all the listeners of village avita akhil willi all these people are right now listening to that particular event and again right now we are making another call here in the app.java we are making another call here called stream committee circus and right now this method is getting called and gcap is starting committee circus episode number something and then is publishing the event right now you can see the first log in the console starting the committee circles episode number zero zero seven and there we go justin is started listening to that okay so only one listener is there and again we are creating another bean and some here in the app we are creating another bean right here and then we are streaming big bang theory episode zero zero five okay and then again the same thing will happen starting episode zero zero five in g cafe then all the listener of villains aditya akhil and willie they are started listening to the event and also then i have again called the committee circus right there okay and again all these events get triggered okay and it's basically started watching i mean justin has started watching the community circus so far so good i mean you guys understand everything that we have discussed so far yes okay one last thing okay i will be wrapping the session right now only one last thing remember this this um the events and the listeners if you if you guys think just right here the publisher even uh all these things these are synchronous in nature the spring events by defaults are synchronous in nature so the problem will happen you know what if imagine i'll just go for a listener let's say obvious listener okay let's say right now this listener event will be occur because we have this at event listener annotation and right now imagine if i'm gonna do something weird here let's say trade dot sleep okay and if i want to make my application sleep for let's say 3000 millisecond and if i will do it sorry let me just handle it with a try and catch okay now you'll see something funny here now you'll see that whenever i'm gonna run my application right so see right now that now my execution stopped now maybe i can just do something here when before i do is do this thing i just do start let's say um let's say the big bang theory event okay um let's say obvious listener started watching started listening okay listener listening start okay and you can just paste it after this once the thread will happen i mean once the thread did end and they'll just do a end you will see that what will happen right now the application will be dead see it is starting now it is stopping it is then once that thread does sleep once that sleep has been completed then it is again you know releasing the flow and it is doing end and again here it is starting then it is waiting for some time and once that thread does slip time is ending at that time only it is just you know resuming the flow so it is synchronous in nature see it is putting your application in hold so it can see it again put your application in hold okay this particular trade till the time it will not complete executing uh you know your application will go on hold okay so how can we make it asynchronous how asynchronously this listeners will be keep listening tomorrow i'm gonna let you know but be very uh careful whenever you are doing some heavy heavy duty stuff just right here okay i hope you understand that the default behavior of this is synchronous tomorrow we're going to talk more about this okay more about synchronously yeah tomorrow tomorrow we'll listen to that um okay okay uh s2 is already late and just you know whatever i did make sure to do the practice so that tomorrow you can catch from here okay cool any other questions vivek hi how are you no question no question i mean everything made sense yes perfect um yash no question today you have covered object listener or this one is the only listener we have in spring this is listener only think like this is listener for today tomorrow we'll think more about it okay okay but today whatever we discuss yes made sense yes sir yes okay really you are good yeah i'm good okay is it is it is it so much complicated to understand no right or no yes not really okay perfect okay cool guys see you guys then tomorrow and same time whoever wants to join the session you can join then after three four days we'll go back to our normal system thank you bye [Music] you
Info
Channel: Selenium Express
Views: 3,901
Rating: undefined out of 5
Keywords: spring events, spring events tutorial, spring boot listener example, spring boot events, spring boot event listener, events in spring boot, applicationevent in spring boot, eventpublisher spring, applicationeventpublisher in spring boot, event handling in spring boot, event handling in spring, selenium express, ApplicationListener, ApplicationEventPublisher, @EventListener, abhilash, abhilash panigrahi, publisher listener pattern
Id: xkWTO5M51FA
Channel Id: undefined
Length: 83min 2sec (4982 seconds)
Published: Sun Jul 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.