Learn Spring Framework the Easy Way! | Karthikeya T | Skillshare

Learn Spring Framework the Easy Way!

Karthikeya T, For Your Learning Needs

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
64 Lessons (4h 54m)
    • 1. Must Watch!

      1:42
    • 2. SpringIntroVid

      2:19
    • 3. ImportingMavenProject

      0:35
    • 4. 0401TightCoupling

      5:08
    • 5. 0402DependencyInjection

      4:38
    • 6. 0403DIWithSpring

      5:15
    • 7. 0502JavaConfig

      9:43
    • 8. 0503MultipleConfigs

      6:38
    • 9. 0504BeanFactory

      2:36
    • 10. 0602CIExample

      9:51
    • 11. 0603NoteworthyPoints

      3:41
    • 12. 0604SetterInjection

      5:02
    • 13. 0605AmbiguityProblem

      2:52
    • 14. 0702InitDestroyExample

      8:30
    • 15. 0703InitDestroyFromConfig

      2:27
    • 16. 0704BeanPostProcessor

      8:04
    • 17. 0705SpringWiring

      4:22
    • 18. 0801AwareInterfacesTheory

      2:00
    • 19. 0802BeanNameAware

      4:45
    • 20. 0803ResourceLoaderAware

      3:05
    • 21. 0804ApplicationContextAware

      4:10
    • 22. 0805OtherAwareInterfaces

      1:43
    • 23. 0901SingletonDesignPattern

      9:58
    • 24. 0902DontUseSingletonWhenObjHasState

      3:13
    • 25. 0903LazyAndEagerLoading

      1:41
    • 26. 0904SingletonInSpring

      3:42
    • 27. 0905SpringLazyAndEager

      4:38
    • 28. 0906SingletonInSpringvsSingletonPattern

      1:28
    • 29. 1001PrototypeScope

      3:00
    • 30. 1002PrototypeBeanLifecycle

      5:30
    • 31. 1003CallingDestroyMethod

      5:26
    • 32. 1004NoteworthyPoints

      1:37
    • 33. 1005MethodInjection

      7:20
    • 34. 1101SIAutowiring

      6:35
    • 35. 1102AutowireAnnotation

      4:47
    • 36. 1103ConstructorAutowiring

      4:54
    • 37. 1104IncludeExcludeBeans

      5:14
    • 38. 1105CircularDependency

      4:29
    • 39. 1106ProsAndCons

      3:45
    • 40. 1201DefaultPropertyEditors

      5:47
    • 41. 1202CustomEditors

      4:32
    • 42. 1203EditorsWithOwnName

      3:47
    • 43. 1204UsingRegistrar

      2:43
    • 44. 1301Profiles

      7:57
    • 45. 1302JVMParam

      2:18
    • 46. 1303SingleConfigFile

      3:12
    • 47. 1304ProfilesInXML

      2:09
    • 48. 1401FactoryMethods

      5:09
    • 49. 1402FactoryBean

      4:06
    • 50. 1501AOPIntroCrossCuttingConcerns

      7:02
    • 51. 1502AOPTheory

      3:02
    • 52. 1503SpringAOPExample

      11:04
    • 53. 1601AspectJvsSpringAOP

      2:59
    • 54. 1603AOPWithAspecJ

      9:14
    • 55. 1701Advices

      8:09
    • 56. 1702OrderOfAspects

      2:53
    • 57. 1703Attributes

      3:28
    • 58. 1801PointcutExpLang

      3:01
    • 59. 1802Designators

      7:52
    • 60. 1901WhatAreEvents

      3:09
    • 61. 1902ObserverDesignPattern

      5:12
    • 62. 1903EventsInSpring

      4:00
    • 63. 1904BuiltInEvents

      1:37
    • 64. 1905CustomEvents2

      3:25

About This Class

Welcome to Spring Core!

Spring is lightweight, extensible, robust framework (or rather framework of frameworks!) for creating enterprise applications that are easy to maintain.

Spring Core'  is the core module that comes with an embedded servlet container (IOC Container) and would enable the core functionalities of spring. You can think of Spring Core as the core engine on which other modules would run.

Spring Core is the starting point for learning spring framework, and having a solid understanding on the core concepts of spring would ease your journey with learning other modules/projects of spring.

This course will cover all required spring core concepts in great detail.

Transcripts

1. Must Watch!: full stack developer sort in great demand and are paid very high. So it's not enough that you'll on one technology but rather full stack off technologies. We have all the courses on sculpture that would help you become a full staff Java developer so that you will gain skills to develop, end, to end applications from scratch all by yourself and be eligible for top profile jobs or to work on freelance projects. So was it this link? Its corporate dot com slash skill share. It has all the links to our courses, and they're all organized in a proper sequence. You can start from Java programming, and here's a link to that course. Once you complete this course, you can get into advance Java. And here's the course link for the same. Like was, you keep going and complete the entire stack off technologies. The courses were also include creating into and applications from scratch. And if you're struck anywhere, you can always post your questions. Even if you're inexperienced. Dollop er I would highly recommend you to start from scratch because many people who followed this approach and gone through all these courses from scratch have seen very good results. Many individuals have clear interviews. Some of them also cleared certification exams and some of them also got into good jobs with better income and all. So I would highly recommend that you followed this approach. Start from Java programming all the way to all the advanced courses. Hope it makes sense. 2. SpringIntroVid: spring is the most popular framework and an inversion off control container for developing Enterprise Java applications. Applications that are developed years and spring will come with a whole stop advantageous. Some of them are. Spring promotes loosely coupled architecture where all the application components are less dependent on each other. Spring will also help us get rid off the crosscutting concerns and help the dollar per focus only on the core functionality. Off the application spring will help us create applications that are easily testable, using some of the unit testing frameworks like J Unit Market. Oh, etcetera. Spring is pretty lightweight, and you can completely get it off an application Silver altogether and there are many more advantageous and using. Spring Spring has lot off projects, and depending on the type off application that you're building, you need to choose one or more off these. But you won't be able to understand or used these projects without understanding the core concepts. Offspring. And that's what this course is focused on were to talk about spring core and understand some of the core functionalities offspring, and that will enable you to understand the news any off these models. What we cover in this course. If you wake up in each our developer in the middle of the night and asking mortars for everything about spring, he's going to say dependency, injection in motion, off control, aspect oriented programming. And he would go back to sleep Well, will give a great deal off attention to these concepts, and in addition to that, we will also cover a host off other concepts that you're seeing on the screen. Obviously, some of these may seem strange to you at this point, but I assure this course is going to cover every core concept offspring in great detail. I trust my abilities off teaching, and I understand the fact that you are a big nervous spring. So I will start from scratch and a guarantee that the course will be a great value. Add for your professional growth. Just continue watching the free preview videos, and I'm sure he will like them. I will see you soon 3. ImportingMavenProject: 4. 0401TightCoupling: in this chapter, we're going to talk about dependency injection. Before that, let us try to understand the term tight coupling. Let us take a look at some of the real world examples off tight coupling, thereby you'll understand better. So here, from this image, we can say that the pillars and the building are both tightly coupled to each other. That's because let's say that I found out that the pillars are getting old, and now I have decided to replace all the pillars. I can't replace the pillars alone without disturbing the building. That's because the boat are tightly coupled to each other. But the point here is if I'm making changes to pillars that will have impact on some other entity. A human hair is another example. Off tight coupling. He can't pluck the hair from your head without hurting yourself, so plucking the hair from your head will have an impact on your body. Here we have the one last example off tight coupling. The cables off this bridge are tightly coupled to the bridge. If you cut down all the cables that because the bridge to collapse. So what are some good real world examples off the opposite loose coupling spectacles. You can keep switching the spectacles depending on the color off your choice, and that will not have any impact on your body. So both your body and spectacles are loosely coupled to each other. Removing one from the other will not have any impact on either of the object. Similarly, a mobile case for be a good example Off loose coupling. A mobile case would be loosely coupled with a mobile phone. You can keep changing the skin as you wish. It will not have an impact on the phone. The phone would still function as usual. Now, these are all some of the examples off tight coupling and loose coupling in real world. But how about programming? So let us see what is tight coupling in programming. Before that, let us try to understand the term software component just as we have a car that is made out off multiple components. For example, a car would have a competent called engine, another component called steering wheel, and another competent called gearbox etcetera, when all these components combined together will have the final product. Similarly, a softer product is also combination off multiple components. Now these competence can be as simple as a simple class, or is combination off set off related files that together will solve a certain problem? I mean, just as with the case off a car, we have a bigger component, which is the engine, which in itself has multiple other components, and we also have some minor components, like a steering wheel, for example, and often times these components are dependent on each other in order to function in a certain way. For example, in order for the wheel to rotate fast, you have to press the accelerator so the competent wheel is kind of dependent on the competent accelerator and similarly, in softer as well. We do how multiple components and are dependent on each other. What we're going to take a look at now is a very simple example of the same. So here we have a very simple example off a competent being heavily dependent on another component. We have this double bean inside, which we're trying to extend. She ate the dog class, and the dark class has just one method in it, which will ask the dog to make noise. And that's the matter that we're trying to call from here Now it's safe to say that Java Bean is dependent on the dark class. Only then can it call its method. But we have a very, very serious problem. Let's that I've decided to make noise like a cat. Now what should I do? I should go back to my being and try to make changes in the code. I'm going to make this a cat and make those changes everywhere and do take a note that this matter is no longer valid. I need to use the method that is specific to cat, which is shout now. This is a very simple example. Imagine that you're working on a really world project. You'd have to make this changes in thousands off locations, which is, ofcourse, not feasible. That's why we need to try our best to make this competent as independent as possible or, in other words, way to make sure that they're loosely coupled to each other, not tightly coupled to each other. So let's continue from next video and see the solution 5. 0402DependencyInjection: In our previous example, we've seen that our class is heavily dependent on the dark class and so we're not able to switch to the alternate. Oh, if we make this to cat, then just off the court is breaking. So one way to solve this problem is by using interfaces. Let me explain what I mean. So what I have done now is I've created this interface animal, and both cat and dog would implement that interface. And these two classes would provide their own implementation for the mattered. Make noise? No, in my code. Instead off explicitly mentioning the class name as dog. I'm going to make it more general and make it animal. And the method name in the rest of the code would also change with the method name. Make noise. Now, that way I had the freedom to switch to alternate O. For example, if I make this toe cat, the rest of the court would still remain same. No changes are needed or what so ever, but still wouldn't accomplish our goal off making this class and the cat class being independent off each other. So what can we do in here? So that this class would be completely independent off the cat or dog. Can you make a guess on how we can fix that? Well, it's actually quite easy. All I'm going to do now is introduce a new constructor, a parametric constructor and the Param Eter that we're going to pass is going to be off type animal. We're not going to explicitly mention at the cat or dog, but we're going to get more general and say the argument is off type animal. So let me show you what I mean. So I have this class in here and that defined the constructor that takes in the animal Param Eter. And I'm just trying to assign it to this variable. Now, nowhere in this code did we mention cat class are the dark class. Now it's really safe to say that this class has no dependency are. In other words, this class is loosely coupled with a cat or dog. But who is actually going to pass this argument? Well, it's from our main class, and all I'm trying to do here is I'm creating an instance off this being and that I'm trying to pass the perimeter to its constructor. No, depending on the perimeter that I pass, its corresponding method would be invoked. So in here I'm trying to pass the dog object. So that's going to result in executing this line off statement. If this was cat, that would result in executing this statement. So let's quickly run the program and see how things will go. And now let's make this as Capt. Said, the file on the program. And there it is. Now we're certainly accomplished. Something really significant in here wouldn't define the being class dependencies in X Class itself, but rather we have sort off sent its dependencies as a perimeter to its constructor. Or, in other words, we're trying to inject the dependencies into this being. And that's exactly what is called dependence injection. So by using the technique off dependence injection, both this bean and the dark class are not tightly coupled to each other. Now, all the way. Accomplice, dependence, injection. How mean class logic is not intended to manage. The dependence is in our application. Now imagine that you're working on the Real World Project and you have thousands off class files. It's really messy to manage all the dependence is from your application logic itself. Instead, it would be wise to leave that task to 1/3 party to, and the tool that I'm talking about is off course spring framework. So let's see how we can leave that task of dependency management to spring framework because spring is designed specifically for that purpose. Well, in fact, spring is just not meant for dependency management. But a host of other stuff, which will talk about incoming videos, hope it makes ends. See you soon. 6. 0403DIWithSpring: Okay, let us take a look at how we can perform dependency injection. Here's in spring framework. I don't know the project that is bundled along with this chapter and imported into your workspace. I believe we already have a video demonstrating how to import maven projects into workspace . Just follow those instructions and import the project afterward. You should be able to locate all these files inside the project. And before we move on with the dependence injection, let us try to import certain libraries required to perform this job and the libraries off interest. Our spring core, which will provide all the core spring functionalities and spring context which will help us create the spring application context, will understand what is application context in a while. Well, ideally, it sort required that you need to specify spring core library in here because spring context is dependent on spring core. So even if you don't Spence fight, Megan is actually going to take care off downloading spring core because spring context is dependent on it. And once you do that, just right, click on the Project Goto maven and click on Update project. Only then will the maven look for all these libraries in the Internet and download them into your local depository. Once you do that, you have to create an XML file. You can give any name off your choice in my case, it with the name conflict dot XML and this XML file must reside under the resources directory. And this is XML file, which is a key file. Where would define although classes in the ice offspring every component are a class is called a bean. So we just simply defined all the beans that we're going to work on. And here is how you would define them. House was fired. That last name, which in this case is dog and I have given it and identify where. But the name animal similarly have also defined how about being in here, including its package. And then I've given a unique name to it. Along with that, I've also added another tag in here, which, basically the constructor argument that this being except that's going to accept a argument off type animal. And it's the same thing that I'm trying to do in here. I'm trying to send a bean with identify animal so that points to this dog class, which, of course, implements the animal interface. Now, earlier in our main class logic, this is how the court waas. We're explicitly specified the dependent class like cell, and then we're trying to call the mattered. But this time we're going to do things differently. We're going to use spring, and this is how it is done. We're going to create an application context and what application context essentially does , is it? We're just simply parsed through the XML file that just specify in here. And it would take north off all the list off beans available. And when you run this program, the application context is actually going to create instances off all these classes and then inject. The dependence is in this case, the bean is dependent on the being animal. And so application context is going to take her off injecting animal into this being class . And right after that, we're going to use this object context to get the bean off your choice by using its unique identifier. In this case, we're interested in getting this being class object, and then we're trying to call its method shout, and that would result in executing make noise off animal and whichever declines that were trying to inject from here, its corresponding method would be called It's as simple as that. So if you're on this program, this should ideally call the shot method off dog. It worked. Now that has change this to cat, save the file and run the program. And it worked as well. Well, I'm actually trying to keep things simple for now, but we will go deep and understand what is application context. What is wiring what is in motion, off control and all such concepts would becoming in future videos for sure. But I hope that this chapter has given you some good start. But make sure that you're able to run this program successfully on your own. If you face any issues, do put your question, and then I should be able to answer it. But please don't go further unless you get this application working because this is your first crucial milestone. All right, see you soon. 7. 0502JavaConfig: so far, we're trying to use an example document to define our beans. But using an external document may not be right. Choice Every time, for example, said that I'm trying to change the name off this job. All files, then there won't be any kind off a highlighting Index Immel document indicating that the change has happened. This is going to be a bit off a concern, especially when you're working on a large scale projects. Because in order to test your changes, you have to build the entire project and then deploy the artifact under the server. And then you will realize that the application has crashed because off a small type of error in your XML conflict, that is going to be a very frustrating moment for a developer. Also, it would be hard to re factor your code when you're trying to configure things using XML file. So since spring washing number three, we have a solution to address this very problem, and the solution is you can now configure all your beans in a Java file, for let's see how it's done. I have the exact same court as previous, but the only change is I have now changed the name off the being for Java toe animal noise dot Java for two good reasons. The name being is contracting with the name being in spring. It's not a good practice to use that name. And number two, this name makes more sense. So let us see how we can configure our beans using a job, a conflict. So for this purpose, I'm actually going to create a new folder inside a package and the name off it is going to be simply conflict. So this is where all our conflict files would remain. Well, I said, convict files, meaning that we can have multiple conflagrations split across multiple files and we're going to take a look at it at later point of time. Or maybe pretty soon. So now let's try to create a Java file inside that folder. Best clear class file. I'm going to give the name as Java conflict. He can give any name off your choice, and then here were just simply going to define our beans. And we're trying to replicate the same thing that we're trying to do here. So let me try to imitate the same being in our Java conflict. So all I have to do now is to create a method that just simply returns and in stance, off animal or rather, cat to be more specific. So I'm just simply going to define the public method public and is going to return an object off animal. I'm going to name this method as simply animal. So this is going to be the name of the being that level. Using our code and written to call the braces were just simply going to use the written statement, return a new instance, offer the cat or dog the start with dog and with semi Colin all the country's shift. Oh, to import all the required classes and interfaces. And although control shift F to give a proper indentation to our code now, wait explicitly mentioned spring that this file is a convict file within which will have our beans. And this in here is a bean that we can do by specifying the falling annotations you would say configuration. So now spring will start looking for beans in this file, and then we're going to give the bean annotation to tell spring that we're trying to define the being in here 1000 blouse that now let us also try to configure just being in here, I'm just simply going to copy the same mattered. But this time I'm going to return an instance off animal noise and same should get reflected in here. He can give any name off a choice to this method. That's fine. Although control shipped. Oh, let me pull this down now, Just like we're trying to pass an argument to the constructor off animal noise, we have to do the same stuff in here as well. I'm just simply going to call this matter from here. So this would essentially pass a new instance off dog to the constructor off animal noise just as we're trying to do in here. Nothing different. And guess what? We just declared all our beans inside a job. A convict file. Now, inside our main logic instructors in class politics, similar application. I'm going to copy this and use the different implementation off application context that will help create a context using a job, a conflict and the instruction that I'm going to use is annotation control space to give some suggestion conflict application context And of course, we would no longer provide XML as an argument. This has to be our job. A convict file dot class. Let me pull this in here so that you have a better view. So that's all there is to it that's on the program and say things are working. Sure enough, it work. Now Let's try to change this to can't, I would say, Kat said the file and on the program and always seem to have a never. That's because we haven't imported the cat class, although control ship Oh, and let's run the program. And sure enough, we have different output. No letter that I found this name animal noise a little misleading. I'm going to change the name to something else. Maybe I would name it. Does animals sound? Now? Guess what. We have syntax highlighting everywhere, including this Java conflict file. That's the beauty off it, whereas if you go to maximal document, you don't see any difference. So we can just rename the file and triples the name everywhere else. So this is a lot easier, and this should dio I'll save the file and something should get reflected in here and run the program. Everything is smelt. So which one is better? Should we use XML file or the Java conflict while it depends on your requirements But if you're not sure off which one to use, I would suggest you to go with Java conflict. As off the time of this recording, I would say to the most pot industry is still using XML convicts. But sooner or later, industry is going to move towards using a Java conflict because off its benefits. So all the double come pick is not a popular way to define your beans as off. Now it is going to be the future. So for that reason, even I would be inclined towards using Java conflict or a maximal document. Or I will try to use mics off both that we will get to know how things are configured in both these files. But again, I will try to use boat. But I won't promise I would aid to go with Java con flick which would most likely be the case or XML document or both. So let's see how it goes. Incoming videos also for using example document. I want to quickly talk about another implementation of application context, which will let you configured XML file that recites in your computer and implementation that I'm talking about is file system maximal application context and in here as an argument, you would get the part to the maximal file, but off course, more often it's always better to use an XML document that recites in the class part over giving a file part in your local file system. But, like wires, we also have many other implementations which were not concerned about right now. They would come into picture when you're trying to use spring to develop Web applications. So for now, we're not talking about them. Let me under the changes, and I'll see you in my next video. 8. 0503MultipleConfigs: as off now, since our application is very small, we don't really have to configure a lot of beans. But more often in real world projects, we might need to configure hundreds off beans. In that case, it may not be an ideal choice to list down all the beans that existed in your application in a single conflict file. We might want to spit that conflict file into multiple conflict files, and each file would constitute list off related beans. That way it is easier to manage. For example, let's say that you have an e commerce application. In that case, you might have a convict file with list off beans whose responsibility is to manage the customer orders. And then you may have another convict file, which may have beans that are related to payment processing, etcetera. In this video, we're going to take a look at how we can manage multiple conflict files. So in this video, we're going to take a look at how we can manage to have multiple convict files. First, let's talk about XML configuration. So in here I have defined couple of beans. No, let's say that I wanted to spread this xml file into multiple files. So I'm just going to make a copy of this file. You can give any name off your choice. I'm going to leave it to convict to dot XML. And inside this file, I'd like to define this being, so I'll get it off the other one from here said the file. And in here I'd like to keep this and get rid off this. Now we need to do something that will connect these to convict files and attack that I'm going to use to solve. That purpose is import. And you would set an attribute resource that just points to the conflict to dot XML. It's as simple as that. That's all there is to it. Now. If you're on the program, everything will work, as expected, pretty straightforward. So this instruction is as good as importing all the list of beans that are available in this file. So this file Now, let's take a look at how we can do the same in Java conflict file. So I'll go here and make a copy of Java con fink. I'm going to leave the name to Java, constitute or java. Let me open that up. Okay, Now, similar to what we have done with XML conflict. We're going to do the same in here as well, so I'll get it off this being from here. But I would keep it in the original conflict file. And just like we did an import in here, we're going to do exact same thing with an annotation. Also add rate import and inside the parentis is I would just say Java conflict to dot class . But since we don't have this method in here, we have to pass that as a perimeter and things that look smoothly. Now we don't really have to get into internals off how this is working. Who is passing the perimeter, etcetera. It would all be taken care by the spring framework. When you have this annotation at the red Bean, let's go to our program and try to see if things are working. I'm going time come in that line and run the program, and it worked well. You can also mix board Java convict an XML conflict if you wish. So let's see how it's done. All you have to do now is in straw, fusing the import annotation you would say import resource and you would toward the class but off the XML file. For that you have to mention class part and then a colon and then the name of the XML. So that's going to be conflict to dot XML. And let's if things are working. Sure enough, it worked well. Same thing can be done from XML file as well, but it is bit off a task. So let me just quickly Paul's a video, make those changes and then I'll come back. So here we are again, and in order to include a Java conflict into an XML file, you have to now introduce the following names space into your XML file. And in addition to that, you also have to include the following schema locations in order for it to work, after which you would define the stag that just simply tells spring that we're trying to use a Java conflict, and then you can define all your java conflicts by using the bean tag just as you see in here. And make sure that you include the complete package name as well, and that's all there is to it now. This is as good as including all the beans defined in here in your XML file. But make sure that you're not using the names that you have given to the methods. So let's on the program and see if things are working. And sure enough, it did. Let's try to change us to say cat and run the program and things will work. Hopes that import the class in here and on the program should work. So spring off a lot of flexibility to mix and match multiple conflict files and took on Figure your beans. And when we talk about Arthur Wiring, we'll talk more on similar lines, so I will see you soon. 9. 0504BeanFactory: in this video, we're going to talk about being factory so far in our examples, we had used application context to load the IBC Container and it's beans. But there is something called a bean factory, which you can think off it as a predecessor off application context. Or, in other words, you can think off it as a previous washing off application context, which is lightweight with less a number of features. And in order to define the being factory, you have to include a couple off instructions. One is to create the resource object, and the other is to create the being factor itself by passing. That resource has an argument to its constructor. Now, just as we have multiple variants off application context, depending on prom, bear the conflict families to be read. We do have the same stuff in case off being factory as well, so you would create the resource object accordingly, and you would pass it as an argument. Toby in factory. In here, we have a couple of examples to demonstrate the same. If you'd like to read the XML file from the file system, you have to create the falling resource and then you would feed that as an argument toe being factor constructor. On the other hand, if you'd like to read XML file from the class part, then you would use a class path resource to do that job. Likewise, we have many different ways to define the bean factory. You have to choose the one that sells your purpose. But if you're using being factory or application context, you have to sacrifice a lot of features that spring offers. For example, you're limited to using only XML conflict files, and you cannot use any annotation based conflagrations if you're using being factory now. This is a serious problem, especially if you want to leverage the Springs auto wiring feature, which will talk about incoming chapters. Also, being factory would be a problem for internationalisation, eventing, etcetera. Now, don't worry about all these stones. We're going to explore all of them incoming chapters. But clearly, application context is of a better option combat toe being factory as it supports a lot off features which being factory doesn't. But there could be some cases were being factory might be a better option. For example, say that you're developing an application for a mobile device. And at the same time, you realize that you don't need all the features off application context. Then you can use being factory because it will offer better performance at the cost off features. All right. Hope it makes sense. See you zoom. 10. 0602CIExample: Okay, let us have a court walk through of the following scenario. First of all, the trusted company will have to publish their FBI so that somebody can provide implementation for it. Let us assume that this is their project where they would design their FBI for the sake off simplicity. We just have one single interface in here and that would just define set off standard methods that will help their clients connect to the servers and store data insecure manner . So this would have methods like Configure Silver's, which would let their clients connect to their servers. And this method is going to accept the falling para meters like account. I agree, which would be the unique account I D given to a customer their user name, password, and then the list off service to which they want to connect to and list off Convict para Meters, which would let the customer provide the amount off course required amount of hard disk. The amount off Ram, etcetera and rest of the methods are pretty self explanatory. And once they defined the FBI like this, all they're going to do is to publish a jar archive off this project and in this example, all I've done is I've right clicked on this project and then clicked on export search for Jaar. Select this option jar file. Click next, and then you can choose Depart Very want to store that jar file? Give it some name. In my case, the name that I've given is trusted company Jar Jar. And once you do that click finish, that would save the file. This is like the trusted company has published their FBI so that the people who implement will now start providing the implementation for the FBI. So now one of these implementer Z will have a project that looks something like this. Let me open this project and inside their project, they're going to use that external AP I from the trusted company. So I go to the properties, I have a bill part, choose libraries, chills the class part, click on add external jars and then you would add this jar file Trusted company, which is basically having one single interface in our case, have already done that, and we're able to see it in here. I don't have to do it again. Wants to do that. You can actually create a class that implements that interface, and that's exactly what I've done in here. And many implement that interface. You're forced to provide implementations for all its methods. And I just kept things simple with his out statement, just as you see in here pretty straightforward and self explanatory. But the key thing to note here is when I'm trying to create an instance half sick, you store implementation. I'm expecting somebody to set the state off this object with a constructor, and the constructor is actually going to act up the same amount off para meters that are defined inside this method, so that at later point of time, when you're trying to call this method connect, it's actually going to call this method. Conficker servers, which is actually going to make use off all the set off, feels that we set pretty straightforward. No one's the implementer provide implementation. They're going to publish their jar file so that somebody can use it again. In our example, I've just simply exported the jar file and with the name a PR implementer Dodge are. And once the published The Jar File, it is we who have to use their implementation to be able to access their sores. So inside our project, all I have done is if you go to manage APP data file. I have declared that interface secure store access which belonged to the trusted company. That means prayer to this. I've actually imported those external libraries. So I go to properties libraries. And if you notice I know have added this to libraries. When is the FBI and the other is implementation off? That FBI sounds simple as that. The reason why I had used an interface in here is tomorrow at little point of time. If I would like to switch toe alternator implementation, I can easily switch without much offer trouble. And you'll understand why in a while and just off the methods are pretty straightforward. All I'm trying to do is to make use off the methods that are defined in the subject. Pretty straightforward. So imagine that we're trying to really connect to the silver and trying to store the data insecure manner or whatever. Now, let us go to the XML conflict file and see what is happening there. So if you notice the constructor of this class is accepting a perimeter off type secure store axes, which is an interface. So we have to create a bean that we're going to inject to this constructor. And here is where we defined that being. But if you notice this class doesn't belong to the current project, but to an extra library that we've just imported. So as long as the classes available in the class, part spring can find it. And if you remember that implementation classes having a constructor with this para meters and so we're passing the same. But do take a note that so far were able to inject an object. But here, you can see that were also able to inject primitive data types just as you see in here here . We're trying to inject the in teacher in the following format and couple of string arguments which will be the user name and the password, but do make sure that to include the following attribute the type if you don't define the type, this may lead to ambiguity. We're going to talk about ambiguity, incoming videos and similarly, we can also inject collection types here. We're trying to pass the collection list all you have to do is to define the list tag and its values. Testing Plaza C E mails with the map. You just have to define the tag map, and then you would introduce the key value. Pairs and all these para meters would be injected to this being. When an instance off it, they'll be created by the spring. And after that instances created. We're trying to inject that object into our application into this constructor. It's as simple as ERT. Let's take a look at our main method, which will be the my app dot java. This is pretty straightforward. What I'm trying to do here is to require this particular being from the application context . And then I'm trying to use its methods pretty straightforward. Let me run the program and see the result. It worked now a little assignment to you. Pause the video and try to do the same thing with Java conflict. It's pretty easy to do, and I would like you to give it a try. All right, I hope that you tried and you're successful and guess if you didn't, then here's a solution. So let's go inside the Java conflict file and see what's inside it. It's pretty straightforward. All have done here is created all those para meters and then passed it to the secure store implementation constructor and I'm returning the same. And in here we have defined the other being just as we didn't guess facsimil and I'm trying to call this method. So this essentially would pass the secular store implementation. So the constructor off manage up data Pretty straightforward. Let's on the program and see how it goes. And it worked well. So where is the constructor injection in here? Whatever we're trying to do in here is nothing. But we're trying to inject Para Meters into a constructor. It's as simple as that. But the purpose off the center example is just not explain about the constructor injection , but the lot off takeaways from this example. We're going to talk about all that incoming video. All right, I'll see you soon 11. 0603NoteworthyPoints: So here is the first take of it from our example. Nowhere in our application logic. Are we concerned about how to connect to the server to transfer the data or to retrieve? We're just simply going to say connect and everything will be taken care by another entity or another bean. And in fact, no, very no class. Are we mentioning the connection para meters like we didn't mention toe which so what do we want to connect to? He is in good check on. Do we want to connect to what are the conflict paramilitaries, etcetera. It would all be injected by the spring using constructor injection just as you see in here . And in fact we're not boned the one specific implementation or another word. This class is not tightly coupled to any specific implementation. We have the flexibility to switch to other alternative implementations without having to change a single line off court. In our logic, because whatever the implementation that would like to use, we can just simply inject it using spring. Okay, another take ever use. You may be having a question as to why do we need a non parametric constructor when you have already defined a constructor where the answer is pretty simple. It's always a good practice to have an empty constructor just so that you can create an instance without having to pass para meters. I mean, there could be certain methods in this class which has nothing to do with the secure store access. Then why would you want to take a four to create this, accused your axles and passed it as an argument instead. If you have this default constructor without any para meters, you can create a plane object and access those methods. For example, if you go to the Java conflict, you can't just create an object without having to pass an argument. If there was no default constructor, so you would see an ever in here that's undue. And also make sure whichever the constructor method that you're going to use for dependents injection, make sure that it is set to public unless you have a good reason to change it to something else. The reason is if you go to the conflict file, were trying to pass this argument to a constructor off a class that belonged to another project. If the constructor off. It was not public. Then we wouldn't be able to access that constructor. Also, I want to quickly point out that if you're not finding it user friendly, to call a method within the constructor like so you have an alternative way to deal with dependents injection when you're using Java conflict and the approach is quite simple. Holy have to do. It's just send a para meter that is off type. Whatever is the type that is being accepted by this constructor. It's as simple as that and spring. It's modern of to understand that that it has to inject the following bean and everything will work as usual. Let's run the program and make sure things are working. And sure enough, it worked. All right, I'll see you soon. 12. 0604SetterInjection: Okay. We have seen how to inject dependencies using constructor injection. In this video, we're going to talk about settle injection using settle injection over constructor injection will have a lot of benefits. But before we talk about the benefits, let us try to understand the settler injection in order to incorporate center injection mechanism. All you have to do is to get rid off this constructor from here and do they can know that we're in that implementation class And instead of using a constructor, all we're going to do is to use a bunch off center and get her methods to set all these fields Assassin Plaza. Now I don't have to type in all the methods I can make yourself the eclipse feature. Right click on gender getter. Since errors select auto and generate, that will do the trick. I want to do that, said the file exported as a jar archive and then imported into a spring project. And once you do that went to make changes in our project accordingly and the changes that are made doesn't go inside our application, but rather on the conflict files. So inside our conflict, XML instead off saying constructor arc. I'm going to use this tag property with this to attribute name would correspond to the field name and its corresponding value. Likewise, everything else is self explanatory and coming to Java come pick. It's nothing different. All have done here is I'm trying to create an instance off secular store implementation, which is this line and then I'm just simply trying to set the properties. It's as simple as that. Let's run the program, see the result and then we'll talk about the differences between the constructor injection and the centre injection. So this has worked well, same would be the case with Java Kontic I had already tested. So what are some of the good differences between the two? If you use that are injection, you have the flexibility to inject specific fields while ignoring others. Whereas if you're using constructor injection while the creation off the object itself, you're forced to provide all the para meters. Otherwise things won't work. But at the same time, this may be a drawback as well, because sometimes you may be required to inject all the fields. You can't ignore even one off them. In that case, constructor injection may be useful in our example. For instance, we can't risk missing any off this para meters. So it's better that we use constructor injection or settle injection in our case. And another don't age with settle injection is that you can change the values off these properties at later point off time, but it's not possible with constructor injection. Every time you want to change, you have to create a brand new object altogether, and you can't change just one field but combination off all the fields. That may be a drawback at times, or better yet, you can use combination of both. For example, we can still keep that constructor alive in here so that you have the flexibility way to go with constructor injection or settle injection. In that case, the values that you inject using settle injection will overwrite the values that you had injected using constructor injection. Also, I want to bring up one important point in here is that even after introducing so many changes in the implementation class, no no for double files were affected, the code remained, as is just as our previous example, there is no single line off code that has changed all the changes. However, who are inside our conflict files now? That's the beauty off dependents injection. If we were using the secular store implementation directly in our class, we had to make changes in our code. We have to remove that constructor. Make it a default, constructor Unparalled Metris, constructor! And then we have to set all the values inside our code. Just a very good point note. All right. I hope it makes sense. C ism. 13. 0605AmbiguityProblem: in one off my previous videos, I was mentioning that it's important that you provide the type attribute for each and every constructor argument that you send so that spring will be aware as to what type of literal is this. Otherwise, if you don't provide the type that may lead to ambiguity, So let us explore what is ambiguity in here We have a very simple class and it has couple of constructors and the both or accepting same number of para meters. But in one constructor we have string as the first argument in the other constructor. We have indigenous as the first argument from my convict file. I didn't tell spring what type off literal is this? So should the call go to this constructor or to this constructor? Well, spring would be confused. Spring cannot deter mine. Whether this literal should be treated as a string are as integer if it has to treat it as a string call would go here. Otherwise hair, we wouldn't have this am bigoted problem if there was only one constructor. Spring is smart enough to understand that there are three para meters in here and so the call has to go to the only constructor that is present in here. But when you have two constructors, that may cause ambiguity. So let me try to run the program and see the result. So here you notice that the account I d is set to zero, which is a default value. That means the call has gone to this constructor. But maybe I want the call to go here. How can it solve this problem? It's pretty straightforward. All you have to do is tow. Introduce that type, attribute and say, What type of literal is this? I'm going to say it in danger. Same goes with other tags as well. But this is going to be string and this will solve our problem. And sure enough, we have it populated. And this is one of the reasons why I prefer to use Java conflicts over an XML conflict is because you're pretty well ever asked what type of data that you're passing as an argument . So by looking at this court, I can tell for sure that this is off type integer and the call is going to go to this constructor. All right, that's about it. See you soon. 15. 0702InitDestroyExample: Okay, let us take a look at a quick example that demonstrates the life cycle off of being here. We have a very simple example. Where and we have a silver conflict file. It says that I wanted to connect to a silver and so I need to provide all the following properties to the civil conflict object so that I'll be able to connect to a server. Well, we're not really going to connect to a server, but let's just assume so here, I'm trying to use mics off constructor and settle injection to be able to set all these properties. And so we have a constructor that accept all these four properties as para meters. And then we also have s center mattered that will accept the conflict Param map and from our come pick file. As you would expect, we have this being defined civil conflict, and I'm trying to send mix off constructor and center arguments and in addition to that, I've also set a few messages. Basically, I'm just simply using assault statement. Since we don't have any logging mechanism, come figure at the moment so that we can take a look at how the life cycle is taking place and stuff like that. Ideally, it's not a good idea to include all these instructions in the centre method. But just for the sake off this example, we're going to let it go. It's on the program and see the result. What has just happened So initially, after the beans are created, the call has gone to the constructor while the object is being constructed. And then after that setter injection has happened, as you would expect and then finally were able to display the object itself. Now do taken note that we're tryingto also close on the application context. This is when the spring will take care off garbage, collecting the objects or destroying the objects. But we don't have any message to display. We will be able to display it in a while. No letter that I wanted to perform something right after the dependency injection happens and before the object is made available for us to use in our application. Also, in addition to that, I also wanted to perform something before objects get destroyed by spring. So for this purpose would actually going to introduce a couple of interfaces and those are these two, and by doing so, we also need to provide the implementation for these interfaces. And these are the methods for which went to provide the implementation. So this method in here, after properties set, belong to initializing being interface. And whatever you performing here will take place right after the dependency injection happens. Well, actually, in between the dependence injection and calling this matter after property set, there's another thing that will come in the picture, which are ever interface methods, but we're going to talk about it. Incoming videos for now, whatever you want to do before the object is made available for application to use, you can do it in here and in our example. Let's say that I have a logic in here that will make sure that the silver is started before we're trying to connect to it. For simplicity's sake. I just have a simple message that just is starting and connecting to the silver. And once after we make sure that the server is available, it is up and running. We're going to go ahead and connect to the server so that at later point of time in our application, we can do a variety off things isn't that object? And after we do whatever we have to do and then we call context got close. Do they can know that we need to typecast this application context two configurable application context to be able to close it alternatively, can also introduce a shutdown hook, which is basically a piece off court that you want execute before JV M is shut down and when either of these events will occur. The destroy method that we have just implemented will be called before the objects will get destroyed by spring. Framework the knowledge on the program and see the result. And sure enough, after the center injection are in, it mattered is called. And so we're able to see this message. We have done certain task with that object. In this case, we're just simply trying to display it and then before the object gets destroyed, we're trying to shut down this silver's. It's as simple as that. No. Alternatively, if you're used to developing J two ee applications, you're probably ever off these annotations. They belong to jail Sarto 50 specifications. So what this means is, instead of using these interfaces in here. You can now use those annotations instead and things will work as before. But then these annotations belong to the job extension library job I X. We don't have it at the moment, and so we're not able to use it. But, Justin, because if you'd like to go with this approach, then spring doesn't stop you to do that. Let me just quickly undo those changes. So so far we have seen are being go through all the faces that are highlighted in white as well as initializing and the destroy phases we're going to explore on the other face, the ever interfaces, etcetera, incoming videos for sure. Now you may be having question as to why don't we do whatever the stuff that we're doing in here in the constructor itself? Well, there are two good reasons why you don't want to do it. Constructor will come into picture when this object is being constructed. That means the object is not fully constructed for you to use it. Also do taken note that the constructor injection will come into picture before the center injection. That means we may have certain dependence is that we may be injecting using settle injection and those dependence is may not be available in the logic that you perform in the constructor. Also, one another thing, which is what noting is that if you're continuous creating multiple instances off silver conflict, then constructor would be called each and every time. But whereas the post construct method will be called only once the godless off number off instances that the container will create. Now we have a chapter dedicated to talk about the scope off all the beans and there will explore how maney in stencils off these beans will be created, etcetera. You have to hold until that point. But for now, if you're using a post construct method like this, no matter how many instances does he continue create this method would be called only once . Unlike the case for the constructor and only after this method is called and the court and it will get executed well, the object would be qualified for dependents injection letter that we have another class that is dependent on this class, then springs to take care off, creating the object off this over conflict before it injects this object into another class and spring will not inject this object until this method is executed. All right. I hope it makes sense. I'll see you in my next video. 16. 0703InitDestroyFromConfig: okay, We have seen how we can make use off in it and destroy methods during the last cycle off a bean by using some spring specific interfaces. But we have one. Seed is drawback with this approach. Is that our code or are bean is tightly coupled with spring framework, and that may actually destroy the very purpose why spring is even existing. So we have an alternate away to deal with the same instruct using these interfaces in here . We'll just simply right or methods, and you can give any name off your choice. Let me make it toe in it. And then, from our convict files, we can introduce some additional attributes. Something like this. We're going to introduce these two attributes, and it mattered and destroy method, and they're just point to the respect to methods reciting in the bean. And that's all there is to it. It's as good as our previous example, except that we're not tightly coupled to spring specific interfaces. It's as simple as that. And if you want to do the same from the Java conflict file, it's simple as Bill just happen to Jews. The falling attributes, just as you're seeing here and let's go to our main program and is the Java conflict to run the program and this will work as well. And Justin, Because if you're okay with the tight coupling and you introduce those interfaces in here, then the following is a priority. If you've introduced all these methods like a post construct annotated method, the after property set method is in the spring interfaces and then it method as well. Then spring will call these methods in the falling order. Forced it would call the method annotated with post construct and then after property set. And then they innit, method that to define in the conflict file. Just letting you know in Gaza. Feel curious. Hold it. That's about it In this video, I will see you soon. 17. 0704BeanPostProcessor: spring containable manage multiple beans, and each bean may or may not have been it mattered associate ID. But regardless, office for being has an In it matter or not, there might be certain scenarios where you might want execute a common piece off code for each and every bean. Before there, innit Matter is called and another piece off court that they'll be executed after their innit method is executed. Well, Spring will give us that opportunity with an interface called Being Post Processor. Let us go to the Eclipse workspace and explored the same. So here we have this over conflict file the Java file, and this is similar to the previous example. Nothing different. We don't have a couple of things to inject the dependencies and in a 10 destroy method as well. But this time I have no into just another being, and even it does have the in it and destroy method. And I'm trying to display some messages to signify that the messages coming from this bean and in or convict file. As you would expect, we now have defined those two beans along with their dependencies. Along with that, we also defined this couple off attributes in here in it and destroy methods. Now let us take a look at how we can introduce a common piece off code that will be executed Before then. It mattered off each and every bean, and after Thean it method will get executed. It is how you would do it. You just have to create a class that implements the being post Prosser interface. And if you take a look at what's inside it, all it has is couple off default method default method in interfaces is a feature introduced in Java Abortion eight. If you're curious as to how it works, then you can take a look at my job. Of course, on the same, we talked about it in detail. But even if you don't know, that's fine. So all I have done here is we have provided the behavior for those two methods by default. If you notice these methods are accepting couple of perimeters and these perimeters will be sent by the spring container. One is the being itself and the other is the name of the bean. And in here, the default code would just simply return the being as it is. But if you override it like we did here, then you can all right, the behavior and return an object that's unpleasant. So this method will be called before the end admitted for each and every being in the container, and this will be called after the indictment that gets executed. Let's go to our main program. Let's run the program pretty straightforward. If it ignored off the sequins off action that has that took place initially, the constructor injection for this over conflict bean has taken place. And then the center injection for the same object before, innit? Method? So the call has gone to the bean, both Prosser Bean. And so we're able to see this message, and after that, then admitted off civil conflict was called. And after that, as you would expect, this method will be involved. And so we're able to see this message and same set off event would occur for each and every bean. And so we're able to see the same for the another sample being that we had created. After that, the beans are available for us to use in the application. And here I'm just trying to simply display message. This message is coming from the main class. And then after that, once we close on the context, the destroy method off put the beans are called. And finally the container will also destroy the objects. So it's as simple as that now. The $1,000,000 question. When do we actually want to use this feature? Well, in my experience, I've never used it, and I doubt if I have a bill. But this mechanism is utilized by the spring itself to provide a severity off features. If you go to the documentation offspring in their official website, spring dot eyeball, and if you search for this interface, you would notice that it has a lot off implementations already. So these air are part off the spring libraries that will be using indirectly in our coming projects. So although we won't be using it explicitly, Spring is actually using that feature for itself. But Justin guests, if you have something that you wanted to perform before each and every beans in it mattered , then you have the provision in to make use off this feature. Nobody is going to stop you. Let's quickly take a look at the Java conflict as well. And here is the cord pretty straightforward, nothing really different. The only difference between this example in the previous example in terms Off Java conflict is that I have now introduced this new being in our Java conflict. It's as simple as that, and from our main method. Let's run the program and see the result. And by the way, I'm also going to make this project available for you to download so that it can take a look at the output and play with it. So here we have the output, and it worked pretty well. Also, I want to quickly point out that in case if you have in the in it and destroy mattered for a lot of beans and they're having the same name, then you can actually take this out from the bean tag and then instead make it part off the bean stag itself, just as you're seeing here, and this will work as usual, but do make sure that the method names are consistent across all the beans wherever you're using these methods. So let's see if things would work and off course. Let's change this back to XML and run the program or that we seem to be having some ever. Okay, I believe the attribute name supposed to be default happen. And it mattered. And same is applicable here as well. And in kids, if you want to. All right, These You always have the freedom to do so in the bean tag. No, let's run the program and see the result. And this time, it worked Well, All right, I'll see you soon. 18. 0705SpringWiring: in a typical spring application, there could be a lot of beans or components that are dependent on each other. And in one of my videos I was mentioning that regardless off the order in which the beans are defined in the contract files, Spring is modern of to understand which being is dependent on which being and performed dividing operation. In this video, I'm going to prove my point. So what we have here is the exact same example as previous. Let me run the program, and if you notice the silver conflict, bean is created first and then the another sample bean. But let's say that the silver conflict is dependent on another sample being, which means we are not expecting spring to create the another sample being first so that it can be injected into the server conflict. In order to do that, let me just quickly introduce a constructor in the server conflict. Let me get it off this and let me introduce a new property off type, another sample being so this is going to be private. Let's do it some name and now using eclipse, where does create the constructor with para meters and Of course, we're going to get it off this conflict parents, because that is being injected the Zen center injection and from, or XML file. We're not going to have another perimeter, her constructor argument and this is going to be off reference to this being. How simple as that. And of course, we're going to get an arrow in here. So that's the beauty of Java conflict file. You would instantly get the compilation ever. So let's pass this mattered or this object as an argument, and this trip fixed the issue. All right, let's on the program now and see the result. We seem to be having an error. Let's take a look at what it is. Okay, it's saying unsatisfied dependency expressed through Constructor, Pyramid of four. The Parliament of For is the one which we've just defined. Am biggest argument values for perimeter type. Okay, I got the problem. We missed something in here, and that is with the type. Obviously, this is not off type string. We can either get it off. This all together are define the type. Now everything should work. This is a very reason again. Java conflict is better. I hope you're now convinced that Java conflict is better. It's on the program. And this time, as expected, Spring has called all these methods and then the end of the sample being is qualified to be injected tau another being in our case, we're trying to inject it into this of a conflict. And so you're seeing the following result. And after everything is done, the beans are made available for us to use in the application and finally destroy those objects. It's as simple as that. But the point that I want to make here is spring framework, its modern of to understand the dependencies and perform dividing off all these components etcetera and our job. A contact file would work as well. Let's quickly run the program and off course it will work. And sure enough, don't bother about this message. This just an info message, not netter. All right, that's about it. I'll see you soon 19. 0801AwareInterfacesTheory: so far, we had explored all the phases involved in the being life cycle. But one thing that we had left out is this face haven't office call back method that comes in picture right after the dependencies are injected and before initializing method. Well, there are hosts off interfaces you can use in your being to get access to some spring specific objects to do a variety off things. Basically watch it would do is you would implement one off the interfaces so called ever interfaces. And when you do, spring will take care off, injecting its respect to object into your being. We're going to take a look at all those examples in this chapter. I also would like to take a moment to explain how our code is organized for this chapter. Instead of creating multiple projects for each and every example for this chapter, I have created all the examples in a single project. Once we import the project that is bundled along with this chapter, you should be able to locate all these packages. Well, each and every individual package responds to a unique example. For example, this package in here that ends with be any stands for being named ever here. We had used the being named ever interface to get the being name. And if you expand it, he would see all its files in here that we're going to work on to demonstrate that specific example. And in addition to that, we also have the Java convict file that corresponds to this package or this example. And the name ends would be any. Similarly, we also have the XML file associated with it. Likewise, we have a couple of other examples and their associated conflict files. I just want to let you know before we move on. All right, I'll see you soon. 20. 0802BeanNameAware: okay. And this week they were going to take a look at a very simple example off ever interface and say that you have a bean within which we wanted to perform a task that has something to do with the being name. Now we can actually hardcore the being name itself in the class. But it's definitely a bad practice because in future, the name of the Bean can change while our project is pretty small and we know the name of the bean. But in large project, you don't don't really remember the name of the each being. In that case, he can leverage the functionality off the ever interface. So let's see how it's done. All I have here is I've just defined a string which is going to hold the name of the bean in future. And here I'm just simply trying to display that name as simple as that. And in our main logic, it's pretty straightforward. I don't think I have to explain this, and our convict file has just one definition off the being, and that is off this sample being we have in here pretty straightforward. That's on the program and to the result. Currently, as you would expect, there is no being name assigned inside that bean, and so he would see a no. But now let's try to do something. I won't implement one off the so called and ever interface that spring provides. And by doing so, I have to provide the implementation off the only method present in the bean name ever interface. And here it is. This is basically etcetera that will set this value as simple as that. But who is actually going to take care off injecting this particular name? Well, it's spring. We don't have to perform the injection explicitly from or conflict file. But spring will take a look at all the beans and try to identify the beans that uses the ever interfaces and accordingly, spring itself will inject. The dependence is, and in this case, we're using the settle injection, and we don't have a constructor were off injecting in this case, though, and that's all about it. Let's on the program and see the result, and it worked. Let's try to get it done from the Java conflict as well. Now let's try to make changes to the name of the bean. By providing that attribute, I'm going to change the name, toe something else. Now that's on the program, and we should be able to see, well, an exception. No such definition exception. All right, this has something to do with this in here. So let's quickly change this and on the program, and it walked. And this explains. Now, if you're a bear of the Observer, it is. And Patton, this is some more similar here. The Observer is the bean, and spring is going to inject its dependencies using the center injection. Also. Now you may ask me if we are tightly coupled with the spring framework because we're trying to use the spring specific interface in here. The answer is of course, yes. You have to see the pros and guns. If you think you're able to add value to replication by using these ever interfaces, go ahead and use it. But be aware that it might cause your cord to be tightly coupled with the spring architecture. But let me tell you, it's not possible that you have a completely loosely coupled architecture. You have to couple the components to some extent to get them working, and it is pretty expected. Nothing unusual. So that's about it. That's a quick overview off ever interfaces and how to use them. We're going to take a look at a couple of more examples and then we'll see what we can do. All right, I'll see you soon. 21. 0803ResourceLoaderAware: Okay, let us take a look at another example off ever interface. And in this case, let's say that I wanted to access a resource that recites in the file system and do something with it. In this case, I'm trying to read a properties file, and I'm just trying to display the values just as you see in here. This technique is often used for internationalisation, often application. And if you're not ever off, what is a properties file is just a simple file with key value pairs. The key would remain unique. But the values may change, and this is usually the preferred way to store the text displayed in your application. If you want to know more on properties file, you can explore a bit about it. But this file in here is reciting under the E Directory and under the temp folder with the name Test our properties. So this is the resource that I want to get access to Tobe able to read all these values and in order to load this resource, we need this object resource loader, which were expecting spring toe inject into this being using the settle injection. It's as simple as that. And for that very reason, we have now implemented the resource loader ever interface, which has the following settle. Method and rest will remain same just as our previous example. And if you go through this logic, we're trying to read The Properties file isn't the input stream and then trying to display its values Well, this is all core java. Let's go to the main chance and try to run the program, and it worked pretty well. Now it's not necessary that the resource has to reside in the file system. It can also decide in the class part if you want to pick a results from the class part instead of saying file, you would instead say class part. But do make sure that this file is reciting in the class part, though if you're using a maven project, he can keep that file under resources, and here we have it. And here are the bunt off key value pairs. Let's run the program this time and see a different result. And it worked Tassan Blazer and say, Mazza caspit Java conflict. Nothing different. You can even loaded resource using, and you are Oh, if you wish. So instead, off class spot and you would provide the You are How did that sit on this? I'll see you soon. 22. 0804ApplicationContextAware: OK, in this video, we're going to talk about one of the key ever interfaces, which is the application context There, there, this Villela was to get access to the application context object itself, which will be injected by the container using settle injection just as you see in here. And then we're trying to do something in here with that object and for the sake of this demonstration have actually created another bean. I would just simply has one method and that's just simply going to display the following message and from our sample being we get access to that being isn't the application object that is injected by the container and we're trying to call its method Hasn't Blezard apart from that, the application context object also has set off other methods. I'm trying to use a couple of them in here. Well, this will display what is the default class loader. In our case, this is going to be the Java's default class loader and ah, Spring uses it internally to load the class files etcetera. And here we're trying to display the enrollment and this includes profiles that we had set etcetera. We're going to talk about profiling and all incoming videos, for sure. And similarly, we got several other methods, which you can just simply take a look at. Just can't through all these methods and read the description. The documentation is available right here and get a sense of what each method is doing. And if you notice using the application context object, you can pretty much get it off All the apparent professors that we had talked about so far . For example, you can even get access to a resource in the file system or by using a Ural or even by class path by calling this method get resource, and then he would provide the location of the resource. So it's a pretty powerful object, and you can certainly livers the functionality as you wish. So by using this method, get parent, you'll be able to get dependent. Context object. In our case, there is none. Similarly, can also get the parent bean factory. All right, let's on the program, and this works pretty well. Message from Another bean and other stuff that we're trying to print. Also note that it's not a good practice to actually try to get access toe being by using an application context in this manner. The reason is this may actually destroy the purpose off purpose off introducing spring to avoid tight coupling. So in here, if you observe this being is asking for its dependence is. But the dependence is not being injected by the container, so usually it is a bad practice to try to ask for dependence is from your being in this manner or in other words, you should try to avoid dependency. Look up from your being. Whatever we're trying to do in here is called a dependency. Look up and we'll try to award that. This is just a quick example on the application context of air. And this example doesn't really demonstrate the usefulness off the application context being injected as a dependency. A better example off. This would be demonstrated once we talk about the bean scope. So you need to hold on until that point, all right, How does he assume 23. 0805OtherAwareInterfaces: in case of your curious Astor water all the available ever interfaces, then search for the interface ever, which is the parent interface for all the ever interfaces in the official website offspring Dark start spring dot Ivo and then you would notice that it has a lot off sub interfaces and we had all that explored some of these. We've talked about the application context ever. We've talked about the being name of air, and they've also talked about the resource loader at their. Likewise, we got many other interfaces and each would serve a different purpose. And in fact, we're going to be exploring some of these incoming videos as we progress through the course . For example, when we talk about even handling in spring, this interface will come into picture, the application, even publisher of air. And if you're developing a spring embassy application, then thes two interfaces will come into picture. So stay tuned down the line as we progress through the course. We will definitely be calling a great deal of these and professors either directly or indirectly, and Justin gets If you want to have a quick glimpse off what these interfaces are doing Then just click one off these interfaces scroll down a bit, and then it should be able to see the description off it. So just try to get a glimpse off what they're doing and down the line will have some practical examples demonstrating the same. All right, I'll see you soon. 24. 0901SingletonDesignPattern: okay, Before we talk about being scopes in spring, let us try to understand what is a single turned isn't baton in general. Thereby will have some solid foundation toe understand scope off beans in spring. So what is the singleton designed Patton single Done designed? Baton ensures that you would be able to create only one instance off a class, and you would use the same instance across your application. So no matter how many times he would be needing that object, he would use the same object again and again everywhere in your application. We're going to talk about some of the scenarios where this might be useful, But let us try to understand how we can bring up a very simple example off single time descent baton. So what you're seeing right here is a simplest example that I can think off the Demonstrates, the Singleton's and Patton. So now my goal is actually to restrict the creation. Often instance, off this class toe, only one. Nobody can create more than one instance off this class, so let's see how we can accomplish that. So the first thing that I did is a made the constructor private What this means is when the constructor is a the public or default. We can actually create an instance off this class from elsewhere outside this class. But if I make it a private constructor, then I can only create instances off this class from Britain the same class itself, not anywhere else. And that's what I'm trying to do in here. I'm trying to create an instance and assign it to this variable. But before I do that, I'm also checking to see if the instance is now. If it is not only then I'm trying to assign that instance in here. And then finally, I'm just returning this variable when this matter is called, and that's all there is to it Now. We thought off accomplished a single turn designed Patton because the first time that a request for an object this is going to be no and the condition would be satisfied. And so there's going to be a new instance assigned, and I'm returning that in stance. The second time, a request for in stance the condition would no longer be valid because we already had an instance, created prayer to ask him for it. So Likewise, for all the subsequent requests to get the instance, we would keep getting the same in stance. But there won't be any new creation off Dane stands. So let's go to our example Main method and try to demonstrate the same. I'm just simply going to put a since our statement and then I would say single Thanh example. Dark getting stance and then, if I wish, I can call the business logic method. But here I just simply want to print the hash court off the object, and I'm going to do this several times. So no matter how many times I'm going to request for in stance only for the first time, the instance would be created and forest off the instructions. This condition will not be satisfied, and so I keep getting the same instance. It's as simple as that. It's on the program, and sure enough, the hash code is displayed all same. That means we're trying to get the same instance again and again, but unfortunately wouldn't quite accomplish our goal off. Singleton designed Patton because was still left a loophole where there is a possibility to create multiple instances off this class. The possibility is there when you have a multi traded enrollment where multiple threats are trying to access this piece off logic at the same time, then there could be a possibility that multiple threads well satisfy this condition and enter the section off, cored and create multiple instances. Let me just try to demonstrate the same. I'm a gated off this from here and let me create a new thread. And I'm going to use the Lambda Expression and Justin goods if you're not aware off Lambda Expressions. This is a feature introduced in Java abortion aid and my course on Java programming explains this beautifully, and what I'm going to do is the exact thing that I'm trying to do in here. I just want to get the hash cut off that instance and print it. I'm going to say trade dot start, and I'm going to do it multiple times that's on the program. Okay, This time, coincidentally, all the threads went in sequence. But if you keep running so there we have it. We have a hash code that is different from others. That means we now have two instances off this class, So this object is not a singleton object. And this would be more evident if I try to keep a timer in here. Are are just simply use thread dot sleep and I would wait for 1000 seconds off course when it dropped the court with try, catch block. And now let's run the program. And you would notice that all the objects are different. That means we have. If you have 10 different threats, you're going to have 10 different instances off that class. Definitely not a good thing if you're planning to accomplish the single to understand baton . So what would be the solution? Well, the solution is quite simple. All we have to do is to wrap this piece off coat within the synchronized block, and I'm going to do just that. And I'm going to let this class objects acquired the lock. Now, this is all core java. I'm assuming that you older in all this stuff if not again, you can check my course on the same. It's a beautiful course rated very good. I'm gonna have this check here as well. Hopes. Why is it not getting copied? Okay, It's to control sea and control the perfect. So Now what I'm trying to do here. Let's say that there are a couple of treads which are trying to get inside the section off code, and let's say they both have this condition met center here, and one of the thread would be ableto acquire the lock and entered the section off court and then created. Instance. When second threat tries to enter the synchronous section, this condition will not be met anymore and hands it would grab the existing instance. It's as simple as that now the striker on the program. And sure enough, this time we have the same object everywhere. So now the real question. Why would you ever want to use a single Tenders? And Patton? Well, the answer is quite simple. Why would you want to create so many instances when it doesn't make sense just to let you know? Creating an instance demands bit off a performance, and if you go ahead and create instance, everyone's a vial that's going to cost some performance. Moreover, creating an instance would also take bit off memory, and there is a possibility to come across with someone foreseen exceptions like part off memory exception, etcetera. So hence, we always should try to create the instance creation toe minimal and more ore. This class in here does not have any properties. That means the instances that we create from this class will not hold any state. If you're ever offi JB's, this is equal into a stateless being. The object doesn't hold any state. All it has is set off methods, and they perform a certain task. One good example of this is when you're trying to create a database connection. You don't want to create a database connection every time you contract with the database. That's going to cost a lot of performance. Rather, you would just create only once. In other words, you would implement a Singleton descent Patton to create the database connection object, and you would use the same object everywhere in your application and save the performance. Like was. If you're aware off logging mechanism, you just create one instance off a logger, and then you would use it across your application to print the logging messages on the standard output. But Singleton patent may not be ideal choice. If your object is going to hold some state, we're going to talk about it in coming video. I'll see you soon 25. 0902DontUseSingletonWhenObjHasState: okay, I was mentioning that he shouldn't be using the single done designed baton. If you're expecting your object to hold a certain state well, in the sweet, I'm going to prove my point. And for this purpose, I went into just a new property in here that's going to hold the state off this object and in order to keep things simple. Is this going to be a string? Let's call it a message. So instead of setting the message from a method argument, we would introduce the settle method to do the same about a soldier, settlers and getters. And then I want to gender it. Only the settlement ID. Let me make the necessary changes in the main class as well. And then I'll get back. Okay, here we are again, and all I'm trying to do in here is I'm trying to create multiple treads. So here we have four different threads, and each stretches essentially hand the same base off court with slight variation. So in each thread, I'm trying to get the instance. Remember, we're still following the Singleton descent, Patton. So all these threats would get the same object every time and each threat is actually taken gear off, setting the value or no case setting the message. So the first thread is setting it s t one second thread as t two seamers with other tragedy as well. And then after that, I'm just trying to wait for a second. This method simply has a logic that keeps that read on hold for a second. And then I'm just simply trying to call this method which would just simply display that message. No. What is output that you're expecting? If I run this program, just try to imagine and guess the output Well, this is actually going to mess things up. Let's see what's going to happen. So here it is. I'm expecting something like t one t two d 34 all the not in same order, but here. We're seeing a completely different result. Why is this happening? It's quite simple. All the threads are trying to set the value on the same instance, and the last read that serves the value is the value that will be remained for that object until all the objects are just trying to display that particular message. So this is clearly a problem And what is the solution, don't you? Singleton Patton? Instant creating instance for each and every thread. And soon we're going to talk about how all these can be applicable in spring framework. So stay tuned. I would see you soon. 26. 0903LazyAndEagerLoading: in this way, they were going to try to understand the terms lazy loading and eager loading, Whatever the example that were talked about in our previous video, his tone does lazy loading because the instances created when we need it only when we request for an instance would then stands be created, for example, to go to the main method. What we're doing here is we're calling this mattered gettin stance off Singleton example. And that is when the instances created not before it. This is called lazy lowering. Now we're going to design our singleton patent in such way that the instance would be created, pray or two requesting for it. And here is example of the same. And as you would guess, our instance, creation code would go inside s static block and the static block would be executed when the class gets loaded into the memory. And every time we request for an instance, we're not going to create an instance but rather were just simply returning an instance as is. And here are the set off statements where were using that instance Sassen Plaza. So that's the basic difference between lazy loading and eager loading And if you run the program, everything works. Spell. I'm going to make this project available for you to download so that they can take a look at it. Now, from our next video, you will comfortably understand What is a singleton in spring. I'll see you in my next video. 27. 0904SingletonInSpring: let us try to understand singleton beans in the context offspring in spring, By default, all the beans are singleton. That means you can only ever create one single instance por Bien and ah, in order to demonstrate the same, we have a couple of beans defined in here the first off, which is with the name another bean, which has a very simple structure. It has one simple method that takes in one Param Eter and displays the same. And know that this class does not hold any state, no properties of whatsoever. And in our sample being we're trying to inject this another bean is in constructor injection and then we're going to use that object to call its methods and perform certain task. Now imagine this is kind of like a doll object. Are Dow class off your application? If you're better off MBC architecture down stands for their axis object and typically this is how the floor would be. That means the Dow object does not hold any state in itself. But you asked me We have this property in here. I'm going to talk about it after we run the program and in our main class in order to prove that the beans are singleton in spring. I'm just trying to create multiple threads and in each thread, I'm requesting for the same instance off sample Bean. And then I'm trying to get the hash code just as you see in here. What I'm expecting here is that all the hash courts should be seen. And right after that, I'm just trying to call the method sample method and trying to send a threat specific. Param Eter Just as you see in here. Now, let's try to run the program and see the result. And it worked pretty well. All the hash courts are same no matter how many threats you have, and the messages are also consistent. But it is you, as a developer who have to take care off, eliminating the possibility off thread rece condition. How many for take a look at this being or the class all that is holding one property? There is no possibility that multiple threads can edit this field. I mean, we're not performing in the centre injection to let any other thread to modify this object or whatsoever. Even this being is stretched safe because it's not holding any state. All it has is behavior, and same is the case in here, and each other thread would send a unique message that will be processed based on the logic that you have in here. So ultimately, it's safe to say that these two beans are thread safe, and so a Toki that we can let them be, Singleton instance, is by default. The opposite off single turn is prototype, which will talk about incoming videos, for sure, But here's a quick example that demonstrates Singleton in Spring Java config. It's pretty straightforward as well. Let me just quickly change this and run the program, and this has worked as well. All right, I'll see you soon. 28. 0905SpringLazyAndEager: by default. Spring beans are eagerly loaded. That means all the beans would be created right when the container loads and before you request for a bean. In order to demonstrate the same, I have put this message in the constructor of sample bean. So when the instance off the sample being is being created by the container, we would be able to see this message. And in our main method, we pretty much had the same code except the hand of included the following message. Rick listing for a bean. We're trying to display this message before we're requesting for a being for the first time . And now if you run the program since the beans are loaded eagerly, that means the constructor is called before we requested for being in stance. If you wish, you can all right, the default behavior and make these beans load in lazy manner just as we are. And for that you just have to include one attribute that's lazy in it, and then you're going to make it true. Just simple as that. No, it's on the program. And this time I'm expecting this message to be displayed first and when we request for the object. For the first time, the constructor would be called. And sure enough, everything worked well. In case if you wish to load all the beans and lazy manner, then you can just move this particular attributes to the root tank and call it default lazy in it. And this works for all the beans now and now. This is applicable for all the beans, and it worked as expected. And when you make all the beans as lazy, there might be instances where you might want to make a particular bean as eager. In that case, you just have to add that attribute in the particular bean so that that particular bean would be loaded in eager manner. You just have to make delays in it, too falls, and that will work without any problem. Although this are put, doesn't really the monster that, um, trust me, it works in order to do the same from Java conflict. You just have to include an annotation, and let's try to do that. Let's I want to make this particular bean lazily load. I would just say Lazy Tess and bless that and and it has worked and If you want to make all the beans lazy, you just have to move this particular annotation at class level and things will work pretty smooth. And finally, if you would like to make particular being lord eagerly, then oops, you just have to set the flight to faults and it works as well. So if you have certain means that you're not going to use it very frequently, then it's better to load them in lazy manner. That means only when you require those beans would you need it in stance. That will save a lot of memory and performance as well. For example, let's say that you have a database connection object. Then you might want to load it in eager manner because it would be used across your application very frequently to interact with the database. All right, that's about it. I'll see you in my next video 29. 0906SingletonInSpringvsSingletonPattern: I gave an example off Singleton descent Patton to experiencing Wilton in spring. However, they're a little different from each other in the single ton designed Patton. The Singleton instance would be unique, per jbm, but whereas in spring it is unique, per container and pour bean, let me explain what I mean. So what I have here is on simple being with the default constructor and inner conflict file have declared a couple of beans. Now do taken note that these two beans, the long the same class, but the only difference is their name. And from all main logic, all I'm trying to do is to retrieve those beans. And then I'm trying to print the hash cord. Now what the hash could be same are different. We'll ask for the singleton designed pattern that we had talked about. They're supposed to be same, but in spring, like had all that I mentioned. The Singleton instance is Par Bean with a unique name. So this in here would actually result in two different hash cords. Let's on the program and see the result and Harris. So that's one quick point to note. Hold it. That's about it. I will see you soon 30. 1001PrototypeScope: okay, The opposite off single don't sculpt being his prototype scope being that means every time workers for a B in we're going to get a brand new instance off it, which is quite opposite to Singleton beings. In this week, we're going to take an example of the same. But before that, let me quickly demonstrate the problem with the singleton scope beans in spring. And the example that I'm about to show is the same problem that we had taken. Look at when we talked about the Singleton descent. Patton. I'm in this time our sample Bean is holding a property. That means all the instances that we're going to create from the sample being would now have a state. And as of Alder demonstrated, we don't want to set the scope off a bean to Singleton when it has a state. That's a simple rule off stump, and we've already seen the negative implications off it. I'm a quickly demonstrate the same. So for this purpose I have created multiple treads, and in each spread, I'm setting a threat specific value, just as you see in here, and in order to make sure that the output is inconsistent. I've also included the following statement in the sample method, which we're calling from each and every thread. So the last thread that will set the value or the message is the message that will get displayed in all the threads. It's on the program and see the result. So this is what I'm talking about. This is very inconsistent. And also note the hash court is same as before. Now let us change the scope of the bean two so called a prototype. Not this time. If you run the program, I'm expecting the hash code Toby unique every time. Workers for the being. And it should also solve the problem we had with multi threading. And sure enough, things are sorted. And if you'd like to do the same from Java con flick, it's quite easy. All you have to do Hiss tinkler the scope annotation by setting the value to prototype just as you see in here. And that's all. Let's switch to the Java conflict and on the program and the result is pretty good. All right, we'll talk more on the same lines. Incoming videos. I will see you soon 31. 1002PrototypeBeanLifecycle: okay. We have seen that spring manages the lifecycle off a bean, but it may not be able to manage the complete lifecycle off a product I've bean. Here is a statement from the official spring documentation and here's what it states. Spring does not manage the complete lifecycle off a product I bean the continuance. 10 she ate come figures decorates in otherwise a symbol separate type object hands it to the client and then has no for the knowledge off the prototype instance, this means that while initialization callback methods will be called on all objects regardless off scope in the case off prototypes, any conficker destruction lifecycle callbacks will not be called. It is a responsibility off the client code to clean up the prototype scope objects and release any expensive resources that the prototype beans are holding on to. What this means is Sprinkle taker off. Calling the lifecycle callback matters before the object is made available for you to use, and after that it just does not have any hold off that object. And so all the life cycle callback methods that come after it like, for example, that destroy mattered cannot be called by the spring container. But that being said, we can work around it and create our own implementation. We will talk about it. Incoming video. But let us take a look at an example off this scenario. Okay, here we are again. What we have here is pretty much the same example as the one that were looked at in being lifecycle chapter. So inside over several conflict class. I just have the unit and destroy mattered and have declared those two methods in here as part of the being tag. And we also have the being post Prosser class. It's as simple as that. Let's run the program and see the result. But do take a note that we didn't defining the scope of this being. So it has defaulted two single down and here is the result. As expected, all the life cycle called back methods were called, including the destroy call back method. I'm talking about this, but now if I make this being a prototype scope being then everything I mean all the life. Second callback methods will be called until the point object is made available for the application. But after that, since spring does not have control off it. Spring cannot take care off calling the destroy method. So I'm going to change the scope off this being to prototype. And now if you run the program, you'll see it missing. This matter is no longer called. So now the real question. Why does spring does not handle or cannot handle the complete lifecycle off a product I've bean? Let's take a look. All right. Here are a couple of reasons which I can think off as the White Spring cannot handle the complete lifecycle off a product I've bean. The first off word is when the client called request for a Singleton instance Spring does not. Our spring does not give the instance itself, but a reference to it. The reason why Spring had to do it because there might be other threats, which may be requiring the reference off their object as well. So hands, in case off singleton scope Beans Spring have to keep the stance with it so that it can manage giving the references, and since the object is with spring, it can take it off, managing the complete lifecycle. But when you request where a prototype bean spring will create the bean and gives it to you . It does not keep the instance with itself and sew. It does not have any hold off it to manage the rest off its life cycle. It's as simple as that. But Spring will try its best by calling all the callback methods before the object is handed over to the trying code. And after that, it is you as a programmer who have to take her off all the destroying formalities, if there are any. And as I've already mentioned, going to take a look at such example in next video, we're going to use the being post Prosser to accomplish the task. So we indeed will be able to call the destroy method off a product I bean. And here is another reason. Even if you somehow June the spring framework to handle the complete lifecycle off a prototype bean, then it may simply make the container very inefficient. The reason is a singleton being is guaranteed to have only one single in stance. But whereas for a prototype bean, it can have hundreds, thousands or even millions off instances, and we can't expect spring container toe handle the complete lifecycle off each and every bean that ever exist in your application is just simply not practical at all. So that's about it. I'll see you in my next video. 32. 1003CallingDestroyMethod: as I was mentioning in my previous we do that we can actually work around and indeed called the destroy matter off a product I've bean in this week, I'm going to demonstrate the same first things first, since this being he is scoped toe prototype. The destroy face cannot be controlled by the spring container, so there's no point in specifying this attribute. I'll get it off it, but what we can use, rather, is this interface disposable being and that would let us provide the implementation to the destroy matted. But spring still doesn't call this method. We're going to actually introduce another bean, which is going to be a single done being. And there were going to write some tricky code that will ultimately call this method off a product I've bean when the container is closed or when the shutdown hook is called. And here is where the magic cord is reciting. Don't be overwhelmed by looking at this code. Let me walk you through and then you'll understand it. So what I'm trying to do is I have introduced this interface being Post Prosser, and I'm going to keep this method. Ozzy's. I'm just simply returning the same object. But I'm going to provide some behavior toe this method in here Post process after nationalisation. So this would be called after the unit matter off. This being is called in this case, we don't have it in it method. So that's fine. And what I'm trying to do here is I'm actually making use off the bean factory object which will be provided by the container. And in order to acquire this object, I've also implemented the being factory ever interface and ah, using the center injection, I'm letting the spring container inject the being factory object for me and in here using a synchronized block. I'm just trying to add all the beans off prototype scope by using this method off being factory ease prototype and then I would provide the name of the bean. And remember the post process after nationalisation mattered will receive these two para meters by the container when it's called. So by the time the container makes all the beans available for us to use in the application , this list in here must be populated with all the prototype beans available in the container . And after that during the destroy phase off this particular bean. We're just simply trying to loop through that list and simply calling their destroy method . It's as simple as that. And we all didn't know that this destroy method would be called before the container gets closed. Our before they shut down hook off JV and would be called and in or conflict file. We just defined despite it. I've destroyed bean just as you see in here. I was chained the name. Okay, this is little misleading. Same should go in the Java Khan for Gassville. So quick correction there. So since has bean is singleton scope bean spring will have complete control off its life cycle. And so all these methods would be called. And when spring caused the destroy method internally were having a logic that actually caused the destroy matted off each and every product I bean. Now let's try to run the program and make sure that this statement will get executed. And sure enough, it worked. Also do make sure that you have this check in here to check to see if the product I've bean is in stance off disposable being. Otherwise, if you have a prototype bean that doesn't implement the disposable being. You may have a runtime exception because that being made, not implement the destroy mattered. So make sure that you have this check before you call the destroy method and, ah, everything should work smooth. And Tim is the case with Java conflict. Nothing different. Pretty straightforward, very easy stuff. And just try toe run it again on it worked. So just try to walk through this cold and try to understand what you're trying to do or if it is clear, that's fine to simply move on with my other videos. All right, that's about it. Elsie ism. 33. 1004NoteworthyPoints: Okay, here are a couple of noteworthy points on productive beans, but before that, I want to ask you a question. Would the product I beans be eagerly loading or lazy lowering by default? Think about it and tell me the answer. Well, actually, the first point in here answers it. A product I've Bean cannot be loaded eagerly for two good reasons. First of all, the whole idea of product I beans is to create an instance when you ask for it and container does not know ahead of time. How many times you're going to ask in your code. It's as simple as that, and for that reason, it doesn't matter if you said the lazy load flag toe. True or false, it will not take any effect at all. Or, in other words, it doesn't make sense to us the spring container to load the prototype beans in lazy manner . Also, it's important to note when you ask for an instance off a product I've bean from the container. It's as good as creating a new instance using the new keyword. But instead of using the New Key world, you would simply do it dependency. Look up And in order to do the look up, you would need the application context. Object. And for that, you just make use off the application context ever interface and a Sprinkle taker off, injecting that object. And so you can reckless for that bean. All right, I'll see you soon. 34. 1005MethodInjection: in this video, we're going to talk about matter injection technique. But before that, let us try to understand the following scenario. Let's say that you have couple of beans. One is scope to Singleton and the other is the product I've bean. And let us assume that the singleton being is dependent on that I. Bean, which is just having a constructor just to keep things simple. And then inside this class, we also have on method get new silver. And every time we call this method from our code, I'm expecting a new instance off silver to be written. Now what we're trying to do here is we're trying to use the constructor injection to inject an instance off this bean into this, and that's what we're trying to do in our conflict file. But as we observed from our learnings offspring being life cycle, all the dependencies would be resolved during the instant she ation phase and before the objects are made available for us to use in our application logic and for that reason, spring is actually going to create a new instance off. But I bean and is going to inject it into this server conflict credible and good, but that's about it. It's not going to inject every time you need it in our mean class code. I'm just simply trying to call this method off Singleton being and I'm trying to print the hash code and I'm doing it three times. What I'm expecting here is that the hash could would be different. That means I'm expecting a new instance every time I need it, because the object that I'm requesting is off type server, which is a product I've bean so ideal every time I request for it are supposed to get a brand new instance, but unfortunately, because off the way the spring being life cycle works, this is not possible and let me on the program and that would be more evident. And here it is so alternatively, Aiken just simply keeping simple get it off the constructor injection altogether and thereby we're just going to simply introduce a new key would and returned the silver object every time we call this method. Now this is going to work. But as you might have guessed, this is off course about practice because this class would now be tightly coupled to this class. We don't want that. So alternatively, we can also perform the dependency look up by implementing application context Ever interface. I think I got the name wrong application A shouldn't context and there and then you're going to use this object to perform a dependence and look up off this silver being every time we call this method. But obviously, as I had already mentioned in one off my previous videos, that may make our class tightly coupled with the spring framework, and we should try to award it as much as possible. So alternatively, Springville providers another functionality, and that is called matter injection are look up matter injection technique to be more specific, so let's see how it's done. So all you have to do is your going to make this method abstract, and when you make that method as abstract, you don't have any implementation. And when you have at least one mattered as abstract, he must make the class as abstract as well. I'm going to use this key word here as well and from, or conflict file. I'm going to introduce a new tag called Look Up Method, and I'm going to specify that method name but this name, attribute and also the being and I'm going to refer. So this being in here, well, this might look a little tricky, but what's actually going to happen is Wellspring is actually going to create an implementation for this method, and that implementation would just simply return an instance off. Whatever the bean that you specify in here, it's as simple as that. And that would happen when you call a method with this name that belonged So this bean. So guess what? That's all there is to it. And if you run the program, he would see the hash code is different. Which means every time we call this method, we're going to get a brand new instance off silver and same thing can be accomplished with the Java conflict as well. And here is the code that's from the program, and it works as well. But here is how he would deal with it in Java conflict. You just simply tried to return an instance off silver conflict, and since it has an abstract method, you have to overwrite the behavior off that method, and you're just simply return the silver object Justin Plaza. Now with Java conflict, things would look more obvious than with the XML conflict. Here. We're just simply calling this method which internally just simply returns the silver object. And every time you call this method, I'm in this method. The call would go to its implementation. An implementation would just simply call this mattered to return this object. And here is one more reason why you want to use Java Contra Gore Tex Immel conflict. All right. I guess that's about it. I'll see you assume. 35. 1101SIAutowiring: in this chapter, we're going to talk about auto wiring. That means we're going to ask spring to perform the wiring operation for us so that we can save some effort from our end. Before we take a look at a simple example off water wiring. Let me just quickly walk it through the scenario. Let's say that we have the sample being and is dependent on couple off other beans. And here are they. These are just simple class files, and they both have one method, and they print the bean specific message. Just as you see in here and inside our sample being, we also have their corresponding settle methods to let spring inject the dependencies heels and settle injection. It's as simple as that. We also have one more method that just simply calls the methods residing in this dependent beans so that when we call this method, we will be sure that the dependencies are indeed injected and no main classes. Pretty straightforward, we're just trying to call the sample method as simple as that inside our conflict file. I've just defined all those three beans in here now, since the sample being is dependent on this to beans. I have to now add the property tax and refer to these beans. But let's say that I'm too lazy to do that. So what am I going to do now is I'm just simply going toe ad the following attribute or the wire by name by specifying this. I'm asking spring to go take a look at the name of the properties in here. And if these properties are having the settle methods, I won't spring to go take a look at the beans with the exact same name as I defined as properties. And if Spring finds those beans with the same name, I want spring to inject those beans into my sample being it's as simple as that. Now, if I run the program without this, no beans would be injected and so will have no pointer exception because my default this would be no. If I enter just that attribute dough, the result would be good. So what spring has just done is are providing. Spring has identified the beans but the same property name. And that's why I said by name, and Springhouse taken care off, injecting them to assemble as that But clearly we have one serious problem in here. If you modify the name of the bean are the name of the property, things are going to mess up. Let's say somehow I wanted to change the name off this being to another being one. And now spring just can't find that being. We'll get to know point exception. So I have to make those changes inside are court as well to get it working now. This is very tiresome process. Imagine that you have thousands of lines off court where the chain needs to go. It's quite unimaginable. And let's run the program. Okay. Still, we have another because even or settle mattered is to be changed. Now, this is quite a pain Degnan process. So clearly this is not what we wanted. Admit quickly undo all those changes. So what we can do? Rather is instructing by name. I'm going to make this toe by type. So this time Spring will try to identify the dependencies based on that type, not by the name. So here. Now, spring is smart enough to understand that these are the beans that needs to be injected, and it would inject regardless off the name off the bean. So even if I mess up with the name this time, things will still work. But we have a problem here as well. Let's say that I have another being that points to the same class, but with a different name. So now which one of these two beans will the spring choose to inject? Well, Spring will be confused. It does not take any spring would rather prefer to throw an error than risking to change the behavior off your application? No, if you run the program, he would get an exception because spring is confused as to which Bean to be chosen. So in order to solve all these problems, we have to use annotation auto wide annotation, which will talk about incoming videos. But let's also take a look at the Java conflict. Equal int off. Same. It's pretty straightforward. All you have to do is you have to know, add the following attribute out of wire, and then you would either choose by type or by name. I mean, just as we did in here in case of XML and things would work as usual. Let's run the program and it worked. But if you notice you see a strike off on this world, that's because this is now depreciated. We better not use it. So again, the solution can be found with annotations, so I'll see assume. 36. 1102AutowireAnnotation: Okay. Lets see how we can solve our problems using annotations and in order for annotations toe work, you must first include the following tag. Only then will the spring be able to evaluate the annotations that you're going to include in your beans. And in order for this tag toe work, you must first include the following names. Space, the context and here's the your for the same. Along with that, you also need to include the following schema locations just as you see in here. Now, you don't have to type in the link by yourself. I'm going to make this project for you to download, so I can just simply make use off those files. And once you do that, instead of specifying the attribute in here, what am I going to do? Rather is I'll go to the bean and I'm just going to annotate these fields with auto wired annotation. By doing so, spring will automatically look for these beans from the list off beans that you can figure in the convict file, and it will try to identify the beans by type. So even if the name is different, spring will still be able to identify these beans and inject them automatically. Let's on the program and see the result, and it worked. But again, there could be a chance that more than one being could point to the same class. Let me just copy this statement and make a double. Get off it and I'm Linda named as being differently. And now again, Spring will be confused as to which one to inject. But this time is just simply going to choose the bean. According to the qualifier, What is a qualifier? It is the name of the property that was specifying here. So this time Spring will just simply choose this bean without throwing any exception. Let's run the program and it worked as well. Now what if the name of this being happens to be different? There is no property with that name in here and now Spring will throw an exception in order to fix the problem. We can explicitly said the qualifier by using the annotation qualifier, and then you would specify the name of the bean off your choice. That's I want to point this. This is the bean I want to inject now Sprinkle. Just simply inject the bean. But the following qualifier that's on the program handed will And since we didn't add any kind of extra para meters in here are attributes. The Java conflict would pretty much remain the same without any extra element in it. Now, unlike a maximal conflict where you have to include all the name spaces, etcetera, you don't have to deal with such mess in the Java Kontic. So here is one more reason why it's better to use Java conflict over example. So let's switch over to Java conflict and try to run the program. And okay, we seem to have an exception list. Take a look at what it is. Okay, just simply say is that there is no being okay. This is not annotated with had the red bean. So this should work this time. Hopes we got some. Okay. The problem is we have set a qualifier, and this qualifier is not have valuable in the conflict. Let's on the program again. And this time it worked pretty well trying to use this project and experiment with it. All right, I'll see. Assume 37. 1103ConstructorAutowiring: in this video we're going to talk about are providing in case off constructor injection and for this purpose, I have no got it off those settlement. It's and have introduced the following constructors. One is a default constructor without any argument and the other is with a single argument. And finally we have one more constructor with a couple off arguments now from our contact file. All we have to do now is to add the following attributes and Sprinkle taker off. Injecting the dependence is no. The question is, since we have these two constructors in here, which one of these constructors will the spring use to inject? The dependence is well, it is just simply going to choose the constructor with maximum argument So this constructor will qualify for dependency injection. It's on the program and see the result, and it worked Likewise, We can also make use off the auto y it annotation without any problem. Let me quickly demonstrate the same. So this time we can have what a wide annotation set on this fields and by default or the wedding will happen by type that's on the program. And it worked. Hultin, Italy. You can also specify the same annotation on the constructor itself. So now on whichever constructor, you would specify the following annotation that constructor would be used for dependency injection. Let's try to run the program and see the result. And just as with center injection in case if you have a couple of beans pointing to the same class than the qualifier would come into picture, it's on the program. By default, the qualifer name would be Samos, the field name, and this would still work without any confusion. But if we change this name as well, then certainly this is going to cause confusion to spring and would throw an exception. So what we need to do is to introduce a qualifier annotation. And then you would specify the name of the bean testing plans that, but the thing is a con specify the qualifier in this manner because spring will have confusion. Whether this qualifier belong to this perimeter are to this perimeter. So we need to be more specific by specifying the qualifier right before the perimeter where we want incorporate the following qualifier. Now let's on the program and spring will not have any contrition. It worked well. Interestingly, Thin Spring Washing number four Door three. You don't even have to specify the annotation. If you have only one constructor, Spring is smart enough to understand that it needs to order wire the only constructor president in your bean that's on the program and see the result, and it worked. But again, it works only if you have one single constructor and it works only with constructor injection, not etcetera injection. Unfortunately, if you're using Java con trick, there is no easy way to perform constructor injection using or providing. And it's primarily due to the limitations off the way the Java conflict works. But not being said, don't conclude that this is down set off Java conflict. Incoming videos. We're going to talk about some of the pros and cons off, using or providing thereby. You have to decide whether unit to even use hotel wiring or not. But the real usage off for providing will come into picture in the context of developing Web applications using spring framework. So don't come to any conclusions. At this point, I will see you soon 38. 1104IncludeExcludeBeans: by default. All the beans can be auto wired, but there might be scenarios where you might want exclude a particular being from Ottawa writing. That means you won't exclude a bean as a candidate for auto hiding, so that nowhere in your code you can use the annotation to let spring perform the outer wearing off that bean. In order to accomplish that hole, you have to do just include the following attribute. Auto A candidate and you would set it toe false. Now, inside our sample bean. We have a dependency on that being so spring now will not be able to inject it. Performed the auto wedding, Let's on the program and we should see an exception. Let's switch it over to the maximal conflict and run the program, and sure enough, we have an exception. But this is a problem. If somebody disables it being promote a wedding, why should our entire application should be crashing? So what am I going to do is that I'm going to set a flag saying that this is not required. If this being is injected, that's fine. If not, that's fine as well. And that's what I'm trying to tell in here, but by doing so, spring will not throw the exception. But we would certainly have a null point exception in here. So as a good programmer, we need to make sure our code is tuned to handle the scenario. But let's on the program and make sure that we get Denel Point exception. Sure enough, we did. So now we're going to wrap this code with a null check and now our code should work well. Alternatively, you can also specify the falling attribute in the root tank. And by doing so, you can actually specify water. All the beans that you'd like to qualify for auto wiring in this case are mentioned that I want to be able to order wire all the beans that start with this word, followed by any sequence of characters. So this is a wild card that I'm trying to use, and this is kind of like a regular expression. And if you have to specify multiple expressions like this, then you can just simply use a comma character, and then you can specify your list, whatever it ISS. But now, in this case, we're trying to include another being, but here. We're doing the opposite. We're disqualifying that being from auto wiring. So in this case, this will take the priority. It's on the program and we should see the same result. We seem to have an exception list. Take a look at it and it say's Hold it. The problem is different, though. Here we're trying to include this being. That means we have excluded this being, sir way to make sure that we have this flag set in here as well and also went to have the following check here. Oops and hopes this should work as well. This should work now let's on the program and and obviously won't see any messages because essentially, these two beans are excluded. Now, if you're on the program, I should see a message from another bean. And sure enough, it did. Let's quickly also take care off the Java conflict and on the program it worked, and here have just used the following attributes for being annotation in our exclude that bean simple as that. So that's about it. I'll see, assume 39. 1105CircularDependency: in this video, we're going to talk about cycling dependency, and then we're going to take a look at a couple of examples off. Same first of all, what is cycling dependency? Well, you might have guessed it already is just simply means a couple of beans dependent on each other. Let's say that we have bean A and being be then a is dependent on B and B is dependent on a that's called cycling dependency. Now let us take a look at an example. Ofs. I click dependency in case off center injection. So what I have here is couple of beans and the boat or dependent on each other, and I have introduced the settle methods accordingly. And since I'm using the auto wide annotation, I don't have to do much in our conflict file. But of course, we need to define the beans. Now. If I run the program, are we going to see an exception? Or will the program run successfully? What's your guess? Well, there won't be any kind of a problem because the beans are constructed without any problem , and after that, once all the beans already there ready to be injected and spring will not have any problem injecting the beans, even though they're dependent on each other. Let's on the program, and you would see the result. It worked, so we were able to call the sample methods off each bean. It's as simple as that, and these methods are just calling the method off the dependent bean. Now, if I try to accomplish the same using constructor injection, let's see what's going to happen. I'm going to change this. Do constructor injection quickly, so we now will have the constructor with Para meter. Same stuff should go in here as well. Annotate the constructor with hardwired, make it caromed, rised and then said the property value that's on the program and see the result. And we'll see an exception hopes. Let me fix the Java conflict first, we'll fix it later. For now, let's just commended out and sure enough, we have an exception. So what has caused the problem? Can you make a guess? Well, if you think a bit, it's quite obvious that spring cannot inject the dependencies because Spring will try to construct this object when it is inside the constructor. And there it sees that this being is dependent on another bean. So in order to inject another being into sample being, it must first create the instance off another bean. So it will try to call the constructor off another bean. And here it comes across the statement and notices that this another being is dependent on sample being again. It will try to call the constructor off the sample being, and it goes on and on forever. It will never end. So it's not possible to even create the instances, let alone injecting the dependencies. So in the scarce, you must go with center injection and not prefer the constructor injection. Let me just don't do those changes. All right? I'll see you soon. 40. 1106ProsAndCons: Okay, let us conclude on the chapter off water wiring. And in this video, we're going to take a look at some of the pros and cons off using auto wiring. First, let's talk about some of the reasons why you might want to use Auto Whiting. The first advantage is pretty evident is that you're going to save a lot of typing. And so the development efforts in our conflict file we're not taking any time to add any extra constructor. Arguments are properties. It would all be taken care by spring itself, and that would save some development effort. And the second point in here is somewhat call. Relatable with the same is that you don't have to add a new property when you're using the auto wide annotation. So in future, if you're going to add additional dependencies in your being or additional feels, that should bean depends on. You don't have to explicitly mention them as constructor arguments our properties in your conflict file. It would all be taken care by spring framework. Now let us take a look at some of the reasons why you don't want to use auto wiring, and here are day First of all, if you want to inject a the primitive types or string type then you cannot use are providing. You have to explicitly perform the dependence injection in this case, and sometimes this might be a problem, because when you have a large project, you should ever go without providing or explicit configuration. But if you use mix of both, that's going to create some confusion and thereby Emma Hasam complications down the line. And another reason is explicit. Dependence is will. All right, auto wiring. Let's that you have a new developer who just joined your team, and he's not quite ever offspring framework. So based on his past experience, he used the new key word to declare the dependency that the being depends on. Well, this might be a problem, especially when you're trying to debunk for an issue. Because when you look at the conflict file, you notice that the dependency is performed pretty well. But you don't realize the fact that somebody else has messed up with your code by introducing a new key word, and not to mention that when you're using a spring framework, the last thing that you want to do is to use a new keyword in your code on top off it. If you're using or providing, it might be even harder for you to debunk the issue and that this might be a problem in some cases. And on the last point in here, there are tools out there which would generate the documentation By scanning your project. Those tools would be able to look at the conflict files and create documentation as toe, which being is dependent on which being. But if you're using or providing, these tools, cannot actually know what each being is dependent on, and so there won't be any documentation, so there would be a need for manual intervention to edit the documentation. And when you have manual intervention, that's going to result in some inaccurate documentation. And that's clearly a problem. So this is essence off what a wiring. But like I had all the dimension before the real power off water wiring will come into picture when you're developing Web applications. Justin Spring framework there you don't even have to define the beans in the conflict file . All the beans can be scanned by spring itself, but for now it's clearly out of the scope of what we want to discuss, so I will see you soon 41. 1201DefaultPropertyEditors: in this chapter, we're going to talk about property editors, and in this weekend we're going to talk about default property editors. So let me just walk it through this code, and then we'll take a look at an example off. Same. So what we have here is a simple being that is just holding list. Off fields are properties, each off different type. So we have, indeed your double character string. And here we have something new currency and you are all and from, or conflict file. I'm just trying to perform the constructor injection in this manner. And as I've already mentioned, it's always a good practice to specify the type. And so I have done it in here. And moreover, it's better that you get more specific and even specify the package because the euro class may belong to multiple packages. So it's better that you be specific and specify the package as well. And now, if you run the program, all these arguments would be injected into the bean. But have you ever thought If you're using an XML convict file, you cannot really create an object that we can inject directly into the being like we normally do in case of Java con flick. If you're using XML conflict, all you can do is to specify the string literal, regardless off type. So even if it is something like currency are you are you still have to specify the string literal. But how is spring managing to convert the string into the destination object? Well behind the scenes, there is a logic for each and every field that will actually take the string as an argument . And then the logic offered would convert that string into the destination object to be more specific for each and every type. In here, there is a class file associate ID, and Spring is actually going to use that logic to convert the string literal into the destination. Object by default. Spring comes with many such classes to support popular data types. But if you won't spring to convert a string literal from the XML document to your own custom object and there's a different approach for that you have to create your own code or deacon water code, which would convert the string value into your custom object. But anyway, we're going to talk about it. Incoming videos But in this video, I just want to give you a glimpse off how the commotion process will take place. So, for example, here we provided the currency and we provided the value. And behind the scenes Spring is actually going to make use off this class currency editor, which is the converter class for currency object. And typically all these converter classes would end with the name editor just as you see in here. And they all extent the property editor support class. And once you do so, you can all right, the falling methods set as text, which is a method that will take the string as an argument. And this is a string that super wide in your XML file and inside this method, but will provide some kind of a logic that would ultimately convert that text into the currency object. And then we'll pass that result as an argument to this set value mattered off the property and it or support and with the getters text mattered. We want to do the opposite. We want to come with the currency object back to string. However, in our case, this method may not be required and this method may be useful in the context off of applications where the trained request would have a string that needs to be converted into an object by calling the set US text method. And then, when you're sending back the response, spring will make use off, get as text to convert that object back to the string so that it can be presented on the client side in some meaningful manner. Likewise, we got many other editor classes or can motor classes, and each would correspond to a specific type. And here are the list off classes The editors that are that will come with spring Library by default and even for the objects like indigent, double byte character etcetera like we have in here. They all must be having an editor class corresponding to them to let spring use them. And this is a class that will serve that purpose Custom number editor and is basically a property editor for any number class, such a short in digital along big indigent float, double and big decimal etcetera. So So this is a quick glimpse off what our property editors and we will explore more incoming videos. But before we end this video, let us run the program, which is pretty simple. I'm just trying to create the context and then get the bean trying to print the object. And the toasting matter is having did logic to display that object in some readable manner . And here we have the result. All right, I'll see you soon. 42. 1202CustomEditors: okay. In our previous video, we have seen how spring will make use off some readily available editors to come with the string to a particular data type in the Sweden. We're going to explore how we can let spring convert the string to a custom type, for example, letter that I won't spring to convert the following string literal into my own custom object that belonged to server conflict. How can we accomplish that? Spring does not have any readily available editor class to perform this conversion task, so we have to introduce one for that purpose. But before we take a look at the code, let me just walk you through the other files. It's pretty simple. All we have here is the example being and is dependent on the server conflict. And so we're trying to inject Sovecon figures in the constructor injection. It's as simple as that, and from our conflict file, I'm trying to inject with the constructor argument. No, let's take a look at this cord like I had all the dimension you need to follow the following naming convention the destination type class name. In our case, this is going to be the civil conflict. It has to be exact same class name and you also need to upend the following world editor. Only then will things work. So this is the format that we need to follow to name your editor class. And once you do so you have to extend that class with property editor support class and then you'll have the option toe all right. They set as text mattered, which will do the magic so spring will take her off. Passing the string literal. Let you configure in your XML as an argument to this method. And here you would just simply introduce a piece off code that will ultimately convert the string into your destination type. In our case, it's going to be off type the server conflict. So I'm just trying to split the string by this hyphen character. And then I'm trying to set the values off Silver conflict, which is a simple, poor Joe class. And then finally, I'm trying to send the resulting object to this method set value that belongs to the property editor support class. And this is exactly what would get injected ultimately and like I had already mentioned this method in here is not off significance at the moment. This would be useful in the context off of applications. So we will ignore it for now, and that's all there is to it. Let's try to run the program and see the result. And it worked pretty well without any problem. Now they might be instances where this class may not be reciting in the class part. So spring does not have any visual to this class. Or maybe you want to use a different name, maybe with the name editor X or whatever, Then spring will not be able to identify. Since you now broke the rule off the class naming convention. In this case, your program will not run and spring will throw an exception. Okay, We need to first make sure that they hopes, let me name the file to same. And now, if you're on the program, it still seem to have an error. Not really. Okay, if you run the program now, things are going to break. And sure enough, we have an exception. So in our next week, we're going to take a look at the solution to this problem. I'll see you soon 43. 1203EditorsWithOwnName: If you don't follow proper naming conventions for your editor classes, then spring will not be able to recognize them. But that doesn't mean that you don't have freedom to give your own name. You can give whatever the name off your choice as long as you let spring know about the change. So how do we express this to spring about the change in name Wellspring providers a special class specifically for this purpose. And that class is the custom editor configure. So this is the class that will help us create the link between the server conflict and its editor. And in order to do that, you have to populate a field that belonged to the customer editor configure. And that field is the custom editors, which is basically going to accept a map. So we have to inject the map to this class, and in this case, we're making use off the center injection to inject the map into this property. Sassen, Blauser and the map would have key value pairs basically an entry, and each entry would have information about the destination object and its corresponding editor, Tassan Blezard. So in here as part off key This is our destination, object class and then have also provided the value that basically points to the editor class. And that's all there is to it. Things will work pretty smooth without any problem, no matter what. Name the trig you to your editor class. Now this has worked. But we have one problem with this confident class. Or more specifically, with discussed, um editors Property. The problem is, if you have a parametric constructor in your editor class, then this technique may not work. For example, inside our civil Conflict editor I'm now going to introduce a para mi tries constructor. And in our example, let's say that I wanted to upend some kind off a secure string to the password, and that's what I'm trying to do in here. I want to be able to inject that secure string into the custom, complicate it, Rex, and then I'll be able to happen that string along with the password. So no, With the way I configured this editor in here, I cannot really pass an argument to the constructor, and this is actually going to throw an exception because we have to have a default constructor. And when you don't have any constructor like we have before. That means a default constructors already present and so wouldn't see any exceptions. So no. In order for this to work, we have to introduce a default constructor without any arguments. This will get our program working. But still, we won't be able to inject the argument. So in our next week, we're going to see how to solve this problem. I will see you soon. 44. 1204UsingRegistrar: So the basic problem with custom editors is that Spring will create an instance off this editor every time we need to come out the string to the server conflict. And in here Spring is actually taken care off instead initiating the editor, not we. And so spring does not know what needs to be injected into this constructor, So just simply does not expect a parametric constructor. All it needs is a default constructor to create an instance. But now our requirement is different. We want to be ableto inject a constructor perimeter as it is useful in our code. So this time we have to follow a different approach. Somehow we need to get control on the instance ation process off the editor bean and Sprinkle letters have that control with this interface. Property editor Registrar. So we're going to create a class. You can give any name off your choice, but it has to implement the property editor registrar and you have to provide implementation for this method. And inside this, you're just simply create an instance off the editor, bypassing the necessary constructor arguments. In this case, I want to pass the secure string which is useful in the editor bean. And along with that, I also specified the destination type, just simple as that. And ultimately, this, too, is to go as para meters toe this mattered register custom editor that belonged to the registry. So this registry object is kind of like a registry service where you can register all the custom editors, which is exactly what we're trying to do in here. An inner conflict file. We will get it off the customer. It is property. But to use the property editor registers property that belong to this class, and this is going to accept a list off beans. And these would point to the registrar class that we had just introduced. So ultimately, with this approach, we have control over what? The instant creation process off the editor Bean. And so we're able to pass the arguments to this constructor? No, that's on the program and say the result. Sure enough, it worked, and the password also includes That's secure string. All right, I'll see you soon 45. 1301Profiles: in this video. In the next couple of videos, we're going to talk about profiles and spring. We're going to take a look at a simple example off profiles, and then we'll talk about its benefits. But before we do so, think about the following scenario. Let's say that you wanted to test your spring application locally in your development and Roman and see that you're using a database software like my skill. But it may not be the case in case of production enrollment. I mean your customer, maybe running a different database altogether. Maybe your customer is using something like post their skill database software. Oh, he may be using Oracle DB. So depending on the enrollment, you need to switch to a different database. If your indelible mint and Roman then you want to be able to use my skill, or if your application is running on the production enrollment, then your application must switch to possibly ask your how can we accomplish that? Well, profiles is the answer for that. So let's take a look at the sample code and understand it better. For the sake of simplicity, we're not actually going to use any database software, but rather I'm trying to simulate the same behavior by using the interface data source. Data sources interface are settle specifications off J two ee. And this is the interface. All the database vendors were deals to provide the implementation so that everybody can use their court to connect to the database. For example, the providers off my skull database provide the implementation class off data source so that we can use their classes to be able to connect to my school database. Same is the case with all the vendors out there. So in here, what I've done is as Hume that I have implementation in here. That sort of letters connect to the my skill database. And similarly, we have another class that implements the data source and the implementation off. It would help us connect to the post great SQL database and from our application. Depending on the environment, we're going to connect to a drop this data basis. And when we do, we have to call this method, get connection to acquire the connection to that database. But in here we're just trying to simply display a message that we have indeed connected to that database. Same is the case in here as well. I've also created a couple off conflict files, and the key thing to note here is I have no introduced this attribute profile. So all the conflicts that I had mentioned in here would correspond to the development profile. So I want to act to it. All these conflicts if I'm working on the development and Roman or if I'm testing my application on the development and Roman and do take a note that here and I'm trying to inject the data source, I'm using the implementation off my skill database because that's what I want In my local enrollment, I want to be able to connect to the my skill database. Similarly, we have another conflict file for production, and so I have created the production profile just as you see in here, and all the conflict in here would be applicable for production and Roman. I also added one additional profile, which is default. That means if I don't explicitly set any enrollment than by default, this configuration will be considered. In order to make a profile as default, you have to use the exact same word or lower case. And in addition to that, when you have multiple convict files, it's better that you introduce an interface with set off standard methods so that but it's better that we have this as well. So that across all the Java convict files we would follow the same signatures without any problem. There won't be any kind off naming conflicts. For example, here I could name this ass data source X, y z And here I can say data source ABC that's going to mess up things in our actual application logic. So in order to keep things consistent, have actually created this interface so that methods will be consistent across this conflict files. I also have a DB manager class that is specific to my application and using the constructor injection, I'm asking spring toe, inject the data source. So depending on then, Roman time in spring will inject its corresponding data source and we'll see how it works in a in a minute. And from our main logic, since we now have multiple Java convict files, I cannot explicitly said the name in the constructor off this off this class. But what I have done, rather is I've used this method. Context stopped scan and they provide the package, and spring will scan all the convict files that are annotated with conflagration. After that, we have to make sure that we call this method refresh so that the context will have all this conflicts ready to be used. And right after that I'm trying to simply access this method connector DB, which will just simply call to get connection method off whatever the data source that is injected. And finally, I'm just closing on the context. But one thing that is not very usual is this particular statement, and here I'm trying to choose the profile on my own. So this time I said the profile to development so spring will take care off, activating the Doman profile so all these beans would come into picture. Are all these conflicts will come into picture? No, that's on the program. And make sure we should be able to connect to the my skill server, and it worked. And if a switch to the production and Roman this time, it's going to be posed serious skill, and if I don't provide any enrollment, then that's going to be the default one, which is the production in Roman settings, and there it is. But right now we have one serious problem. Can you guess what it is? Pause the video, try to find the issue and then get back. So the problem we have here is we're tryingto heart called the name of the profile as part of the source code. So what is a problem with it? Okay, let's say that I change us too. Development, for instance, from a local testing. Now accidentally, I have committed this code into the base. So in production are application is going to crash because spring will try to choose the development profile. And things won't work there because there is no my skill database installed. So in our next week, we're going to see how we can solve this problem. All right, I'll see. Assume 46. 1302JVMParam: So the solution to our problem is we're going to set the JV Ampara mater. Now they're multiple ways you can set the JV and perimeter. You can set it at operating system level or we can set it at server level, for example for using Tomcat. Then you can set the pyramid dress part off Catalina Dark Properties. But what I'm going to show now is how we can set it in Eclipse and Roman. So when you hope for your mouth over the play button, whatever the name that you see willing to send the William Argument, you click on this down at a button and then go to run conflagrations, choose your application, click on arguments and here you're going to set that perimeter. It's going to be spring dot profiles dot act to, and you would point to your favorite profile. Now, in case off production enrollment, your customer must be setting the same property. But in his case, this would be with the name production. So now let's run the program and make sure we use my SQL. And it worked if retained this to production. Okay, actually, we have to get it off this from here because when you explicitly specify the profile in your source code, this would take the priority, for example, for under program again with a different profile. I'm going to make this to production this time, so it's still my skill database. So let me just get it off this from our source code as it's bad practice now run the program. This time it should be was great. Ask your and it works. So now this is definitely better because you're not hard, according the name of the profile in your source code. But the profile would, which was in depending on the enrollment on which your application runs. That's about it. How's he assumed? 47. 1303SingleConfigFile: when you have multiple conflict files, then there is a possibility off duplicate code. For example, in our conflict files, the only place where profile should come into picture is for this particular bean. The rest of the court would remain common in both this conflict files. Fortunately, we will be able to solve this problem by using the profile annotation at matter level so that we don't have to create multiple conflict files. We can instead use just one. And here's a file for that. So I have no copied both these methods from both these files to one common conflict file and have provided this annotation profile for both these methods. Hold it. Here we have it. So now all the court would remain common no matter which enrollment on what you run the application. But the only thing that would differ are the beans that are annotated with profile. If you're on Devon Rome and then this being would be activated or if you're on production, then this would be activated. But do take a note. Since we have these two matters in the same conflict file, we cannot have the freedom to use the same name as part of the method name because that's how Java is designed. You can't have two methods with the same name, but we can give the same name to these beans. So hey, we named this as data source. Same thing went in here and in our cord. We don't have to bother about the name because it is going to be consistent. And from our main logic, I don't have to now scan the package. Instead, I can just errantly specify the name of the file, just as we did before in our earlier examples. So this is going to be Java conflict art class now, depending on the profile, this is development. In this case, this should be my ask your and if you change the profile of production hard, if you don't provide anything, that would be production. And there you haven't had worked so profess can be applicable at method level as well, so that you can now have only one single Java conflict that takes care of everything. But it might bring some additional complexity in your file. And you also need to take care off these naming conventions. And if you take care off all that, then it's fine to go with this approach. All right, I'll see you soon 48. 1304ProfilesInXML: in this way, they were going to take a look at how we can configure profiles in case off XML conflict. So just as we had a couple of Java conflict files for each profile, we now have a couple off maximal files corresponding toe each profile. And the way you specify the profile is by using this attribute profile with its name inside the bean stank. So whatever the beans that you define inside the bean stag would be qualified for that particular profile Assassin Plaza. Similarly, we have production profile specified in here and from our main class. All I'm trying to do, he is instead of performing the scan off a package to find the conflict files in case off XML, I have to know load them. And here I'm trying to use a regular expression. So this instruction will actually choose all the convict files that start with this name, followed by any sequence of characters and with dot xml extension Jasim al Assad for on the program. Depending on the enrollment, you would see the result task simple as that. And just as with Java con Vic, you can just simply use one single XML file. And for that we have to get it off these instructions and space for your conflict file right here. It's as simple as that. And here's the file. So this time we have multiple profiles in the same conflict file, and for this you're going to be using the nested bean stag. So here it is for the development profile, and here he would mention all the list of beans that you want them to be qualified for development profile. Similarly, we have another one for production, just simple as that. It's on the program and see the result. All right, I'll see you soon. 49. 1401FactoryMethods: in this week, we're going to talk about factory methods when you haven't been definition in this manner in your conflict file, you're asking spring to create an instance off this being, but sometimes you might want to get control on the instance ation process. Maybe you want to create an instance depending on certain conditions. In that case, you shouldn't ask spring to create an instance for you, but rather you should our spring to call him method, where you would provide a piece off logic that would create the instance depending on your own conditions. In order to tell spring to not to create an instance but rather call a matter off for choice. You have to use the following attribute factory method, and you would give the name of that method which spring would call, and this method would belong to the being itself and inside are being. I'm trying to demonstrate a real time scenario. Say that I wanted to create an instance off Silver Manager, depending on the FBI key, for example, say that we have a customer who purchased the license to get access to the servers. In his case, we want to create the server manager in stands as the tears. Otherwise, if the user is a free user, Orphee entered an invalid AP a key are the A P I key that is no longer valid. Then we want to set this flag restricted access to true. And throughout the logic in this class file, we would make use off this flag, and we will give restrict access to that user accordingly. So here we are, checking to see if the A P I key is valid and this matter is just straight of a returning. True. But you need is in that we have some kind of logic that will actually check the validity of the FBI key. If that's the case, we're strike ever returning the object as it is without enabling the Frank. Otherwise, if the A p I key is not valid, then we would said this flag to true. Let's default this to fault and we also have a method display message. So depending on the flag that is set, we're going to display its corresponding message. But currently the a P I key is valid, and so we should be able to see this message so Let's go ahead and on the program and see the result It worked. The user would be qualified for unlimited access. No. Let's say that the user has entered either an invalid AP a key or his A P I. K is expired. Then this should written falls. So now he should be getting restricted access to the servers, and so is the message. Let's try to do the same from Java conflict, but before that, I'd like to point out that dismantled in here has to be static. The reason is, if Spring has to call that method, it needs to have an instance off it. But since it cannot create an instance and then call this method, it would expect the matter to be off static so that it can call the method by using the class name just a quick point to note. Also, do take a note that we have constructor arguments in here, but this is not actually passed to a constructor, but rather to this mattered. So if you're using it, simple conflict, this might be a little confusing, but that's how it works. So the argument that we had passed would end up in here. We don't have a Parametric constructor. Okay, now let's take a look at the Java con. Think very straightforward. Nothing surprising you just simply trying to call that static method isn't the class name and you're passing some argument. It's as simple as that, and everything would work, smoke electron the program and see the result. It worked. But of course, with factory methods, you have certain limitations. For example, the method has to be static, and moreover, this class is not meant to create objects. It would have its own responsibility to handle the business logic. So in my next week we're going to talk about so called a factory class, which especially meant to manufacture these objects. And we don't have to use any static method. I'll see you in my next radio. 50. 1402FactoryBean: in this video, we're going to talk about factory beans, so I have no introduced A new class file which is responsible for generating the arm dips off Silver Manager. And that's why it's called as Silver manager Factory Bean. And in order to create this class, it must implement the factory being interface, which comes with a couple off methods. One is the get object type, which was just written the type off object and the other is get object. So here you would provide a piece off code that will ultimately return the desired object. In this case, depending on the validity off the FBI key, we're going to return the Silver Manager object. And inside this method, he's a P I key valid having to do some logic in here. Of course, it doesn't make sense to hard core values like this, but imagine that we have some cord to validate the FBI key. So in my case, if the appear key is 123 then I'm assuming that the key is valid. Otherwise it's not, in which case with little faults and while creating this object, were also passing an argument to its constructor. So here we're setting this flag restricted access accordingly. Some of the features would be enabled, and some of the features would be disabled. So the user who has purchased the license will have an exclusive access or unlimited access . Otherwise, I usable have very restricted access and from, or conflict file. I'm pointing to the server manager factor being class, and then I'm passing this argument to its constructor in this case and passing 321 So that means R A. P I key should be invalid. And it is the constructor that takes the perimeter. It's as simple as that. Let's on the program and they use a must see restrictive axis. Looks like we have a natural here. Let's take a look at what it is. Not sure Why is that happening? It's on the program and see if things will work and it walked. That's trained the FBI key toe 123 and the user must have unlimited access. Sure enough, it worked from the Java conflict file. Things are again pretty straightforward. You just try to create an instance or factory object, bypassing some value to its constructor, and then you just simply return an object off the server manager by calling the get object method off factory being as simple as that pretty straightforward that switch to Java Kontic and run our program and it walked as well. But the key thing to note here is we no longer have the instance Station called in here that has been moved to a new class, which is solely responsible for creating the instance off the server manager and Spring won't create the server manager object, but it is going to request this factor being to create an instance off several manager by calling to get object method. And that's why you have tow implement the factor being only Denville things work. And moreover, these methods are not static meant. That's anymore because now we're able to create an instance off factor being and call its methods. All right, that's about it. 51. 1501AOPIntroCrossCuttingConcerns: in this chapter, we're going to talk about aspect oriented programming, which is one of the key offerings offspring framework for developing Enterprise Java applications. But before we try to understand what is an aspect wanted programming and its benefit went to be ever off the term crosscutting concerns. In this video, we're going to understand what it is and the problem it creates in the application development process. Imagine that you're working on a banking application, and when I say banking application, it's going to be quite complex because it would typically constitute a host off features and all of the operations needs to be performed insecure manner. Now what I'm trying to do here is I'm trying to simulate a banking application so typical of we may have a file like Great Guard Service, and it would have set off methods that deal with credit card. Maybe we wanted to issue a credit card to a customer, so we will call this method and we'll have some kind of a business logic that will issue a credit card to a customer. Similarly, we may want to block a credit card if the customer had reported some kind of suspicious activity with his account, and we may also have a matter like paid you to let the customer pay the due amount. And similarly, we can have many other services like, for example, we may have a loan service, and it will have set off methods that deal with the loan. We mail tohave an administration service, and this is not meant for the end user, but rather for the system admin. And he will have certain privileges that a normal user doesn't. For example, he can actually add the users or delayed the customer in case the customer is no longer associated with the bank and all such kind of stuff. Now, since the application in here is pretty simple, we can actually get away with just a business logic like we're having in every method in here. I'm not bothered about anything else. For example, I'm not having any kind often exception handling in here because our applications, but a simple But if you're talking about the real world banking application, you would likely have thousands off files. In that case, there lot of other things that you to consider not just the business logic in other words. As a developer, you should not only focus on the core functionality are the business logic, but also you need to be worried about other concerns. Let's talk about this method in here, for instance, paid you here. We need to perform such an operation as part off a transaction to ensure the consistency off the data. So wait to basically begin the transaction, perform whatever we're doing and then come in the transaction. But what if there is some kind off an exception in our code? We need to handle that. And, in which case, we want to roll back the transaction and put back the database into its earlier state. And we don't want to perform this action off paid do unless the user is authenticated. That means only when the user logs into his account can he perform this operation. So we need to have some kind of chicken here to check to see if the user has indeed logged in. Similarly, we need to have another check to check to see if the user has sufficient privileges to perform the following action. For example, not everyone can perform the administration tasks. Only admin will have the privileges to perform whatever is in here. So we need to have a check for authorization to see if the user is authorized to perform the capacity in dental. And in addition to that, for each and every mattered, we need to incorporate some kind of a logging mechanism. So I went to put a log message at the start off every method and at the end, off every mattered, and not to mention the significance off putting a log message that would help debunk the issues. For example, if you have some kind of an exception in here as a developer, are, as a support execute you, you would go to your lock file and you would try to see a log message that came before that exception. And if the exception is in this method, then you'll be able to locate the following log message, thereby. You will get to know that the exception is in the page you method and in the credit cards of this class. Like all these, we may need to introduce code that is not bad, likely related to the core functionality off our business. Now, if you notice all this extra piece off court is not directly related to the core functionality that we wanted to deal. Our core functionality is mixed with all these other concerns that has nothing to do with the core functionality. Now imagine that you have to write similar code across all the methods in your application . I hope you're getting the problem. The problem is it is going to dilute the original purpose off the services when you have a credit card service class, it must only have called that deal with great count. But that's not the case here. We're also introducing some of the concerns, so definitely this is a problem that we need to address. If I want to draw a diagram to represent the current scenario, then would have all these services. The credit card services comes on with great God loan services concerned with loan. But unfortunately, as a developer, we also need to be worried about other concerns concerns that are sort off common in all the components. This is exactly what our crosscutting concerns. All the lines that you're seeing in red are crosscutting concerns, and with a puppy or aspecto in the programming will try to get it off all these crosscutting concerns from our code and let the dollar per focus only on the core business logic and not worried about all these concerts. By that I don't mean to say that will get rid off all these concerns. The idea is to keep all the concerns separate from each other and we will be able to achieve it with a will be, well, come to new from next video. I'll see you soon. 52. 1502AOPTheory: Okay, let us take a look at a simple example off Able P. And for this purpose, I'm actually going to be using the coping mechanism provided by Spring Framework. But down the line, we may actually be required to use some external, ever be projects like Aspect J to address our needs. But for now, this also fines for our example. So clearly we have a problem in the scored. Along with the business logic, we're also dealing with one another aspect which is logging so in each and every mattered. We have a log message at the start and the end of the method. And with every Opie, we're going to get it off this from this file and let the developer focus only on the court functionality or the business logic. And in order to keep things simple in our example, we're going to focus only on the loan service will get it off the other services for now. And before we dive in with the practical example, let us have a theoretical walk through as to what we're going to do. So currently we have the loan service, and it is also dealing with other aspects like logging, and that's what these red boxes would represent. But now we're actually going to create another class that I specifically meant for the logging aspect, and we're going to move out the court off logging from the loan service. So this new class, so far we're able to separate these two concerns. But it is certainly not enough. We need to find a way to combine these two on the fly so that it would have. The similar behaviour, as our current application is having, is to print the long messages before and after the matter execution. And in order to do that, we're going to take help from spring to create so called a proxy object. A proxy object is essentially the combination of these two objects that get created on the fly when the container loads. What this essentially means is spring is actually going to use the Java's dynamic proxy mechanism to create a class file on the fly, and that class file extends the target class, which in this case is the loan service, and that class would have implementation in such way that it combines both the business logic and the aspect so whenever you make a call to one of the matters in the loan service , the call won't directly land on the loan service object itself, but rather on the proxy object. The proxy object will have the internal logic to call the methods off the loan service. Well, I understand the fact that this might sound confusing a bit once we take a look at a practical example off. Same, you will understand better. I'll see you in my next video. 53. 1503SpringAOPExample: Okay. Now let us take a look at how we can implement a war. P programmatically First things first. Let us give the dollar per some piece off mind by removing all these crosscutting concerns from his code so that he can focus on the business logic. But then we have to take care off. Moving all those aspect are not case the logging aspect into a different file for that purpose. I've actually created another file with the following name. You can give any name off your choice, but we can't just copy the log. Best ages, as is to this file. It just doesn't work in that manner. There certain procedure that may to follow to tell spring that we wanted to display log messages before an after each one of these methods are called. And the way we convey spring is by using the interfaces that spring is ever off. And depending on your needs, we had to choose one or more of those interfaces. You know, case I wanted to displace some messages before each one of these methods get executed. And after each one of these methods will get executed. And the inter offenses that I need are these method before advice and after returning advice that will also provide the implementation methods. And here are day, the before method would belong to the method before advise and after turning would belong to the after returning add wise. This method would be called by spring before each one of these methods will get executed and this method will be called after each one of these methods will get executed. Well, as off now, we didn't create any kind of relation between these two entities were going to be doing it without conflict while in a while. But let us try to understand discord in here. So what I'm trying to do in these methods is I'm trying to print a message. But the message is not going to be same every time. The message would differ depending on the class and the method that is being called. For example, if you want to print a log message for this method issue alone that belonged to loan service, then we should be able to see a log message with the loan service class name and the mattered issue alone for this very purpose. Whenever spring caused this method Spring would also send the falling arguments. The method object would represent the mattered that is being executed. The target would correspond to the target object. In our case, it's going to be the loans of this class. And using that object, I'm ableto display the class name and using the method object I'm able to display the method name. In addition to that, if you'd like to get access to the arguments that are passed to these methods, you can always access them with this. Eddie, if they're 10 arguments, you can access his specific argument within Head Index Assassin Plaza. And we have pretty much the same stuff going in here. Except this is exit because this will get executed after these methods will get executed. Now, the next thing that we need to do is to tell spring to create a proxy object by combining these two objects and for that were to first define couple of beans and each would correspond to each one of these classes, and so have them in here. Just as you see, we have one for loan service and the other to handle the aspect. And in addition to that I'm going to define another bean, which is going to be the proxy object off loan service. And here it is. You have to define the bean proxy factor being, and this would accept couple off properties using cetera Injection. The first property is the target class itself, so we should be pointing to the target class. In our case, it's going to be the loan service. And so sending is specified in here, and the second property would point to thes aspect. In our case, we just have one aspect, and that is this. And for that, we're going to populate the following property, which is a list, and as an element in that list, we're going to be including the following being similarly, if you have another being or another aspect, then you can just define that being in here and then added as a value to the list off interceptors. For now, to keep things simple, we just have one single interceptor, but you can have as many as you wish. For example, that's that you wanted to create another class like this, and that's going to deal with transaction so you would basically have same methods, but maybe wanted to begin the transaction the before method and commit the transaction in the after method. Something of that sort and also defined the proxy bean in this manner. You're done. That's all there is to it. But from our main class, we're not actually going to ask for the loan service being, but rather we're going to ask for the proxy bean off lawn service. And here is a code that would illustrate the same. I'm no trying to get access to the proxy bean, our proxy instance, which is going to be off type loan service. And this type condition is possible because, as at all the dimension, the proxy object is going to extend the behavior off the target object. So in our case, the loans office proxy is the extended version or the child object of the loan service. And once we get hold off that particular instance, we can call it smelt it. So we're able to now call the methods that are part off the loan service. But behind the scenes, the call wound directly land on these methods. It's going to land on the proxy object, which will have certain logic that will ultimately be here. Has the log messages are right here. So, for instance, the matter would look something like this. Let me just, uh, copy this instruction before method and then parent object dot issue alone. And then finally, it would call the after returning method something off this sort. I hope you're getting what I mean to say, Let's run the program and see the result. And it worked. Loan is shoud for the following customer, and we've also called the clear Jew method. But before each method, we have their corresponding log messages. So it worked, and from Java convict file, you would pretty much do the same stuff that you're doing in here. We have bought those beans to find, and here is a proxy object as well. I'm trying to add the interceptors as part off the string, Gary. And then I'm trying to pass this as an argument to this method. Similarly, we had also set the target object. This would point to the loan service, obviously, and finally I'm trying to return The proxy object that has tried to run the program is in the Java conflict, and this should work as well. I also know that when you're working with, they will be. It's better that you're aware of certain terminology is involved. We have a video to talk about all those terminologies. But just for the time being, this class in here is called an aspect. Because logging is an aspect, often application, a transaction is an aspect. Often, application security is an aspect, often application. That's where they're called aspect. And we may have a file associate ID with each aspect. And all these methods in here are cold. Had wise is this is an ed wise and this is an ed wise, which means and advice would define what needs to be done and when it needs to be done. In this case, this is what we wanted to do, and this must happen before each matter is executed so and advises talking about what needs to be done and when it needs to be done. But these advisers are meaningless without so called a joint point. A joint point is a location where these advisors can be applied. In our case, all these methods in here, our joint points. It's as simple as that and the method off. Applying the advisers on the joint point is called Leaving. Well, all this time knowledge is may sound a little strange, but we're going to get used to all these terminologies as we progress through the course. But the key thing to note here is we got it off the crosscutting concerns from our cord. That's what is the purpose off. It will be all right. I'll see in my next video. 54. 1601AspectJvsSpringAOP: in this week. We're going to quickly understand what is Aspect J. And we'll understand. Why is it the better option compared to spring Able P Aspect J is an MLP extension created by Park, which is a company, And the main motive is to promote the modular ization off crosscutting concerns such as logging, exception handling security, etcetera. What this means is Aspect JSA Project specifically tuned to address the problem with the crosscutting concerns. And in fact, Spring has inherited a lot of features from Aspect your project to create their own a pope implementation. So since you're comfortable with spring Evo P by now, you should be able to easily understand aspect J as well. Let's take a look at some of the pros and cons off both in spring. The envelop the reading process happens during the run time. That means the process of applying the aspect on the target object happens during the run time, as I had already mentioned and for that very reason it might cost some applications performance. This might be a downside combat toe aspect J and as I had mentioned before spring users proxy Best Able P it's going to create a proxy object isn't the dynamic proxy mechanism and as a downside off that approach were restricted to only matter. Execution joint points. For example, if you'd like to apply aspect during constructor execution, that cannot be accomplished with spring Able P. And that's exactly where aspect would shine in case off aspect J. The leaving process happens during the compile time and less during the run time, and obviously that would have less impact on the applications performance aspect developed . So give us more fine grain control on the joint point like constructor executions or feel change events, etcetera. And moreover, aspect it promotes and petition driven configuration. And we know the benefits off using annotations. It's going to decrease the complexity off the code and increase the readability off the court. And this is something that you cannot do well with. Spring able P That being said, sometimes it might be a better option to go with spring elope instead off aspect J, especially if you're not looking for the features that aspect J provides, then you could just simply go with the springs. Aerial P. In fact, aspect is an open source project. Spring developers can easily make the aspect the library as part of the spring framework, but they didn't do so for a reason. There are a lot of products out there which are still using the springs Able p and not needed a project like Asper J. But since majority of the projects uses Aspect J, we're going to be focusing on Aspect J for the rest off our course, I will see assume. 56. 1603AOPWithAspecJ: Okay, let us take a look at an example off Aspect J. Since spring doesn't come along with aspect here by default, we have to explicitly download those libraries. And what May even our job is pretty easy. All you have to do is to add the aspect, Jess Dependencies in this manner, in our case, we would be requiring the falling tow dependencies. The 1st 1 is the aspect, Jeff Beaver, which is the aspect jit compiler that I'm talking about. So we're going to let our source files go through this compiler in order to get divide code that will have all the aspect supplied. And the other is the aspect. Jess run time library. Together, these two dependence is we'll fulfill the need off aspect j. And once you include these dependence is do make sure that you clean the project by going to project, and then click on clean, choose the project, and then again, click on clean. This will download all the aspect your libraries from the maven depository to your local depository so that you can start using those libraries. And our next step is to move out all the crosscutting concerns from our loan service to a different file, so we don't have all those log messages in here. I'm going to move them to a different class or a different aspect. So in this file, we're going to define what needs to be done when it needs to be done and where it needs to be done. So first of all, let us specify what needs to be done. In our case, we want to print the log message before enough to reach in every mattered in this loan service. So we will specified those two methods. And here are day, and it's just not enough that you add these methods in this manner. You need to tell spring which mattered needs to be executed before and which matter needs to be executed after. So now we're going to specify when the aspects needs to be executed by using the annotations before and after. So we wanted to print this log message before execution off these methods and this log message after execution off these methods. But our task is not complete yet. We also need to specify where these aspects used to be applied. We can't just apply these aspect everywhere for all matter executions went to specify so called appoint cut. Now this is a term used enable p again, we're going to talk about the term knowledge is in a different video. But what we're going to do now in order to specify where this aspect needs to be applied, we're going to use so called a point that expression language. Now you don't have to think of it as a different language that went to learn the syntax off . That language is pretty much equal into a regular expression. Basically, the idea is to narrow down all the joint points. If remember, what is joined points, it's the places where we can apply the aspect. So with a point could expression language, you will specify. Butcher all joined points. We won't execute these aspect on, and this is how you would specify it. And it needs to be mentioned right after these annotations in this manner. No, just by looking at this expression, you can say that we wanted to execute the following aspect. Are the method before execution tough? Any method inside the loan service? So that's what this point could expression, language, say's We're going to take a look at even more examples off the point that expression language and then you'll have a better picture so so far were able to tell what needs to be done when it needs to be done and where it needs to be done. There's one last night going to do now is to tell spring that this class is indeed an aspect and that you can specify with the following annotation, and only when you add the dependencies off the Asper J will you be able to use these annotations. So here are the classes that I'm talking about and do take a note that spring will also send an argument off type joint point with which we can access the class name or the method name in this manner. And there are set off at the methods, which you can use it, depending on your need. But this is a key object with which you pretty much can get control on the target class are the target object when you run the program, and that's all there is to it. We don't have to configure a proxy object or something of that sort. Inside our conflict files That's because everything is going in here. Unlike the previous example, an insider conflict file. All you have to specify are those two beans. But in addition to that, you have to explicitly enable the aspect j functionality because by default it is disabled by spring. And once you activate it with this tank, by the way, in order to use the stag unit to include the following schema locations as well as the following names space and again, you don't have to deal with all such mess When you're using a Java conflict, all you have to do is to add the following annotation to enable the Aspect J. Justin Blauser and then you just define the beans in this manner and from our main class, we don't have to fetch the proxy object. We can just statically fetch the loan service itself because when these files are compiled aspect Dick compiler will do the job for us. It will try to combine these two these two concerns and create one single class file. So the resulting byte code is equal into a class file with all these aspect supplied Assassin Plaza and manufacture the loans office. It's all that those aspect supplied and just off the court is pretty self explanatory. That's on the program and see the result. And it worked. Let's start executed. The program with the Java conflict was on the program and it worked as well. Now let us quickly try to revisit all the terminology is in world in naval P so that down the line it would be easier for you to understand. And it will become much easier for me to explain the GOP. So all these places where we can apply the aspect are called joint points. So this is a joint point. This is a joint point and in case off aspect J there many other joint points which can be utilized in case offspring envelop because of the method it follows to apply the aspect which is in the run time were restricted toe have only method executions as a joint points . But in case off aspect J since the aspect are applied, are the weaving operation happens during the compile time? We can now have various different joint points like matter execution, constructor execution During the time the object gets created, etcetera now moving on inside or aspect file your specifying what needs to be done. And this is called an ad wise in the terms off able p. Well, actually, the combination off what needs to be done and when it needs to be done together called and ed wise and the expression language which tells where these aspects needs to be applied is called a point cut. And all this together it's called an aspect and the process off. Applying these aspect our advisors on the joint point he is called a leading operation in case offspring envelop the weaving operation happens during the run time in case off aspect J the living operation happens during the compile time. Okay, that's all for now, I hope it makes sense. Will continue from next video. See you soon. 57. 1701Advices: in this video, we're gonna talk about various different advisors you can configure using aspect J. We've already taken Look at an example off before and after advisers, but there might be instances very might want to execute and advise after successfully returning from the advised. Mattered are after the advised method through an exception. So here are some of the annotations that would help us accomplish the same. So just try to pause the video for a while, take a look at these definitions and try to understand what each one of these annotations are doing. And then we'll take a look at an example programmatically. Okay, here we are again. For the sake of simplicity, I'm going to get it off one of the methods from here, and we would experiment with only one mattered. We've already talked about before and after. But one thing that I should mention is that when you have the following Ed Wise, after this will be executed, regardless of the result off, the advised mattered. So even if this method throws an exception, this would still get executed. You can think of this as it is in a finally block, and it will be executed regardless off the advice mattered, returned successfully or not, you know that illustrate that. Let me try to explicitly throw another point exception and let's start on the program and make sure that the ad wise would get executed. Okay, we got an exception, which is expected, but the key thing to note is the After Ed Wise was executed as well. If you won't execute the advice only after successfully returning from the adviser mattered , then you have to use after returning. So this is an imitation that would help us with the same now if you're on the program. Since there is an exception in the advised mattered, we should not be seen that log message and you don't see it. And in case if you would like to print the log message when there is an exception in the advised mattered, all you have to do is to create another advice, and this must be called and there is an after throwing is the imitation that went to use and off course. You can have the same at the name. Strain the name to something else and on the program. Since we have an exception here. We should be able to see this message, But But let's just change the message and headed books. So this is the entered log message, and then we're able to see the exceptional log message. It's as simple as odd. Okay, we have one more annotation to explore, and that is around. And that is essentially combination off all these three advisors. Let me show you what I mean. So I'm just going to simply copied this mattered and annotated tiles around, and that's all. It'll change the name a round head wise. What are the name that you wish to give? So here, this particular advises more applicable in case off transactions. So what I'd like to do is I would like to start the transaction before execution off the advised method bacon transaction and in kids off everyone method. We don't get the joint point as an object, but we get proceeding joined point. We'll see why this is useful in a while. So this will be executed before the execution off the advised mattered. And then after we execute this, we need to signify spring that we want to proceed with the advised matter execution So we're going to use this object to say, proceed with execution. And then after the advised matter gets executed, we wanted to perform something, and in case off a transaction, we want to commit the transaction. But there could be a case where the transaction might fail. In that case, the advised matter will throw an exception. So I went to wrap this code around the try catch block surround with dry catch block. And in here we're going to do something, maybe roll back the transaction, that sort of thing. So essentially with their own annotation, were able to accomplish all these three advisors in one goal. No strike to run the program. And first make sure. Okay, first, let's drive with the exception, So the transaction should be rolled back. And sure enough, we're not seeing the comet transaction and the transaction is rolled back because often exception. Let's try to get it off this and try it again. This time out transaction should be committed, and sure enough, it walked. Now, another thing to notice here is we're able to combine multiple aspect like logging transaction like was. It can include any number off aspects as you wish, but as a good practice, you should try to not mix all the aspect in a single file. It's better that you would separate these files as separate concerns. So majors quickly copied this file and create a double. Get off it and let's call it transaction aspect. Here it is. Let's get it off this method from here, said the file. And we're going to keep that mattered over here. It's as simple as that. Save the file and run the program. And okay, we're not seeing the transaction aspects, because can you get the problem? It's quite simple. The issue is. Wouldn't specify, are being inside our conflict files. Celeste quickly do that, and this is going to be the transaction aspect. It's as simple as that. Now let's try to run the program, and this time it will work. All right, that's about it. I'll see you soon 58. 1702OrderOfAspects: In most cases, you might want to get control on the order off execution off these aspect. For example, in our previous output, the logging aspect was taken care before the transaction. Biggane. Maybe I wanted to perform transaction first before ah, logging message. If you want to get comfortable on the order of these aspect, then you have to set the precedent. And the aspect with less presidents will have the higher priority to get executed. First, let's take a look at an example off scene. So my goal is to perform the transaction first before logging. So for that, I just have to introduce an annotation to set the president's. And the annotation is simply harder. And I would give some value. It doesn't matter what value that you given here, but make sure if you won't execute transaction first. The value off order is less than the logging aspect. So in here on it to set a value that is higher, then whatever I set in here. So now the transaction aspect will have the higher presidents, and so these will be executed first. Well, I don't mean to say that all these would be executed. Stride away. Well, we'll take a look at the opera, and then you will understand if you're under program now, you would notice that the transaction aspect executed first and then the logging and while exiting or off the mattered, it's the opposite. The logging aspect executed first, and then the transaction. This is kind off, like the left for approach last in first start. The last aspect that comes in is the 1st 1 to go out, and it actually makes sense to work in this manner. And in case if you have a couple off advisers reciting in the same aspect, for example, let me just quickly copied this mattered and create another mattered. Let's call it before too, and enter too. Then you don't have much off the control on the order off execution. The order is now undefined, and you don't have control on it. You can't have an annotation like order or something. It doesn't work here. In this case, you just have to simply copy whatever is reciting in here to the same method. And here you can have control on the order off execution. Let me get it off this. All right. That's about it. I'll see you soon 59. 1703Attributes: in this video, we're going to talk about attribute that could be part off the advisers and the type off attribute used is dependent on the type off advice that you're advising. Let me show you what I mean. For example, in gets off after returning advise you might want to get access to the value that gets returned by the method. In that case, it can make is off the falling attribute returning. Let me copy it and pistol here. And when you introduce an attribute in this manner, you also need to make this expression as part off a point cut attributes point cut equals. It's basically in the form off key value pairs. How this attribute And here we have it. And inside our method, all we have to do now is going to catch the return value. In our case, we're targeting this method and earlier this Waas returning nothing. But now I have changes to return Boolean value someone to catch the same And here and but this name no, guess what? We can get access to the return value and in our case, the value is going to be true. Let me also try to print it. So that's for sure that things are indeed working just like so. And that's older restaurant. Let's on the program and make sure things are working. Sure enough, it worked. Likewise, in case Off, after throwing you can get hold off, the exception object not gets thrown. And for that you have to use the following attributes in the similar fashion. Just as we used in here under simply going to copy it in here and try to read the the exception, This is going to be off time exception. Let's put it where Agenda Rick and Inside Accord. Let me just try toe display it. And also we need to make sure that there's some exception that gets thrown from here or make it back to a wide and when I make it to avoid and I need to make sure that and no longer expect any kind off return value here, save the file and let's throw some kind of an exception. Let's draw our in famous no point exception. Throw new no point or exception, and that's on the program. We should be able to display the exception, and sure enough, it worked. All right, that's about it. I will see you soon 60. 1801PointcutExpLang: Okay, We already know what is the point cut in this chapter. We're going to talk about so called a point. Could expression language the point Could expression language would help us target a specific set of joint points where we won't apply the aspect on. But before we dive in, let us try to understand what is a point cut signature. The point cut signature would help us express the point cut in more readable manner. Let's take a look at an example off point cuts it nature and then you'll understand better . So what I have here is the same example that we had looked at in our previous chapter, and this time let us make use off the point. Could expression inside the point get annotation as off Now, in our examples, we expressed our point. Could expression in the advise itself in this case as part of the before annotation, I'm going to copy this and make it part off the point Qattan notation as simple as that. But he can't just specify the point could annotation anywhere in the class. Former leaving would specify annotations a direct free level or it matter level so way to sort off introduce an empty method that does nothing. And the purpose of that method is just to hold this expression. So I'm just simply going to copy this from here. And although control shift all to import the point cut class so that we can use that annotation and once you do that, you can just call. This method has bottled the advice in this manner. Now this is as good as he specified the following expression right here. The advantage off it is the court will look more readable. Also, it would say some typing because next thing that you want to use the same expression, you don't have to remember and type the entire expression. Instead, you can just call this method just as we did in here and looks like we have the same expression going on in all these advises. So we can know just simply copy it all over and things will work as usual. And if you want to make changes to this expression, you can just change it over here and it would get reflected everywhere you're using it. So that's the beauty off the point cut signature that's on the program and make sure things are still working and it's working. Hold. It will continue from next video 61. 1802Designators: in this video, we're going to talk about designators that can be part off a point. Could expression does it make us will help us target a specific set off joint points. And here's the one that were already a bear off execution who target the joint points that are matter executions. And here's the syntax. For the same, you would use the key, what execution and then the written type of the methods that you want to target the target type or that last name dot the name of the method. And when you specify darts in this manner, that means we're targetting methods having any number of arguments. And here is an example expression that we've been using. So what this expression means is we want to target a joint point, which is a matter execution and those methods that target can have any written type, and those methods must belong to the loan service type. Don't the method can have any name and any number of arguments? That's what this expression means. Let's mourn with the dogs designator. So by using this designator, you will be able to target joint points whose arguments are either instances off given type are with the same name of the definition. Might look, come choosing. Let's take a look at an example of the same. So I go back to my example. Let's try to use the same expression here as part of the before advise. But no, that is also make use off this ox. Does it matter? Let me just simply copy it. Has these and use it in here. All right, here we have it. So what this expression means is I want to target method executions that belonged to the loan service, and the matter would have argument. Customer I d with the exact same name. So if you go back is the exact same name. What we have in here, you're going to use the same because now we can pass this as an argument to our red wise, And this is going to be off string type. And by doing so, you're getting access to whatever the argument that is sent to this method. Well, this might sound unusual, but Ringel take care off passing this argument when you have the following designator. Let me try to print it so that we're sure that things are indeed working, so I'm just simply going to print the customer variety in this manner. All right, that's on the program and make sure we're able to receive that argument. And sure enough, whatever we're sending in here has been sent to this method and so door advice in this man . Likewise, you can also specify list off arguments that you may be having in this map that if you have more than one argument, then you would just specified those names in here and you would catch them in the same order as part of the before method. Assassin Plaza similarly can also specify the type. For example, you can say that there's a string argument that the target method receives, and now you'll just simply get access to that perimeter. Are the argument Well, not all these expressions were experimented because some of these may not be applicable in the case. Feel free to experiment with all these designators, but I'm just going to walk you through as to what each one of these designators are meant for. So, Mike was he also got this hogs with the following symbol. Well, when you have this at sign then that will have a different meaning. In this case, we want to target joint points whose arguments are annotated with the given annotation type . An example. Expression can be seen in here. Sometimes we use annotations as part of the matter argument. In that case, you can target those methods by using this designator and the annotation class name. Well, this is very rare that you would need this anyway. Similarly got at a notation. This is somewhat similar to the at ARDS, but the difference is here. He won't specify the type, but you would specify the annotation itself and you would be able to target those methods. The designators, this and target are a little tricky, and it requires some understanding on the proxy objects and all. But to put it simple, when you have a class that uses an interface and you want to target all the classes that implement that interface, then you have to use the target designator. So here's expression for the same here. We're trying to target all the classes that implement this interface or, if you want to directly point to a class than you would use this key would and here is example of the same. Well, to be honest, we don't be. We won't be using majority off these designators and real world applications. Most of the time, we would only target matter execution as the joint points. But these are just for your information, just for the completeness off this course, and then we have at Target. And as you would guess, this has something to do with annotations. So this time we're trying to target the classes that used the following annotation type, and this is most often applicable in case offspring and Missy. And similarly, we have at annotation, and I believe we had all the record it. So this is duplicate. Let me just get it off this from here, okay? And then we have this Doesn't matter written. And this would help you target joint points that belonged to a particular package. Our type. An example. Expression can be seen in here. Something I want to talk. Get all the loans of his class, matt retributions and olders methods, or the classes must belong to this package. Arc dot company Jassim Plaza. Holy. That's about it on designators 62. 1901WhatAreEvents: in this chapter, we're going to talk about events in spring framework. But before that, let us right. Understand events in general. If you're ever off some of the front and technologies like JavaScript, angler jails or even some of the competent based rain looks like J. Assess, you're probably ever off events what we have here or set off event supported by JavaScript . But I just want to quickly leave a credit to this website w three schools dot com That's where we have these examples. In order to understand the mechanism off event, let's check one of these examples. So what this court is doing in here is we have an event which in this case, is the on change event. That means whenever would try to make it change to this field then and even will get propagated. And then we have an even publisher, which you don't see it in here because the publisher is going to be the browser. The browser is going to publish that event to an event listener. In this case, they even listener, is a function that we're calling, and that function will have some kind off a handling cord and in this gas. Whatever we enter in here will become voted to upper case. So if I type something in here and if I click elsewhere, they even will be published by the browser to the even handling gored or they haven't listener and the court will convert the characters in the upper case characters. So if I want to describe what's happening in here, we have an event which could be described as changing the state. Often object. That means, in this case, the object is nothing about this particular field. Then put Field, which is going to acts of the text and when we introduce and it changes toe the state off this object on change, then uneven to a doctor, which will then be handled by the even publisher, which in this case is the browser. And then David will be handled or published to all the listeners. In this case, we just have one listener, only one handling cord, and that is this and the act off generating the event. Publishing it to Dave and Listener is called eventing. But what this has to do with events and spring well, he went in spring is nothing different. It is exact same thing except the even thing will take place on the silver side and will have a different set off events. We'll take a look at an example pretty soon. 63. 1902ObserverDesignPattern: Okay, Here are some of the examples off event that can take place on the server side. For example, we can have transaction failed event in case of the transaction Failed as an example. Say that you have a banking application and your log in as a user to send funds to your friend. And if the transaction fails, that should trigger this transaction fail event and we should be having some kind of a handling code we're in will do something meaningful. Maybe you wanted to send out an email or an SMS to the user stating that the transaction has failed, etcetera. Similarly, we can have or we can create many such events. We can have transaction success event or user and rolled event once the user and Rose. Then maybe you wanted to send a welcome message as part of the handling code, and we can have user unsubscribed event, in which cases you want to send out an email asking for feedback or whatever. So there can be 100 different cases where we can have event. But the one that we're going to consider in our example is this transaction failed event. So if I want to write cord at the very top level. Roughly. This is how it would look. We'll have a bean, something like managed funds within which will have a method like San funds and then gives the transaction, is failed. Then we wanted to send an email. But how are we sending the email? It's like this. We're trying to create an instance of the email service object, and then we're calling its method send message. Well, this is bad practice because off one good reason. Both the beans are tightly coupled. But the real problem arises when you want to add even more. Listeners, for example, say that you also wanted to send an SMS. Then you'd have to create an instance off SMS service and then call its method. Similarly, say that you also wanted to send notification. You have to create an instance and call its method. So every time you need to incorporate a new listener, you have to make changes in this manage funds being well. This brings me to talk about the observer designed Patton and what observers and Baton states is that when you have such scenario with observer design, Patton, you don't actually create instances off the listeners and then call their methods. Instead, he would create a provision through which all the listeners can actually add themselves or subscribe to a particular event. Let me show you what I mean. Instead of calling methods by creating instances, I'm actually going to create a list off observers along with couple off methods. One method is to add elements to this list, and this method would be called by the listeners who want to listen to a particular event and this sort off add themselves to this list. And then we have this method publish wherein we'll just look through all the listeners and then call their method send message. So this time, but never transaction fails. We're simply going to call this mattered, which in turn we looked through all the observers that were part of this list, and Sarnoff called the handling method send message My beauty off this approach is that the managed funds being doesn't have to know about the listeners and all it is going to care is is just going to look through all the elements in the list and call their methods in pewter . If you'd like to add one more observer or even handling code, Then you can just simply create a class like this and then subscribe to the event by calling this method the reason why this is called observer design. Patton is because all these beans are observers who are ready to listen to an event published by the managed funds being and in here the managed funds being is observable, so we have an observable being and observers list of observers. This is somewhat synonymous to how the Twitter walks. If you're following a celebrity, you'll get a notification whenever the tweet. But the celebrity does not need to know who are all listening to them. Under similar lines, the managed funds been doesn't need to know who are listening. I hope it makes sense, and this is how eventing would work in spring framework as well. We're going to take a look at an example pretty soon. 64. 1903EventsInSpring: it's time to take a look at an example off eventing in spring framework. And as you would expect, these are the basic things required for us to perform. Eventing in spring are anywhere else. You would need the evened itself and in spring this has to be a class that extends application event, and we need a publisher as well, which is essentially a class from there. We would like to publish this event, and in order to do the same or in order to publish the event, we need the publisher object and that we can get by implementing application. He went published Rivere interface, and we need bunch off listeners who want to listen to that event. And for that we have to implement the application listener interface. But fortunately, with the recent versions Offspring framework, you don't have to deal with these classes and interfaces. Things have been kept pretty simple. Now this even been here is just a simple, poor Joe, and we can use auto wide annotation to inject the object the publisher object. And for listener who can just simply use an annotation, let's take a look an example so that you'll understand better. So what I have here is I'm trying to take the same example as previous, we're going to have a transaction failed event and for that have created an event which is a simple, poor Joe, just as you're seeing in here. Very simple. Poggio. No classes or interfaces implemented are extended. And then we're going to have the publisher cord now assume that we're having some kind off a code in here where we're trying to send funds to another account. And in case if there is a failure off the transaction, then we wanted to call this method publish custom event, and we're going to use this object, which is what a wired publisher that publishes event. And you would send an instance how this event and finally will have a bunch off listeners who want to listen to that event and inside our listener classes, all I'm trying to do is to use the sanitation, even listener, and we also need to mention what is even that you're expecting. In our case, it's transaction failed custom. Even the one would you have just created and you would have some kind of a handling god. Similarly, we have another event, listener for email as well. And in future, if you need to add another even handling cord, you don't have to make any changes in the publisher class. All you have to do is introduce that handling cord and then add that bean as part of the conflict file. And all this is somewhat similar to the Observer designed Patton. Let's try to run the program and see the result, and the court in here is pretty easy to understand. What I'm trying to do is I'm trying to just call this publish custom event method right from here, and ultimately we'll see the transaction failed. Event that's on the program and see the result, and there it is, ignored the first line. We're going to talk about it in a while, but here is what we're looking for. All right, I'll see you soon 65. 1904BuiltInEvents: spring publishers certain application context related events by default, and it would like to listen to them. He can use the sanitation. He went listener to listen to particular events. For example, this even didn't hear Context Refreshed Event would be published whenever the context gets refreshed. Similarly, we have events for context. Stop, start and close and here are there even handling code for each one of those events. And as you might have guessed in this case, the even classes as well as the publisher, Cord is reciting in the spring framework, and they get triggered when one of these operations would take place. But if you'd like to listen, you can listen to those events and do something meaningful. Typically, all these evens would be used internally by spring framework. We don't use them as often as the custom events and the real usage and the significance off custom evens would come into picture when you're developing a spring application using Spring Embassy. But since we're concerned with the spring core module at the moment, we just take a look at a very simple example off event in spring and that would sell fires to understand the behavior and the core functionality off events. All right, I'll see you soon 66. 1905CustomEvents2: In some cases, you might want to have a common handling code for a bunch off events. In that case, you can actually add an array off events as part of the even listener annotation. That way, whenever one off these even soccer, the handling cord would be executed. One destruction with this approach is that he can't actually get hold off the vent object and access its members, like we did in here, were able to get the even the object and we could access the method. He can't do the same in here. He can also for their narrow down the even such a wandell is, um, using a condition. This condition is written using so called spring expression language. If you're ever or regular expressions in Java, this is somewhat similar to that. This is nothing new that you need to learn. You can defer to the documentation and figure out the regular expression that you want to use based on your requirements. And the condition that I have put in here is just trying to see if the matter returned by the get user is equals to the following string. If that is the case, then the following even handling code would be executed. Let me quickly run the program. And since this method is going to accept a couple of events, and I'm sure that both these events are getting generated from our program, we should be able to see this message twice. And here it is, and similarly, even this message is seen. That explains everything. Well, you could actually have this condition inside the court itself after you accept the event, and it may be better to do so because you don't get any syntax highlighting. For example, if you change the name of this mattered, then you don't get any compiled a mirror. You'll see an exception after you run the program, and, ah, that maybe a little annoying. Also, it's worth noting that whenever you're trying to accept an event, Spring will not only accept this particular event, but also the evens that are sub classes off. This event, for example, have created another class, and there's just simply going to extend Howard transaction failed custom event. So if I published this event, that man coming this piece off code, so I'm trying to publish board the super class as well as a subclass event. Wherever I'm accepting, the parent class event will now also accept the subclass event. So in our output, we should be seeing double the results off what we have seen earlier. That's because those listener matters are now accepting the newly published event off this up class. Hope it makes sense. I'll see you soon.