Spring Full Course - Learn Spring Framework in 4 Hours | Spring Framework Tutorial | Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone I welcome you all to Eddie ray Cass YouTube channel your one-stop solution for trending technologies and today I'm here to give you a quick overview of the Spring Framework full course now Spring Framework is a powerful lightweight application development framework it is one of the most popular Java based framework which is mostly used for enterprise java application development now the main reasons behind its popularity are its simplicity testability and loose coupling now considering its demand in the market we have come up with the Spring Framework full course which consists of six modules now the first module is a general introduction to Spring Framework and its various features and in the second module you will be diving deeper into spring and understand its architecture and various fundamental concepts then in the third module we will understand an important concept which is the aspect-oriented programming now moving on to the fourth module we will talk about the dependency injection and in the fifth module we will have a look at the spring MVC architecture and finally in the sixth module we will discuss some basic questions that you might come across in your Spring Framework interviews now this was all about today's agenda but before we get started don't forget to subscribe to a Dirac ass YouTube channel and press the bell icon for more updates also if you are looking for an online certification in spring check out the description box below [Music] hello friends so very warm welcome this is Sean on behalf of Eddie Rica so I'm gonna take on to something on as Spring Framework father day and I hope you will enjoy the session so let's just start so what's the agenda for the day so guys we're gonna understand the Java frameworks for the day right and thereafter we'll jump on to Spring Framework and we'll understand why we need Spring Framework and what exactly it is thereafter we'll also understand the architectural part of Spring Framework and we'll try to discuss few modules available in the spring framework will also see what and all are the various features associated to the Spring Framework and we'll try to do one code on pink framework so let's start with the Java framework so what exactly are the Java frameworks for us so guys we know what exactly is Java right so it's a programming language which is supposed to help us solve a particular problem right and frameworks are nothing but they are the predefined API is which we can embed in our programs right so let's see so as a developer we are writing so we need to write lot of code and many a times when the projects are in a deadline so we are dependent on libraries to accomplish our projects in time right so these life is are nothing but they are the frameworks or they are the epa's right so as a developer you shouldn't be all that about you know writing lot of code so as of now you can see right so so much of course so as a developer we are pretty much attends about it so need something that is fast and efficient now that's where we gonna talk of Java frameworks or we do not talk of the api's that's like application programming interfaces which we need to put into our programs so here is a solution so I can use Java framework and using the Java frameworks it's gonna be very easy for us to achieve our targets and our deadlines in time right so this fits with my code to write and whatever the frameworks you're gonna choose so we got lot of frameworks lot of API is available in Java now so as per our requirement we need to choose for example let's say that I wanna in PDF what I want to generate an Excel report so I need not write a Java IO API so I need not to write my own Java io code so I can use the predefined libraries right so like I text or write PDF you know available libraries and I can do my tasks very easily so the problem is solved right whenever you start using the frameworks or when you start using the predefined code snippets in your programs now what's the key benefit of having all these things right so my application speed and efficiency is also increased because the frameworks or the API is they are written in such a way that they are very well optimized so we are looking on to a point now right so we started from a confused state and we have reached to our potential state where we have to meet our project deadlines and all right so guys the key target should be to use the frameworks as much as it is feasible and it is possible in our programming structures right you shouldn't be writing the code from the scratch so in a smart world if we are writing the programs from the scratch right it's not gonna help us right so always try to rely on the frameworks as possible as alright Java frameworks principle what exactly the Java framework principle is all about right so it abides the Hollywood principle that is 'don't call us we will call you so it's basically inversion of control which we are talking about so what exactly this key principle states like so we got some class right for which we will create the object traditional so what is happening over here so you create a class you write down a class and then you create an object but we are trying to talk of inversion of control which says that do not worry about creating the object so frameworks gonna create objects for you you just configure the objects now right so let's say if we have a class a which is having a dependency on some other classes like B and C right or you can say it is the other way around right so if B and C are dependent on a or a is dependent on VLC so how we can solve such problems so Spring Framework is one of the key framework right which focus is on inversion of control with the key principle as a dependency injection right so that's what we're going to discuss so the traditional way of constructing the java object is by the developer but the Java framework principle of the Spring Framework principle will state that you don't worry about constructing the object objects will be constructed by luke frameworks itself all right so let's see various different Java frameworks they will live in the market so as we had a lot of frameworks available in the market right - a name of youso hibernates struts and spring these are quite tough famous frameworks I believe in the market so JSF claimed a when we got a lot of pain works now it these are some of the most importantly and widely used frameworks in the industry so we are typically going to focus on sting primer so let's see that so what is Spring Framework and what exactly are its key features so let's now switch onto that so guys Spring has bring a label on SourceForge and it's available since January 2003 so it's basically an open source project and it was introduced by the guy rod Johnson if you can see this guy right so the first version was released in the February 2003 and so till that right so spring is available as in open source community you can always browse the spring dot o-r-g and you can browse TI's you can browse which documentation and you can learn a lot so or the passage of years right so we got 4.3 as in the latest version in 2016 right so this is widely used version in the Spring Framework let's now understand the definition of the Spring Framework so spring is one complete and a more Abdullah framework for developing the enterprise applications in Java right you guys when we talk of spring as a framework it's a Java framework but it is an Enterprise Edition Java framework so what is meant by this term Enterprise Edition right so whenever we are writing the applications for the enterprise's let's take an example of an enterprise so Amazon is an enterprise right Google is an enterprise so when we are supposed to solve the problems associated with the enterprise's we the Spring Framework aaalac so Spring Framework can be used for all the labor implemented for a real-time application right so when I am talking about the layer so let's try to keep it very simple let's say Model View controller architecture right so we got a spring MVC as a design pattern which you can use to completely construct all the layers in your application so spring can be used for the development of particular layer as well so if you are not interested in writing all the code in your Spring Framework you can also write few of the layer codes let's see some features associated with the Spring Framework the first thing it is open-source right so it means that you can anytime you have an access to the code and you can manipulate it and you can use it as in your own to me right so let's say I want to write a hospital management system on an enterprise level right and I need to use the Spring Framework so I can manipulate the framework and I can try to manipulate the epa's which fits into my key domain itself so we got comprehensive tools available in our Spring Framework right it's lightweight when we talk off for the Mamre part right so it's very much relevant in solving the problems because it follows the design patterns and it effectively solve all the problems we face in our enterprise edition of Java so we typically say that Spring Framework is basically a framework of frameworks now why framework of payments because you can use a lot of frameworks within the spring framework itself on an instance if you want to do object relationship mapping using hibernate so hibernate can be integrated or it is already there it's already there so we got the APS through which we can directly access hibernate right so that's why we say it's basically framework off frameworks so it avails array of resources so we got lot of resources starting from the very scratch let's say I want to implement security in my application or I want to implement something known as a model view controller or any other thing nice right so it's basically an array of resources so you can have a lot of features within the spring pay market cell even the support like unit testing is also available in the spring framework itself all right so why we need to use Spring Framework I think the previous discus features they are quite relevant that why we should be using spring trainer but let's try to understand it in more detail so guys that these are the relevant a user is across the industry right so we got different different frameworks available in the market bucket you can see the spring MVC so it outshining all the other frameworks in the market industrial right so it means that it is industrially adopted and it is quite you know robust and all the industry all the companies they love development in Spring Framework and it is really popular now there are reasons that why it is so popular so it's simple now so if you talk off for the simplicity level so there are three basically main reasons out here now the simplicity is the first level right the writing of code is very easy in the spring framework right because here you are trying to configure the things and you are not trying to write the major portion of your code here you are trying to configure the things then we got testability right and a loose coupling so these are some of the other key concerns and let us now discuss them one by one so when I talk of simplicity so here you are all these focused on writing your business objects as is Buju or model or DS right so what is for you put is plain old Java object so you are supposed to write your Java object definition in the form of class in your programs right so it's or your odd 4G so when I talk about plain old Java object in a similar way we got interfaces right so you can also write interfaces which will help us to get to our definitions to be implemented by us then we got something on as testability so Spring Framework is all be having the testing layers available for example we got j-unit already integrated so you can always perform a unit test or an integration test as per your requirement right so this is something where we can write our own test cases and we can test our own code right so this is one also great feature which is available in the spring member then we got loose coupling so here we are trying to understand that spring objects are loosely coupled now for this first of all we must understand what is meant by the term coupling and what is this coupling all about right so consider that you got a class Rider right and we got a bite so this bike over here the reference to the bike object is basically initialized in the set bike method right so we got this interface bike available and we have a start method so we got various classes like Honda Bajaj or Yamaha right so which can implement the interface by and we will have a matter like start now right and we can anytime at runtime set the bike to either be Honda or Bajaj or Yama so runtime polymorphic concept over here is implemented in a very beautiful way and it ultimately takes us to something known as loosely coupled environment so let's try to discuss this as one example that how loosely coupled concept comes into action let's say I got one class as in rider okay and we got one more class as in bike now if rider has a reference to bike and I am going to create one constructor for the rider where I am going to create an object of by Christ so object of bike is constructed when object of Ryder shall be constructed now that is the true reality over here so if I am trying to say that I will come up in the main so if I am trying to create the object of Ryder over here so when I am creating the object of Ryder as of now so the moment the Ryder constructor will be executed your magic object will be constructed it means that there exists a dependency of your by object construction on the writer object construction right so this is like a high in coupling so we got a high coupling over here now I need to reduce it in order to reduce it what I will do I will just come down and I will create one function like set bike and I will write the ref Prince over here in this fashion right so I'm gonna say this not be a sign be so it means that now you don't do the concept out here so rather then you create the object over here in the main now the construction of bike object is not dependent on the constructor of the rider right so that's one key concern over here so what I will do I will just say r dot take the bike to be B right so this is where we are trying to reduce coupling right and I will say that it is low coupling so guys this is bear we will be reducing the couple so when I am talking of the spring framework so spring framework will focus on always writing the code so that it is loosely coupled right so loose coupling concept with the runtime polymorphism it brings a charm in your code as in design pattern so what exactly is the ecosystem of the spring framework so guys we got a babbler right and we got a common layer service layer and add a dolly so these are the different layers available in the spring framework ecosystem so we got the web layer right and it's basically the spring IO platform right and thereafter we got a common layer where we do some basic operation in spring boot right and their apricot service layers and the data layers available right as in the spring XD and other you know components available in the spring claim right so this is like the entire spring framework ecosystem how the spring frameworks gonna come up and interact and it is available in various modules available so let's see the architectural part of the spring framework so guys in the architecture part of the spring framework we got the base layer as the testing layer available right so it's a layered architecture not so spring architecture is a layered architecture so the testing layer is basically focusing on the j-unit part right so where we can write the unit testing then we got the core container so core container of the spring framework includes being school context and spring expression language so this is the major and the main layer of your Spring Framework right so here we got inversion of control and dependency injection which is using after layers even right and this forms the core part of your Spring Framework so on top of your container we got AOP aspects instrumentation and Massillon so a up is basically aspect-oriented programming so with the aspect-oriented programming we try to focus on cross-cutting concerns so what is meant by this certain cross-cutting concern cross-cutting concern is the concern which is a kind of a security knob so security has to be taken care in all the modules whenever you are writing the project right so aspect-oriented programming helps us in implementing the cross-cutting concerns where after we got data access integration into the help of which we will be able to access our databases right or you can say where we got our datasets available we got a web container over here where we can write the spring MVC web application right so we can have servlets then portlets which are the standalone applications what the maybe interface is itself so let's see the modules so the spring module basically it contains a lot of features and they are quite well organized and we got somewhere around 20 modules available right so the modules are grouped up and they are based on the few of the features as in the column right so as in the layer conductor we discussed so in a similar way we got a framework simile so we got coal container data excess or integration right so you can access the data sets or you can have integrations with the frameworks like hibernate or soap so core container will be the one which is the basic functionality so using the web layer or the BIP kpi's we can write the web MVC code then aspect-oriented programming the way we got object-oriented programming in a similar way we got aspect-oriented programming so here we are trying to fulfill the cross-cutting concerns then we got instrumentation and the testing years let's talk about the modules one by one so when we are talking about the core part of the spring frameworks we got a core container which focuses on dependency injection and inversion of control so the core layer provides the fundamental parts for the Primeau so these are the base of the Spring Framework ah right so we got something known as beans context and spring expression language so these are the three important of key concerns right so guys beans are nothing but they are the Java objects which are constructed by the spring frame so you need to configure them then we got context it's an implementation or the beans itself right so it supports internationalization for example EJB JMS or remote if you want to come up with that then spring expression language is an expression language with the help of bits we can write programs even the data access modules of the spring framework will be able to access the data sets right so if you have the data available in the database let's talk about we got an Oracle as a data base right and we need to interact with the Oracle and we can do so very easily with the spring framework so we got a JDBC layer we got or M layer so that's like these are all integration layers so over m is like object relationship mapping for example the hibernate so we got this integration layer which we can use then we got basically an object to xml mappings available then Java messaging services are available then transaction management is also available right so if you are going to fire a transaction so commit and rollback all all these supports are available within your data access module itself so guys over the web layer we are trying to understand something known as a Model View controller based web application right so the main industrial usage over the web is WebMD see module right so that's like one of the major modules which is quite readily used in a Spring Framework and it is initially accepted so we got something known as web reentered features right we got portlets and sockets so portlets are nothing but they are the standalone applications which are running in a web application right and thereafter we got WebSockets in order to come up with a two-way communication so the few miscellaneous modules which are available in the spring framework includes AOP right so AOP is where you are going to fulfill the cross-cutting concerns then we got instrumenting where we provide the class instrumentation support and class load or implementation we got aspects right so aspects are nothing but it provides the integrations with the aspectj and aspectj something where we gonna fulfill the cross-cutting concerns exactly where then we got messages right and the testing layer so guys in case you want to come up with the unit testing so we got support with the help of components like J unit or testing G so these are again the framework so that's why we call a spring framework as a framework of frameworks now let us understand how the spring framework will flow so guys the very first component in the spring framework is inversion of control right so we need to come up slowly and slowly so that we can reach up to the endpoint so the first component is inversion of control let's see that so what exactly is the inversion of control so traditionally when we are writing the program so we will create the object right so that's like you come up and you create the object so once the object is constructed right so we need to wire them together so let's say we got multiple objects now and they have a dependency in between let's take our previous example as in bike and writer example right so rider will have a bike so now we need to you know fulfill this dependency somehow so we need to configure them in spring frame what we do we configure them and thereafter so whole management of the lifecycle off the objects is taken care by this spring framework except so we don't worry on that part so guys we will try to code this flow and understand that how this flow will exactly work all right so what and all are the other concerns related with the IMC container so why is the spring IOC container by using the Java for your classes and configuration metadata produces a fully configured and an executable system or an application right but we are trying to say is that you come up with the pojo class you write the pojo class and you provide the metadata in the form of some xml configuration you give it to the Spring Framework or the spring container and it will automatically give us the objects which we can use directly in our application or you can say we got a ready-to-use application itself so let's see what exactly are the ioc containers so we got two different api is which are the ioc containers in the spring frame one is the bean factory other one is the application context so bean factory is basically an interface and application context is an implementation of being factory itself so it is always better to come up and use application context so what happens over here these containers will basically load the xml files and thereafter they gonna construct the objects for us so we can also do by constructing by writing the annotations in our podio classes itself if you wanna skip xml you can come up with a notational model so that is also supported now after the inversion of control we got dependency injection not dependency injection is all about when we have dependency in our code right so Spring Framework has this di technique where we can remove the dependency from our code right so it's going to make our application quite easy and manage to use and most importantly our programming code becomes loosely coupled right so that's the key important point which we need to take care even when we are writing the code examples alright so how this dependency injection is taken care by the Spring Framework so in the Spring Framework we got PI constructor and by setter method so these are the two different techniques right by which the dependency Injection is resolved right so we got a constructor arg and property as he tags which can help us to resolve this dependency injection then next is the AOP that's like aspect-oriented programming so aspect oriented programming is basically Bill we need to implement cross-cutting concerns right so I just try to take up one example of a cross-cutting concern right so consider that we logged on to Amazon and we want to look on to a product right so I search for an iPhone so the moment I started searching the iPhone and I am browsing the different different iPhones in the same category that's like Apple iPhone so Amazon will record from what time I started surfing the iPhones and what time I left surfing nice so based on that Amazon can give me some promotion offers or I can get some you know emails or I can get some advertisements from the Amazon which will say that you must purchase this iPhone right so logging now logging is also kind of a cross-cutting concern to implement such cross-cutting concerns we can use aspect oriented programming in the spring frame so it gives us a lot of other things right so as in the number one it's gonna provide the modularity then AOP will break the program into the listing parts called concerns it also increases the modularity right when we write these cross-cutting concerns and a cross-cutting concern is a concern which can affect the entire application right for example security is one of the major cross-cutting concerns right so we must be implementing them in a proper and in a regular game alright so have a look guys the so in a cross-cutting concern so we have a different different modules and which we got couple of layers for example Model View and controller so we need to implement these cross-cutting concerns in every module and in every stage right let's say security so if I am writing a very basic code and I am trying to come up with the last level of my code I need to make sure and I need to ensure that security always remains so the component or the last important concern which we have to discuss over here is a model view controller as in the spring framework right so guys what happens over here when we are writing any enterprise application on a web application or if you try to write any software product right so model view controller is one design pattern so in this design pattern what happens the user will send a request over the presentation layer right so this request can be on to presentation layer that cycle user interface so it is typically a view right so requests will go from the view to the controller and controller is that guy which will process the request from the client or from the user right so typically the client over here is acting as a browser now Creek so let us try to understand if I am going to search an iPhone in the Amazon right so then the Amazon website is open it's the presentation layer we call it a view where I am searching the iPhone and when I click on the search so this request goes to a controller which will process through all the data set so model represents the data set so we'll get the data from the data set into the controller right and controller can give it back to the view and this can be sent back as a response to the user so model view controller as a design pattern so what we need to focus we need to write the model separately controller separately and do separately so spring MVC as a complete one package helps us to write minimal of code because it's all you know modeled as a framework itself for us right so you need to write minimal code and you just need to write your own business logic and everything else will be taken care by the spring MVC flow all right so let's summarize the entire part so guys we got Java frameworks we got Spring Framework Y spring frameworks the architecture part of the spring frameworks what are various modules available in the spring framework and few of the spring features which we have discuss right so now let's try to come down and see one snippet over the spring framework right so I'm gonna write one java application so let's try to write one java application and let's see how we can come up with our Spring Framework example so let's say it's one spring at Eureka demo and I'm gonna finish the project so here we are with the spring every condemned now what I am going to do over here is I am going to write one new class and I'm going to write the class name as employed so let's try to pick one package as carotid Vika and let us finish it off now on this employee class should be a POJO class so I'm going to write one employee ID let's write one employee name and let's also try to write some employee salary so if you want to come up you can come up with some more data right so these are the attributes for my object which I have mentioned in our class right and thereafter I need to define constructors getters and setters to come up with the POJO structure right so let's say the right click source generate constructor using fields so this is one constructor along with all the fields so I am also going to construct one constructor as in a default one so let's try to write one default constructor I got one constructor with all the inputs and the costly I'm gonna do it as generate getters and setters let's select all so here we are with the getters and setters and now I'm also gonna write one to string method so let's generate one to string method so this is one structure which is representing Hojo or model or being right call it as a POJO or a model or a bean structure right so once I am done with this what I will do next is that I'll write one client class so I'm gonna write one class as inclined along with the main map right so this is my main method so what is the traditional way as a developer how I will construct the objects right so I will say employee as in a new employee right and thereafter I will say e dot set of Eid as 101 he dot set of employee name as in let's say John Watson and finally I will sit a salary let's take 30 thousand right so this is the traditional way how you will construct an object so other than that alright so I think I did something wrong alright so I have taken the salary as in spring so guys let me change it to the integer part just a moment so here we are done now I will just say C so and I will try to bring the e so what I will get as in response I will be getting the response as in the details of the employee now this is my traditional be how we construct the objects in Java let's try to follow the Spring Framework and let's see how we can come up with inversion of control right so I'm gonna write something known as inversion of control right so IOC as in Spring Framework so let's see how we can do that to do that I'll follow certain steps the step number one will be to add jar files for code I'll just say for spring code right so let's add jar files for spring code right number to configure Java object in an XML file right and number three use spring container spring IOC container so when we are saying I use a container for example bean factory or application context to get the objects constructed by them so we now need to follow these three simple steps to perform the first fundamental that's like inversion of control we add the jar files for spring core and thereafter we're gonna configure the Java objects in an XML file and finally we'll use the spring IOC container for example being factory your application context so that we can get the optics constructed by the spring container itself right so let's see how we can do that so step number one I'm going to configure my project with the build path with the spring code jar file so I'm gonna say build path configure the build path and in my build path configuration and add the external jars under the libraries so here we are with this spring jar so I already have my core jars available over here right so you can download it over the web so I'm just gonna link these jar files with my project structure and let's say apply and okay so here we are with the spring core jar files available in my project right so once I am done with this so the next part is to come up with one XML configuration file so let me take up to that so just a moment so I'll just copy one of the employee bean dot XML file so I have this XML file and I am going to copy it in my SRC directory if you will see I'm not copying it to my package I'm copying it into the SRC directory out here right so once we are done with the topping of this xml file let's try to open and see the structure of this XML file ah the structure of the XML file says that it's basically one root tag known as beans with the tag known as beans and the property so what is this bean ID all about so ID can be any name of your choice for example I'm gonna say this is m1 and the class over here goes like co dot a do Rica dot employ the property is the attribute names available in your audio structure e IDE name and is salary right so I got a ID so for my Eid the value goes like one or two right and similarly I got a name and is salary in your case I think let me just cross check right so it's all in lowercase so he name goes like Jenny and he salary goes like let's say 50 powder so this is my one of the B so I can declare as many as object configurations over here right so let's try to come up with some m2 and let's say this is 103 this is Jack with 46 right so these are the two configurations which I have done in the XML file so the first thing was to link the jar file second thing was to configure the concept of my objects as in key value pairs so key our attribute names and the value is the exact value which the object will hold for its atoms so this is the state of an object now right over here now what is next so the next part is to use the spring container in my client class so use the spring IOC container so I'm gonna use the application context out here right so let's see how we can use the application context so you say application context if you can see over here so application context context as in a new class path XML application context and you mention over here employ beam dot eggs inner so that's this name of the file can be any name of your choice right you are free to use any name but make sure that this employee will not XML file is available under the S or C directory right so here we got employee B in dot XML file write it and that to within the application context so now I will say employee even as in context dot get the beam you mention as m1 so what we are doing we are using the API Nunez get B so this m1 we gave it in our XML configuration right so over here I'll just start down cast it as in employ because it's gonna return me the object right so we need to downcast or the other way of using the same guy is like you say me2 as in context dot get the be m2 and you just mention the class over here like this and now let's say C so e1 and C so of e to right so here I'm just gonna put a delimiter spring IOC in action right so this is one delimiter let's run the code and see the output so guys now we can see the traditional way of retrieving the objects was you construct the objects you send the data right and the spring framework says that you don't create the objects you use the objects in your program and now what is the beauty about this XML file coming into earlier so many times will be confused right so why we are doing all these stupid stuff right the reason is that XML over here can be changed anytime right and you need not to worry about changing the source code so it's not the kind of a source code right so XML file is not a source for which will be compiled into some binary and then binding will be processed by JVM so JVM is processing the Java files as in the bytecode so this XML file is not a part of your source right so it's gonna be modified at any whenever and as required by us right so here the configuration goes in the XML file so the real implementation of the same inversion of control can be seen in the Android right so let me you know run down with one example over here so where this inversion of control fits into it so let's see that one so how the Spring Framework is related to the Android let's try to come up and understand this part it's very beautiful concept the inversion of control which we have demonstrated over here right so let me try to open any of the applications over here all right so let the system gets loaded for us so even though it's exactly you know out of our context but it's just for the reference purpose right so we'll now see that there is something known as these layout files which are all the XML files if you can see over here so these are different XML files and in this XML files I can see that we got some you know configurations done out here right so these are the properties now so what is the menu it's a drawer man you write what is the weight what is the height so for every a you know Android UI part so we got this configurations available in the XML file right so any time the user can actually come down and change the XML file structure so the code will not be affected so what Android will do Android will read this XML files and it's gonna construct the Java objects its inversion of control which is available so in my boat if you can see over here right so the UI is exactly dependent if you can see the same XML file structures right and we are trying to say find view by ID so let's try to understand this so here you can see the toolbar coming out right so as in some toolbar and here we are trying to say find view by ID as in toolbar so it's the very much similar story as in we are trying to say context dot get B right so the APA standards are different so guys this is what we got as in spring IOC right and introduction to the spring cream [Music] we're gonna represent the spin framework tutorial now so hope you will enjoy it so let's see what and all we have in our Spring Framework tutorial today so what's gonna be the agenda will understand why we need the frameworks in Java and what exactly a Java framework is so thereafter we shall be proceeding towards Spring Framework now that's where we're gonna focus on we will see what Spring Framework why we need Spring Framework and various modules available in the spring we shall be focusing practically on inversion of control that's like IOC container which is the core of the spring module we'll see what our beans and we'll also see what is meant by a dependency injection so why Java frameworks still eyes are there are various problems associated with the enterprise edition of Java right so when you talk about the technology it becomes complex when we are writing the enterprise applications to name a few we got a lookup problem so when you gonna do lookup right so you're gonna face the problems and on the Mamre front again the optimizations are not done properly right so we got heavyweight components and they're gonna focus more on memory pod so we need to know Java frameworks which are basically a collection of classes we say flexion of api's it's a predefined code which you can use in your programs to solve some specific problem right so how the things gonna work over here so we got large bodies of predefined code available with us right we can also call them api's so framework is nothing it's a set of API so so if you talk on the code part so it's nothing it's a byte code right the class is available for us to be utilized so what we need to do we need to add this snippet or this predefined API in our program right so that we can solve a problem for a specific domain right so that's how you gonna come up with your API usage now what can be the advantages of Java framework the first thing is efficiency right so we want a security we want less expenses and a support right now all these features so they are an expectation at the developer end right so when you are writing the code if you are using the framework if you are using the predefined code so all these features you need to relax now on these all four fronts right so the code which you are going to use it will be efficient right it's gonna be secure so you need not to up pay for it because most of the Java frameworks they are open-source right and you just need to use them you just need to include it into your program as a dependency and then you finally use it right other than that support in the form of documentation or even you know in the form of various Stack Overflow threads coming in so it's it's gonna be very easy for us to manage any problem which we have so there are various disadvantages evenly associated with the Java framework right so the number one is you got a restriction then the code is public and finally the custom build features let's understand that point now so we are restricted to use an API the way it is so you can't modify it now that's the whole scenario if you want to use an 8a in your own different way right you can't do that so we are restricted to use the same API let's say that one of the map third it's taking three inputs and you are supposed to have a method which can do the same task with only one input so you can't do that now it's a limitation right so there are restrictions the code is public it means that if you are going to use any of the API what's going to happen the same API is exposed to the public let's say you are supposed to use an open source API which is doing an encryption and decryption for you right so it's gonna be very much you know challenging for us to maintain that kind of a security then the custom built features so if you have API is you can write your code but the thing is whatever the API is written so what is the language or the technology behind the API so many times that will not be exposed to the developer right so specifically in case it's a web service you might not Oh what is the internals of that API so that's what the encapsulation is all about right and here you face a challenge again so we got various Java frameworks available now right there are a lot of frameworks available so frameworks is nothing it's a set of api's so you got JSF may even hibernate starts plays spring etc etc etc guys none of Java frameworks are available in the market so specifically we are supposed to discuss on Spring Framework all right so there's a web development team of some XYZ company with this group of six members and it's hired by the manager Harry white so he's the leader of the team so Harry has got a call from the management team with some urgent requirement NOC right so he's having an urgent requirement to be implemented and to be delivered to the client somehow so he we got one request from the client that we need to write an application but the challenge is that we have got very little time frame to finish off this product now this is one challenge which as a developer or as a development company we don't have face it for sure so clients whenever they're gonna come they're gonna come up with the first thing as we have very less time right when they have less time we need to perform the end proper to the client so we need to rely on the framework somehow so if we go by the general way it's gonna take a lot of time in writing the unnecessary code right it's gonna take a lot of time so we need something that's fast flexible and efficient so it's not only that we're gonna use a framework but we also need to consider more of the parameters so that's like you should have an efficient code which you can integrate and you can write an application way right so in case we need to use any framework now Riya says let's let's use any framework over here so Alice says we can go for starts when someone says we can go for hibernate so guys these are all but Java frameworks more or less right so finally I says that we can use spring framework now why specifically Spring Framework because it outshines the other frameworks in way that it's a framework of frameworks knob so it provides us the flexibility as well as we got model-view-controller architecture right so spring MVC is basically a based on interfaces so the way we got struts and hibernate so they are not all the interfaces but you need to use the objects and classes whatever we have so in spring framework specifically in the MVC what we got we got interfaces so that we can write our own implementation now that's the benefit right so we our interface we just use it we write the way we want to create the implementation of that interface so this way you won't affect the client side even all right so scott says let's have a discussion on Spring Framework so how it's gonna solve our problem right so so guys Spring Framework increases the efficiency of the application so it's gonna be optimized so the code is optimized so it's gonna reduce the overall application development time since it's a framework right we need to use the API so it's gonna help us in our development time so we can easily finish our project in the given time frame now that's where we try to fit in the spring so that's really nice let's proceed with the spring main so guys let's see what is Spring Framework right and what it is all about so are there a lot of features if you can see now right so it's an open-source framework it's a lightweight application framework it means that it's a less on memory so the objects constructed in the memory so we're gonna take less space and it's efficient it's optimized so overall code complexity is also reduced over here and we got various tools associated with the spring framework which you can use we call spring framework as framework of frameworks so what we can do now over here we can integrate the hibernate code with the spring code and we can use it in our application wear right so we got various tools it's an array of resources which is available to us and we can just use and we can concentrate only on writing out this logic rather than focusing on writing some unnecessary code so spring was released in the February 2003 right it's available on SourceForge and the guy rod Johnson is behind the spring framer right so this guy did the spring framework now spring framework has an eco system if you see on to the spring framework right so we got web layer the common layer the service layer and the data layer so we got various modules coming in to our Spring Framework ecosystem so we got spring i/o platform then we got a spring boudin we got a spring XT so that's like the foundation layer right so if you can see now so how from web layer to the common layer to the service layer to the data layer how we gonna access it so everything we got everything now we got from spring data spring cloud right you've got a lot of things down so spring is a kind of a framework which is basically ecosystem starting from mobile apps to the web apps you can fit it anywhere right so that's a pretty good ecosystem which is available to the developer so why we should be using Spring Framework right so that was something about Spring Framework so there is some industrial use it off the spring framework over the other frameworks so it's a 40% as compared to the other frameworks if you will see that so this is on top of it right so this is industrially Spring Framework is used very widely rather than the other frameworks if you can see this slab coming up right so spring framework is maximum with 40% now why so so guys number one distinct division between Java Bean models controllers and view so what I'm trying to say we're here is that spring is basically an MVC architect so Model View and controller so all these three are the different different divisions right so we can say it's an architectural design pattern which is followed by the spring frame of one of the most popular design pattern right so it's very flexible and this is very flexible as it makes the use of interfaces so here we use interfaces so that we can write the implementation as per our requirement so nothing like you know friction coming up over here then spring MVC web tears are typically easier to test we also got testing modules available in the spring framework itself so J unit and all is available so we can use the testing frameworks then well defined interface towards the business layers spring controllers are configured via inversion of control so guys there is something known as IOC which is the core of the spring favored we wanna understand it practically what exactly this term IOC is other than that offers better integration with view technologies other than JSP so let's say I want to integrate you know J speed with the JDBC I want to use JDBC code in my JSP right so one way is typically the developers they're gonna follow the basic approach they're gonna write all the sequential steps and they're gonna do it other than that we're gonna use a design pattern for example the DAO and DDO right you're gonna use the design patterns and through which you will be able to access the JDBC so what spring framework will do Spring Framework is already having the integration api's which follows the perfect design patterns so that it becomes scalable robust modularity of your boots quran' has right so all these kind of integrations we got the ati it's available within the spring framework itself so you need not to worry again on writing the core core right and you need not worry even about the design pattern so it is already based on the design pattern somehow alright so let's try to compare spring starts and hibernate even though I burned deserve entirely back-end framework where spring and starts they do different jobs but let's try to have a differentiation so the first thing is that spring is an application framework right it's lightweight layered architecture and loose coupling so when we say loose coupling it means that less dependency of one module on the other modules right so just imagine that we are supposed to you know reserve a flight and we need to use a flight system and if that web service goes down due to any of the module failure so there will be a very big loss to the airline company so coupling of one module over the other module should be as least as possible so you need to have loose coupling so Spring Framework by default is designed in a way that it has it facilitates loose coupling more or less so there's nothing like tag library so it's basically it's a application framework but it has no tag library so it's all about interfaces and the implementation of the interfaces so very easily you can integrate the ORM technology so you can integrate hibernate a Baptist anything you can integrate with the spring famer very easily other than that the client-side technology integration is not that easy when you talk of Spring Framework right so so these are some of the key differences when you have Spring versus the structure or the hibernate now let's try to see what is the architecture of the Spring Framework so that if Spring Framework architecture has various layers now it's a layered architecture right so the base layer is the testing layer on top of the testing layer we got the core container so today we gonna discuss on the spring core container right to understand what are beans what's the core of the spring framework that's like inversion of control we're gonna focus on that part and we'll also see what is dependency injection so the core container over here will have beens code context and a spring expression language and on top of code container we got a opie layer so that's like aspect oriented programming so guys aspect oriented programming is quite analogous to object-oriented programming so here what's happening your aspect oriented programming will focus on how we can implement the cross-cutting concerns how we can implement the cross-cutting concerns over here right so we gonna understand the AOP module in terms of how you can have a cross-cutting concern which is a fulfilled using something known as aspects right so that's how we got a UT moving on we got data access or integration layer so here we gonna deal with the over m or the JDBC or all your a back-end part right the vet module will focus on how you can write a web MVC application so for example the servlets or the portlets or the sockets or the web layer right so that's how you got various layers if you live in the spring architecture all right guys so what are all are the module so we're gonna discuss the core module over here so the core module or the core container is having four off of these major key concepts the first is core so that's like the fundamental part of the Spring Framework then we got the B which is provided with the help of a bean factory so it's like a container which is going to generate the bean bean is nothing it's an object guys right so it's an object we got the context it's some medium to access any objects defined and configured then we got spring expression language so spring expression language is one of the powerful expression language through which we can do lot of stuff over here then we got a data axis so I data access deals with the backend part the data base spot we got JDBC so we got a JDBC abstraction layer which we can use then we got over N so ORM here Spring Framework will give us an integration layer so that you can have object relational mapping API so then o xn is also there for any abstraction layer we got lasting service so that we can consume and produce the Massa disk then the transaction management either it's programmatic or it's declarative right so it's also facilitated through spring frameworks data access layer the bed layer has bed socket servlet babe and portlet so webs of it is basically a support right based on a two-way communication between the client and the server so servlet is again the core part of the Enterprise Edition so here it contains the Sprint's MVC implementation for the web application and the VAP oriented integration feature is also one of the key feature over here in the web part then portlets are nothing but they are the standalone applications running on the web application right so it provides the MVC implementation and all mirrors the functionality of a whip servlet module we also have some miscellaneous modules where AOP that's like aspect-oriented programming which is fulfilled with the help of aspects then we got instrumentation support and how we can have class or implementations then the messaging support is available and the testing support is also available if you want to perform the testing right you can have a unit testing available with the help of a unit a library right so guys that was a bit of theoretical introduction to the spring framework right so what we are going to do now is the first program in your spring framework right so let's see how we can write the first program in the spring framework so number one you need to have an eclipse ID right so you need to download the Eclipse ID and you need to install it so once you do it you need to do it from the Eclipse dot o R D right so you download the enterprise edition of Eclipse go to the help and go to the Eclipse marketplace so you need to select the marketplace option and from there you just search for the spring and here you will get the spring IDE so the versions might be different right so whatever the latest one it will come automatically for you so you then you're gonna click on this install button and spring ID within the Eclipse IDE will be plugged in right so we need to first of all perform this step so that you can make your Eclipse to be ready to write any spring application so for the further steps you need to confirm and proceed right so once you click on the install button you need to finish the installation and then you say confirm so once the installation is finished it's all next and finished right so just restart the Eclipse even though it's going to ask you automatically but you if it is not so restart the Eclipse and then you will be ready to code now in order to code the application we got five simple steps the first one is you need to write a beam class then we need to have an XML file then there will be one main class that's gonna have the main map right where you gonna write your core logic you need to load the jar files required for your spring code framework and finally you do the run on your project right so that's the you know sequential approach of writing our program right so guys let's try to focus on how we are going to perform this operation so let's try to see that if there is a class known as employee let's say that there is a class employee and for this class employee I got an e ID that's like employee ID and you got the employee name coming in let's try to say the address off of the employee right and we can also have what's the gender of the employee a lot of other things you can write over here right so what are these these are nothing but these are the a P right so we got the attributes coming in on here so that than that we can have methods of our choice now traditionally so or you can see conventionally how you write the object right so the object construction so how you want to construct the object in order to construct all that you will say employee then the reference to the employee and you create a new object of employee right so this is how you gonna create the object op employ so it's a very basic to understand that traditionally if you want to write an object of employee it's very easy in Java you just write the class name a reference variable then a new object right so there's a call to the constructor somehow what I can do now I can just say I left our PID is 101 right then you say ear F dot E name goes like John Watson then you can see here F dot address of employ it goes like let's say redwood shoes and then finally you can have here F dot gender which goes like mommy now this is the traditional approach how you write the object what spring will do so spring has a core module right which follows inversion of control now what is meant by this term inversion of control let us try to understand at this core concept spring IOC so what spring IOC says that you don't create objects right so you don't create objects now objects shall be configured in an XML file so objects shall be configured in an XML file by the developer right so there is one module known as spring container right so spring container will be responsible to construct the Java objects by parsing XML file so you don't create the objects so objects will be constructed by the spring container by passing the XML file so you need to mention the data which these attributes will store in an XML file right so that's what we call inversion of control so having the control over the object construction is given to the spring container right so what is the benefit of having such an object the benefit of having such an object is that your XML files they are not a part of source code right so you can configure you can anytime manipulate the values and your object will be constructed accordingly let's see that how we can do it in our product right so my Eclipse is already configured guys I'm just gonna write one new Java project let's start and let's see how we can do that part so I'm going to write one new Java project and let's uh name it like spring core demo and let's say next and let's do a finish on it and now what I'm going to do I'm going to create one bean class right so let's say a new class so I'm going to write it within a package known as kirat IDO Rica and let the name of the class goes like M point let's finish it off so here we are now what I'm gonna do I'm gonna write the same parameters gid then you got a name and let's have one employee entrance alright so guys I'm gonna take three of the attributes you can take n number of attributes over here as for your choice so we have these many attributes now I'm gonna write down the methods so methods typically are the Constructors then the getters and the setters right so I'm gonna write one employee constructor which is a default one right so it has nothing over there then I'm going to write one parametrized constructor so I'm gonna do right click source generate a constructor using the fields over here if you can see right and say okay so here we are with the constructor along with the attributes now you can say a source and a generator of getters and setters you say select all you say ok and here we are with a get two setter methods and finally I'm going to generate a two string let's say generator to swing and say ok so here we are with the to shop now such a class over here if you can see is referred to as a bean all model or Hojo so food is like plain old Java object right so plain old Java object so basically here we don't have anything other than the attributes and the methods to write and to read into the attribute so that's like a bean or a model or a poodle so it has no business logic right so there is no business logic over here now what I'm gonna do we're gonna write one more class and I'm going to write this class as a client class with the main method so here we got a main method right and this is my client class with the main method so let's see the object construction so how we consume the objects so we wanna say employee ref is a new employee right so I can say EDF dot set of employee name after on what son then you can say ear F dot set off employee ID let's say 101 so guys even though my attributes are you know not private but I'm trying to just utilize the the set of right so if they are private that makes a more sense of using the settles over here right so here F dot setup employ address so let's say it's gonna be like they're good shows and now when I'm going to do is see so on employee details and then let's try to say yep right so when I'm going to print EDF it's gonna call the two string method right so when it's gonna call the two string method so you wanna get these details Bogota and ploy right so let's run the code let's run it as a java application so here we can see over the console so you got this output coming in right so employee details so II I it is one one name is John Watson and addresses redwood shows so who created the object now so object construction was done by us right so this was done by developer now what we want we want to do it in a spring way so what spring says right so spring way we're gonna implement IOC that's like inversion of control right so you wanna say we need to use inversion of control so in order to use inversion of control over here we shall see how to write the spring API is and how to utilize them the very first thing is you need to add jar fights so let's add jar files to add the GAR files do a right-click and then I'm going to say build path configure the build path right so here we are with the libraries and I need to do add external Josh if you can see we've got a spring code em over here I did a right-click I went to something known as build path configure the build path then you see libraries which I'm gonna add the jar files and Here I am with the spring cool jar files for here right I'm gonna say open and then I'm gonna say apply and okay so here I am referenced with my spring code framework for here right so that's like my spring Crowe framework now what's next over here in order to use my spring code framework I need an XML file so there are five simple steps right if you can recall so these were the five simple steps if you can recall we got a beam class so that's like employed we got a demo class that's like the client we loaded the jar files and now we need an XML file right so I'm going to copy my XML file so just give me a moment so we'll just copy the XML file so I have this fine known as employee being dot XML file eyes I'm just gonna do a control C and exactly within my SRC over here if you can see I'm gonna do a paste of it so I just copied and pasted it one of the XML file available over here right so nothing you know very complex which I have done it's very basic which we have done so far now let's open this employee B not XML file so the very first thing over here if you can see it's an XML file with a root tag of beans and beans containing one bean over here so wanna say the idea of the bean over here is something like EMP and the class goes like Co dot ed we cannot employ right so I am going to give the reference over here and this is the class name so there will be some properties now so the property goes like e ID e name and E address so these are the three properties available over here right if you can see a ID e name and then EA trace so these three properties I have given over here ie I de name and address so for my a ID the value goes like 1 or 2 for the a name goes like Jenni and my address I'm gonna write it as let's say some southern shows let's take some different address value here now you can write more beans as of your choice right so let's say this is M 1 this is M 2 and here it goes like 103 let's say Jack let's say Eastern right so these are you can say it's a concentration resource for my objects so I am just putting up the key value pair over here so key is the attribute name and the value is the exact value with the attribute codes let's come to the client let's see how we can write the bring me how we can do the inversion of control to do the inversion of control we'll write an API known as resourced so it's like from the Spring Framework if you can set your interface now right so the third one resource resource is a new class path resource and here I'm just gonna give the name of employee being dot excellent so if you can see now so what we have done we have given the name of this XML file over here right and thereafter I'm gonna say being factory so again we got an interface known as the bean factory over here so bean factory factory is a new eczema bean factory from the resource now this is known as the spring core container right so being factories nothing it's what it's a sprint container which she'll read or which she'll parse XML file and construct the objects so we don't construct the objects now so if you can see so this was the traditional way how we are writing objects but this is something the spring way right so now what I need to do I need to say employee II one is factory dot get the bean and you just mention the reference that's like nth one so I want to get the reference to the nth one so I'm just writing the same point and if you get it so you need to do a typecast over here similarly you can also try to get the second guy you can say me2 is an employee so rather than saying it in this way I will use one more syntax so I will say a get beat here elevation the m2 but the tide goes here right so this is one more way of getting the reference to the object so objects are constructed by the spring core container and you are just obtaining the reference to the object right so you do a Cecil and you say employee one details plus e1 right so it's gonna call to string for you similarly employed to details it's gonna go BBB let us run the code right so guys here if you can see so spring framework came into action so details now coming from the spring core container object so object was constructed by the spring core container you didn't create the object so this was a traditional approach how we write the object and this is a spring approach how we are getting the reference to the objects right so let's see it once more so what I'm going to do am grab this write a thesis statement in this so I'm gonna say employee object constructed right so we know how many objects are constructed so one is constructed by us and two are constructed by the spring container so when you run the code you find that employ object constructed and to employee objects constructed out here right so let's try to comment this code over here so we commented this code over here and we tried to run the code and now we can see one difference so you didn't gave a request to get the beam and the beam factory didn't create the object for you so you have configured your XML being resource file but the objects are getting constructed when you request for them right so when you are requesting for them and now when you run the code over here you will see that your objects are constructed so object construction is happening with the Spring Framework and whatever the objects are being constructed to the Spring Framework they get constructed when you request for them right so this is what we call as in the Spring Framework cool layer where we got five simple steps to do it right so these are the five simple steps so you need to write one beam class then you must have an XML file right and in your program you need to use the spring way of doing it all right so now we got one more API if you can see over here application context as a new class path XML application context right so let's try to understand one more spring container over here the first spring container is this bean patterning the next spring container is known as application context so application context over here is also one of the code container in the spring framework which is used to do the inversion of control and it's basically an implementation of the beam pattern right so you say a new class path XML application context and you mention your employee beam dot XML over here so the way you are saying factory dot get of B right so here what we were doing was we were doing factory dot get of B so rather than saying factory I will say context so that's one difference right and now you run the code again you get the same output rights you get the same output but there is one fundamental difference over here so let's try to comment out these two guys let's not use the beam pathway so the output is acid same right so either you've been fat free or you the application context now try to comment out these instructions and rerun your code right what you find is that application context will create the objects for you even though you don't request but the beam factory will create the objects only when you are going to request for it so spring inversion of control says that you don't create objects objects will be constructed by the spring core container so there are two api's the first one is the bean factory second one is the application context application context is built on top of bean factory so bean factory constructs the objects when you request for it by calling the gate bean method whereas application context will construct the objects even though you don't request for them now that's one major difference between your application context versus the bean factory so guys I hope you got the key difference between both the api's so once we are done right so guys your files need to be loaded and then you need to run your go back right so IOC container it has various features now so construction of the objects managing the entire lifecycle of the object is not the heading of the developer now so it's again spring core layer so just like the slow red container you got the spring container right so the way servlet container manages the lifecycle of a servlet in a similar way spring container is going to manipulate cycle of your object then wiring them together right so there might be dependencies which we can link up and configuration means the key value pair which you have already mentioned right so you just need to configure your object in the XML file so there are various features of ioc container now right the spring IOC container by using a Java POJO class and the configuration metadata produces a fully configured and an executable system or an application right so what we do we give the metadata and the poser information to the spring container right so if you can notice over here so what we have given we have given the XML file that's like metadata and we are mentioning what is the type right so the information is given to the spring container and the final result will be that will be having the object which you can use in your application right so let's see once again so we got two types of ioc containers one is the bean factory other one is the application context bean factory will construct the object when you request for them whereas application context will construct the object even if you will not request for them right so that's the major difference other than that bean factory is the simplest container so that we can provide basic support for example dependency injection whereas application context is built on top of bean factory and the container will add more enterprise specific functionalities right so this is something which is more relevant to use rather than the bean factory all right so what is a bean now so guys bean is nothing it's a Java object morones right so there are the objects which form the backbone of our application why we need object we need object to contain data and these objects within a spring framework are managed by the spring IOC container itself right now that's the point of relaxation further spring IOC container will be the one who will be responsible to instantiate assemble or manage the lifecycle of your entire bean object the configuration metadata and the bean is given to the spring container so that we can get the constructed bean object right so that's something which is in line to your Abilene object so what is happening over here IOC containers gonna take the metadata right and thereafter it's gonna give us the B let's understand the life cycle of a mean so what happens the core container will construct the object and that will be responsible even to destroy the object right so we don't destroy the objects now so it is sanshi ation then populating the properties first of all constructing the object then setting the data into the object so setting of the data into the object is done by the setter methods so if you don't have the setter method in your beam class this IOC will not be possible so you need to have setter methods right so once we are done with that part so all the center methods are called then your applications you know processing will happen by API is the code container right so that's gonna come into action so any post-processing or any properties which you need to set you can do that so you can have your custom in it and you can have your you know kind of post processor available in your beans we will see that and once the bean is ready to use right the container is finally shut down and your bean will be destroyed so let's try to see one basic life cycle implementation of this whole process so what are you gonna do over here in my employee I'm going to write two methods right so let's see when I say public void my in it so I'm gonna say C so object initialized and I'm gonna write one more method so this goes like my destroyed so I'm gonna say object destroyed now in the employee beam over here I'm just gonna comment out this second eye right so let's try to use only one line send wanna say init method that goes like my in it and a destroy after it which goes like my destroy so you can manage the lifecycle of the beam right by writing your own in it and destroy methods so here what we have done we have mentioned that my in it is the initial method where the object initialization can be done and destruction method is my destroyed so you mentioned it over here coming to the client now so let's I'm just gonna comment out this guy as well so from my application context let's try to run the code and now what you see is that once the constructor is called thereafter your my init method is getting executed and then you can use your employee object other than that what we can do we can close this application context so that all the objects can be destroyed so to do that what I'm going to do I'm just gonna give it the reference to this guy class path XML application context right so I'm just gonna say let's downcast it so I'm gonna downcast with context over here and thereafter I'm gonna say CX T dot close so I'm just gonna close the context right so close the context I'm just gonna say shutdown dear spring code containers so when the container is gonna shut down its gonna delete all the objects available right so let's try to run the code now and now what we can see is that object is destroy so your object destruction happens when you are explicitly closing the context so otherwise the object destruction will totally be dependent on your spring core container right so guys that's how you got the lifecycle of your spring core container right so let's now try to see some more demo on a beans through a dependency injection guys wait so let's see what is meant by the term dependency injection so before we understand the term dependency injection we need to understand the term what is meant by coupling right so let's see if that coupling theory first of all consider that I got this class employed right so we got a class employee over here now and employees having one t ID then employee had one name but for the employee address I'm gonna create another class right so it's a multi-value thing so I'll just have you know kind of what's the city then what's the state what exactly is the zip code right now this is where I am going to create a has a relationship and I'm gonna say employee has an address right so you got this relationship available over here so it's it's basically where we have gotta has a relationship associated with the employee and address so employee has an address now now how you are going to create the employee object you will say employee is a new employee so within the constructor of this guy employee what I'm gonna do I'm gonna say a ID is 0 e name is again let's say some default value like not a label and address can go like a new address so here what we have done we have done de object construction right whenever I'm doing an object construction over here so this is a high dependency dependency what is happening over here so construction of address object relies on construction of employee object now that's the scenario so what is happening over here right so guys what is happening over here then you ride the object of employee then the object of address will be constructed it means that there is a dependency of your address object which is getting constructed in the constructor of the employee object it means that until and unless you don't create the employee object you can't consider the address object such a code write such a statement leads to high coupling we need to lower down the coupling first of all so we don't do it in this way so what we do we rather create one constructor which takes a reference to the address as input and thereafter what we can do we can just assign this guy like this right or what you can do you can create one setter and you mention this address over here and you say address is addressed so over here you construct the employee object and thereafter you can create an address object separately now you are constructing the address object separately right so there is a difference now and what I can do I can say e dot set alpha you know this address as in a or I can just truly create one another employee object and within the constructor of this employee object I can pass this so in this way your employee object is constructed separately right and your address object is constructed separately so later on you can use them so you have reduced the dependency and we have achieved something on as loose coupling so for such a concept when we have a dependency so this is a dependency right that employee has a dependency on address dependency can be justified through constructor or through setup and correspondingly in the spring way right because in the spring way what we do we use an XML file right we use inversion of control so we got something known as dependency injection either through constructed or said er right so that's what the topic is so in the topic dependency injection is basically focusing on how we can reduce the coupling and thereafter we can use this key concept for our Spring Framework so guys it's a design pattern which will remove the dependency from the programming code that's going to make the application easy to manage and test dependency injection makes our programming code loosely coupled which means any change in the implementation of one will not affect the other right so that's what we need to achieve so in the XML file and in the Java file right so we got two of the things right so you wanna use XML file to achieve dependency injection right and you will be giving it to the Java so it can be done by two ways either by constructor or by the setter method for the constructor we need to use a constructor Arg and for set method we just use the property itself right so guys let's see how we can do it let's try to create first of all a different class and link it all together and let's see how we can do it so what I'm going to do I'm gonna write one more class so here we are with one more class and this class goes like a dress and now let's have three things one is the city then I'm gonna write one state and finally I'm gonna mention one is it good right so these are the attributes for my address right so let's say that these are the attributes and let's have the methods so you can have one default constructor you can have a parametrized constructor you say source generate a constructor using the fields so here we are with parametrized constructor let's say source then rate of getters and setters so these are required when you are writing a spring IOC code right so without get a set it's not possible right you must do this part then finally you temperate one new string so that if you want to display the data to the user now coming back in my employee so rather than having this guy address over here wait so I'm just gonna take it like address and address so let's try to take it in this way so what I'm going to do I'm just gonna write the methods again so let's try to say source generate getters and setters right and all right so here we are I don't know why it took so space but let me just correct if I the code guys alright so here we are with the crews and setters and one thing which I'm going to do is I'm just going to write one employee constructed with the address as an input right so let's say this of address assigned to address right we got the same a name so are you gonna use it in this way so this is like constructor injection right and here if you can see we got a setter injection so how this injection can be given right that we need to figure out so we got employee having an address right and address is a separate class so come to your employee bean so the very first thing which I'm gonna do over here is I'm gonna write my beam that's like address so go dot ed we cannot address right so you're gonna create one address so we got City then state and finally you got a zip code so let's say city goes like bangaluru then state goes like Karnataka and then zip code goes like five to some zeroes and one right so let's let's say that this is an address right so we have mapped the address as in city state and zip code over here you got city state on is it good now over here in my employee right so what I'm gonna do now I'm gonna use this dino Ness constructor arc and I'm gonna say refer ad RS so whatever this address object is available right area is so I'm gonna give the reference a DRS over here and in my employee the constructor injection will take place right and let's come to the client and now let's try to get this guy m1 and let's try to say print of e1 so when I'm gonna do even printing so in my II 1 so here if you can see let's create one to string you don't have a to string over here so let me generate one two three so this two string is gonna print the address right so it's one up in the address the address to string will be executed over here let's come back and let's run the code so just give me a moment I think some basic synthetical mistake yeah so here we are right we just need to close this let's run the code once again here we aren't ice fight so object initialized and if you can see now you got the employee one details here we are the details with the employee and here we are with the details of the address object so this is as a dependency injection through the constructor so if you don't want to use constructor addiction what we can do we can also give it as in so set-apart goes like this now this dependency injection is happening on the setter part right when you use the property it's gone I use the set address method that's like setter injection right the output will be same anyways but the concept of utilizing the thing right it's changed now so output is same so there are two major ways to fulfill your dependency part right either you can achieve it through constructor or you can achieve it to set up let's look at the agenda for the session first and foremost I will give you a brief overview on spring AOP and why do we need aspect oriented programming next we will discuss some core concepts of aspect oriented programming once you understand the basics of ARP then we will dive into the practical part and learn how to create spring AOP along with examples after that we will see different advice types in AOP like after before around etc and finally I will wrap up the session by explaining you about aspectj example without any further ado let's get straight into the module first we will understand what a spring AOP that is aspect-oriented programming spring AOP enables aspect-oriented programming in spring applications in ARP aspects enable the modularization of concerns like transaction management logging or security that cut across multiple types and objects and AOP provides the way to dynamically add the cross-cutting concerns before after or around the actual logic using simple pluggable configurations it makes easy to maintain the code and the present as well as future you can add and remove concerns without recompiling the complete source code simply by changing the configuration files so why do we need this ARP as you all know it provides the pluggable way to dynamically add the additional concern before after or around the actual logic suppose say there are 10 methods as shown in the figure and there are 5 methods that starts from 8 that is a 1 - Fi and 2 methods from B and three that starts from C so first let's understand the scenario here what I have to do is I have to maintain the log and send the notification after calling the method that starts from a so what you might think that will be the problem without using a or P first thing we can call the methods that maintains log and sends notification that starts with a in such scenario what happens we need to write the code in all the five methods correct but if in future client says I don't have to send the notification you need to change all the methods and that leads to maintenance problem and that's exactly why we need a or P so what is the solution that we have with a or P here we don't have to call the methods from the method but we can define the additional concern like maintaining log sending notification etc later I'll tell you how to do that and future if Klein says to remove the notifier functionality we need to change only in the XML file so maintenance is very easy in aspect oriented programming so that's the whole concept why we need aspect oriented programming and sprints now let's move further and understand some core concepts of AOP there are many as such like aspect joint point advise point cut target object weaving proxy etc so first we'll see what is aspect an aspect is a class that implements a java enterprise application concerns which cuts through multiple classes like transaction management security etc and aspects can be a normal class that is configured through spring XML configuration so what does advise advises are the actual actions taken for a particular joint point basically they are the methods that gets executed when a certain joint point needs a matching point cut in the application so here you can see you have the program execution and these are the join points and the point cut and there we have the aspect code that is the advice so what is a joint point it's a point in the program such as method execution exception handling etc yes and that is also a candidate point in the program execution where an aspect can be plugged in so what is the point cut point cuts are the expressions that are matched with the join points to determine whether advice needs to be executed or not so again we have target object that is the objects on which advices are applied and in spring a subclass is created at runtime fair the target method is overridden and advices are included based on their configuration and also we have proxy that is a proxy which is an object that is created after applying advice to the target object in terms of client the objects the target object and the proxy object are all same so what is weaving it is a process of linking an aspect with other application types or objects to create an advised object so this is all about the core AOP concepts having understood this let's move further and see what are the steps required to create a opie and how join point advice aspect point cut everything are apply so these are the four main steps very first thing is that we need maven dependencies in actual you need to create a maven project and add all the dependencies next you must write aspect and point cut expressions after this you have to write the methods that is join points and finally you should write the main class and run the application so for maven dependencies just type MV and repository click on the first link so in this maven repository you can find all your jar files so basically say I want spring jar so you can see here you have spring web spring be in score context say I want spring context and the latest release will be five point one point three here you can see the dependency code being present over here you have to just copy this and this dependency code is required to import all the jars of spring context you have to paste it in the palm dot XML file open your eclipse that is for Enterprise Edition click on new and give here a smelvin choose maven project click Next I love it to use workspace default location only here you can see there are so many Archie types like for plugin plugin start profile you have 4 web app side side simple but I want to choose Quick Start as I'm not running anything on the web click on next and now it will say group ideas combat at Eureka and next it will ask for artefact ID you can give artefact ID depending on your project I'll give it like spring - AOP finish so here you can see you caught spring AOP project file click on that and click on the pondered XML file so here you can see your group ID your artifact idea version your packaging that is a jar package and the name of the project is spring and the URL will be your maven Apache dot o-r-g because it is a Apache a product and you can see here the dependencies of simple j-unit that will be present in your maven dependencies as I already told you I have not imported this jar file but it has already been imported internally from the remote location so you can see it is present and users and Neha and followed by this path as you know I have copied the spring jar files correct so I will tell you one thing here for spring you need to go on importing all the jar files finding them in the website and then importing it here again it's a very complicated task right what we'll do is the code that is being present over here that is a dependencies will copy that and we will just paste it over here and save this it will take a little while so here you can see as I just paste the dependency code for spring context it imported all the jar file status context AOP beans core JCL expression everything it's very simple just copying the code from maven repository and coming over here and pasting it and convert xml simple thing again say I've worn the code for AOP SOTA showing here that you have to spring your pitcher you can again copy it and paste it and say you've worn a speck Shh on so when you type aspectj jar you can see here you have aspectj beaver runtime jrt tools everything so you need to copy and paste all these over there the latest release coppy again pace it after this dependency so even that came over here now again I warned aspectj run time so even a SPECT gr thr got imported over here similarly I will import all the other jars as well so basically these are the required jar files for a spring program again you can just click on help click on marketplace and the search tab just give a Springs so here you can see spring tools three add-ons you have to install this it is very essential because it has to configure all the add-ons with Eclipse so you have to install this I have already installed so it is saying as installed so basically you can see here I have a spring dashboard over here so after installing this this should appear I hope that you understood about dependencies in everything from maven repository now let's see how to create a basic customer service class and print few methods for demonstration I have already created that so as the soap on that project so this should be your directory structure as you can see inside the source there is main and inside mean I have Java and comm dot ed Eureka is a group ID and at Eureka is the artifact ID and I have three classes over here so first I will open customer service so here you can see I have used only two variables such as name and URL correct and I have used some setters and getters that a set name and set URL and then trying to print the URL and the name using print name and print you oil methods very simple class so you don't have to worry about this directory structure because by default it will take this way but the only thing that you have to worry about is your group ID your artefact ID etcetera and now in the resources I have customer dot XML file so these things you will get it from any of the website or anywhere you can just copy and paste this for Springs or else you can never note it down from here also no issues for that you can just pause it and you can just copy this thing from here and now what we have to do here is after writing this XML location and everything we have to configure bean in this we will write a bean class with ID name value etcetera so bean ID will be your customer service and class format goes like this that is your group ID artefact ID followed by the class name and the property name will be a name of your own choice I have given it as ed Eureka and the value and property URL will be our ed Eureka website you can give anything of your own choice apart from this I have done one more thing here that is I have applied before advice method it has to execute before the joint point methods and they are configured using before annotation mark I'll show you where I have configured at using before annotation mark so this will be your main class where you have declared your main method in this main method I have set application context and I've given the XML application context class path that will be your customer door XML because your XML file name is customer dot XML so you have to specify this and then forget beam method I'm giving it like a context get beam which will be your customer service proxy so this customer service proxy is being set over here that is your beam ID for before if you don't want to do before advise you can just simply delete this part and your output will be just at Eureka and followed by your website name but as I have already included before advice so I'm specifying the beam ID will be customer service proxy that which I have given in this file that is your get beam and this class will be from your factory beam and the property will be target and the reference will go to again customer service and your property name will be in scepters name and I have given value like something before beam method so what happens this before beam method will be executed before the username and your website here you can see I have used a class name that implements method before advice and I have used before method to print the message Sata's hijack simple save this don't get confused this is only for before advice a fedone before advice you can just run it without this and just removing this part of code but then as I have already used this before advice I'm showing you how it works so after that and your main class you can just simply print the customer name and customer URL so let's run the program and check the output so you can see here as I told without advice a just prints advocate and followed by your website so this is how basically your spring example books now will see different types of advices that will be your before advice after advice after returning from the advice and around the advice so as you already know before advice means it executes before the joint point that is your method and next after advise it is advice to execute after the joint point methods and next will be your after returning from the advice that is execute after the joint point completes and around the advice means that executes before and after the joint points and are configured using act around annotation mark now we'll see how before advice and after advice books all that you have to do is you have to make a small modification over here so just change this customer service proxy and in this all that you have to just do is include this bean configuration that's all so here you can see I have created a class called hijack after method so that it will display this message that is your hijacking after method beam so this message will be displayed after your join point ok because I have applied the advice after the join point so by default it will display it after the join point only and I have made some modifications and given here as customer service proxy and the customer at XML file I have declared the bean ID as hijack after method bean and given the class name that is your hijack after method class name and then the bean ID will be customer service proxy and the class name will be by default from the spring framework and then property name will be your target and the reference will be customer service so save this and when you run the program you can see the output over here first will be your customer name that is a breaker and then it is stilling hijack after method that is after method it's been hijacked and again it's telling the customer website and again you're getting the advice after method so this is how it works if you want it before I show you even how to do that as well nothing much change in this the only change that happens will be in hijack after beam method and in customer dot XML let's see how it occurs I've modified the same program so what I have done here is instead of giving hijack after method I have given it before method and I have changed implementation to before advice and in displaying that I said it friends before method okay save this and now go to customer dot XML wherein you have to change certain things I'll tell you what are they everything remains same you just have to give before over here and even over here as well save this again run the program so what your guests will be the output so as I told it appears before the method so you can see here it's appearing before the customer name and the customer URL so this is how before works and if you want for around as well I'll show you how I will again modify this file so this is my code in which I have given hijack around method and it implements method interceptor and then I'm passing the name and arguments and just telling like it has to print around the method again and try and catch block I'm trying to catch an exception if it does not occur now again go back to your customer dot XML file and change few things that will be your around over here as well and then here as well and again the same thing given in case of value you have to make it around save this as you can see I'm not recompiling the entire thing I'm just saving the files and I'm just executing that as I'm running the main class only this makes you understand it is very easy for maintaining purpose so when you run this let's check what will be the output for around the method so here you can see first what it is printing method arguments then it's telling get this hijack around the beam method and again it's displaying its hijack so what does happening here is it is getting hijacked around the name and URL and then it's selling to print the URL method arguments and then if there is anything that has to be trolled as an exception of destroying here as well so basically this is how you can apply the advices before after and around the joint points like that is around the methods I hope you're clear with this now let's move further and understand the aspectj annotations one of the key components of spring is the aspect oriented programming framework correct while spring IOC container does not depend on ARP meaning that you don't need to use AOP if you don't want to and you P complement spring IOC you to provide a very capable middleware solution just like the key unit of modularity in AOP is the class in AOP the unit of modularity is the aspect aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects and aspectj has grown into a complete and popular ARP framework and spring support that are written with aspectj annotations and since aspectj annotations are supported by more and more AFP frameworks your aspectj styles are more likely to be reused in ARP frameworks that support aspectj and now we'll see how we are implementing aspectj over here and for that we are using aspectj annotations so first again project dependencies spring beans enable the aspectj and then apply advises let's see how to do that so I have created three classes over here one is track one is operation and the final main class happens abroad Java so in case of track class I have applied the annotations that is at aspect at point cut that will be your point cut name and then I have applied before advice as well so this is how basically we apply the annotations and you can see here I have imported aspectj dot land or join point as annotation so this will be my track dot Java which is useful for tracking and then in the operation class I have message is invoked method M is in book and method K is as well in book so that will be your method K your method M and the message so finally I'm giving it like return return bounded on to return three and now coming to approach Java you can see I am calling the message I'm calling method M and I'm calling method key so in the application context I am just specifying the XML file that will be present in your resources so again it's similar and here your bean ID will be up being and your class will be this operation class and then again your one more bean ID will be track my being and your class name will be the strap that will be followed by your group ID and artifact ID as well simple so when it is calling this I'll just give you a hint as how the output occurs so first it says calling this message from this class and it goes back to your operation class and it says message method is invoked and then again it says I'm calling him and it again says method M is invoked and again it follows with K as well so I will save this and I will run the program let's wait for the output now as I told you it will tell calling message and it's a message ham is involved and then I'm calling method iam and again at court invoked and again I'm calling K and even K method as well got in book so basically this after Java will redirect to operation door Java class wherein it will call all these methods and then I have used execution of operation M that will be your point cut so it will print accordingly I hope you understood how point touched joined points and aspects everything can be applied and how advisors can also be applied before them joined point after the joined point around the join points etc in a world where we working with programming languages on a day-to-day basis all of us tend to search for methods and tricks to make our life easy right while dependency injection is one such technique which aims to help the developers code easily by providing dependencies of another object and I welcome you to the session on what is dependency injection so in the session you'll mainly focus on you mainly understand what is dependency injection and how you can use it so without wasting any further time let's take a look at the topics for today's session so we'll start today's session by understanding what is dependency injection and then I'll talk about inversion of control after that I'll tell you the different types of dependency injection and then tell you the benefits of dependency injection or maybe why you should use dependency injection finally I'll end the session by showing you how you can implement dependency injection using springboard now before I start with the session let me tell you that if you if you are interested in learning about the top trending technologies in today's market and then you want to scale up your career according to that you can go ahead and subscribe to our ad Rekha YouTube channel to get daily notified on the top trending technologies right so now since I've told you what topics I'm going to cover in today's session let's get started with today's session so what is dependency injection now if I have to tell you what dependency injection is you can just understand that you know dependency injection is the ability of an object to supply dependencies of another object now I'm pretty sure that you know you might not have understood anything with the technical terms over here right like I've just told you that you know it just supplies dependencies to the other object so let me just create the confusion for you before I tell you what dependency injection and programing means let me tell you what dependencies what do you understand by the term dependency in English it's something relying on something else right well that's the same in the case of programming also dependency in programming is an approach where a class uses specific functionalities of another class so for example if you can see on my screen I've considered two classes over here that is classy and Class B and let's say that you know Class A uses the functionalities of Class B and so when Class A uses the functionalities of Class B you can just understand to the fact that you know Class A has a dependency of Class B right now if you're using both these classes in Java then you might be knowing two a fact that you know you have to create an instance of Class B before the objects are being used by Class C right only if you create the instances of class P before the class who uses it only then it is said that you know Class A is dependent on Class B so I hope you understood what dependency in programming is right it's just that you know when you have two classes and then a specific class wants to use the functionalities of the other class then that is said to be dependency and then in case of Java if you want to use these classes then you first have to create the instance of the class B and only then Class A can use it now if I have to define dependency injection for you then you can understand dependency injection as process of creating an object for some other class and let the class directly use that particular dependency right now for example let's say you know a class ask the dependency injection for two objects right so what dependency injection will do is that you know according to the request of the class dependency injection will create two objects and then what will happen this dependency injection will just return those objects to that particular class right so I hope the flow is clear to you initially the class will ask the dependency injection for two objects and then the dependency injection will create two objects once it creates it just returns those objects back to the class now if you observe over here they were mainly three classes involved that is decline class injector class and the service class right well the client class is basically the class which is the dependent class and is dependent on the service class the service class is the class which provides a service to the client class and finally the injector class is responsible for injecting the service class object into the client class right so if you see the flow on my screen then you can see that you know the client class uses the service class and the injector class injects the dependency to the client class right so that's how basically dependency injection works guys it's really simple to understand you just have to understand that you know when you have various number of classes and then you want to use two functionalities of maybe some other class and maybe you don't want to create objects manually that's where dependency injection comes into picture right so I hope you've understood what is dependency injection now next in the session let me take you to the principle based on which dependency injection came into picture well that is inversion of control so next in this tutorial on dependency injection let's talk about the principle behind which dependency injection came into picture so what is inversion of control so as I just mentioned inversion of control is a principle based on which the dependency injection is me also as the name suggests you can understand to the fact that you know inversion of control is basically used to invert different kinds of additional responsibilities of a class rather than the main responsibility so if I should explain you in simpler terms then you can just consider an example of maybe you know you know cooking right so what you can do is either you can cook cook for yourself right so you can cook food for yourself or what you can do is you can just open swiggy Zomato food panel any other website to order food right so basically what's happening over here is when you have the ability of the cook according to the ioc principle so that is basically according to the inversion of control principle you can invert the control so instead of yo cooking the food for yourself what you can do is you can just directly order the food from outside where you know you get the food at your doorstep right so basically this process of getting food delivered from outside at your doorstep and your doing nothing in that picture is basically inversion of control so whenever you have a class so basically if you consider yourself as a class and then your responsibility is to cook food right now what you're doing is you don't want to take care of this additional responsibility and then you're asking somebody else to do it for you right so you just order so basically you'd ask some other class to basically perform that functionality for you that is basically order food for yourself and then you just directly get the food at your doorstep right now in the scenario where you don't have to cook for yourself and instead you can just order the food directly and let the delivery executive deliver the food for you you're basically trying to avoid all the additional responsibilities of your class and then just trying to focus on the main responsibilities right so this is basically what is in version control guys in version control says that you know a class should not configure its dependency statically but should be configured by some of the class from outside also it mentions to the fact that you know a class should concentrate on fulfilling its main responsibilities maybe like you know a flow of an application or maybe not creating objects or how your application works and so on and it should try to avoid the additional responsibilities as far as possible so basically when you avoid additional responsibilities you have to make sure that you know somebody or the other may fulfill those responsibilities right so that is where dependency injection comes guys dependency injection takes care of those additional responsibilities and then helps a class to fulfill all kinds of responsibilities so guys that was the concept behind dependency injection so now that I've told you the principle behind dependency injection let me take you through the types of dependency injection so there are mainly three types of dependency injection now when I say types of dependency injection I mean that you know how you can implement dependency injection in your project files right so there are mainly three ways to do that either you set up a constructor injection or you set up a setter injection or an interface injection coming to the constructor injection basically in this type of injection the injector supplies the dependencies through the client class constructor coming to the setter injection in this type of injection the injector method injects the dependency to the setter method exposed by the client so the setter injection is also known as the property injection so it's basically where you play around with the getter setter methods coming to the interface injection in this type of injection the injector uses the interface to provide the dependency to the client class so basically the client must implement an interface that will expose a setter method which will then access the dependency so basically guys these are the three types of dependency injection that is the construct Injection the certain injection and the interface injection right so till now I hope that you know you have understood that dependency injection is responsible to create objects to understand which classes require those objects and finally provide those classes with the objects so now on that note let's take a look at the benefits of dependency injection so the benefits of dependency injection mainly also imply to why you should actually use dependency injection so let's understand the same so now before I list down the benefits of dependency injection let me tell you what is the need of dependency injection right so for example let's take an industry level project where you know they have a class of email service right now this particular class responsibility is to basically take care of the image received now what do you think will be the objects for this particular class it's quite obvious it will be the two email address two from the subject and the body and maybe many more but let's take these four into consideration now now let's say you know if the company wants to expand themselves and maybe they also want to save the text and the audio messages right now do you think this class with this the set of objects can save the text and the audio messages not only that do you think this class can you know take care of the complete responsibility of saving each and every factor of the text in the audio messages well the answer is no right now this is because obviously the email class cannot handle the parameters of the text and the audio messages now in such cases what you have to do is you have to basically recreate the class with this particular scenario so for example let's say that you know you want to save the text and the audio messages then you automatically have to recreate this email service class based on the parameters of those particular messages now this might be sounding quite simple to you guys but if you take the same example on an industry level it's quite cumbersome like you know every time there's an update or maybe every time the company wants to do something new they cannot simply just sit and recreate the classes right now to avoid such scenarios what what they can simply do is they can use the dependency injection so with dependency injection what they can make sure is that you know they can change the objects at the runtime so in this way you don't have to basically recreate the class and obviously then you just have to you obviously based on whatever type of messages that you want to save you can just change the objects at the runtime so that's where basically dependency injection plays a role in industry level wherever you see a scenario free creating the class and maybe you don't want to recreate the class then you can just use the dependency injection and just change the objects at the runtime so that you know you can use the objects based on whatever query that you have so I hope that you know you've understood why you should use dependency injection so now if I have to just list down the benefits of dependency injection then let me tell you that you know it enables loose couplings so basically it enables a really easy way to interconnect the components right so as you might have heard in my previous explanation I told that you know there were three classes right the client the service and the injector class so with dependency injection you can interconnect the components really easily coming to the second benefit that is application can be extended easily I think this point you might have understood by now when I gave the example of email service class right so basically you can extend the applications by you know where you don't have to recreate the classes you can just use those classes only and maybe you can just have to change the objects then coming to the third benefit that is unit testing so unit testing is made much easier because whatever product that you have maybe if you want to just test then it becomes really easy and finally the reduction of boilerplate code as you know the dependency injection understands to the fact that you know it has to create an object and then it knows which classes require those objects and it returns those objects right so you don't have to mention a lot of code for your project-based look for this particular factor right so guys these were the benefits of dependency injection so I hope that you've understood what is dependency injection what was the principle behind it what are the types behind dependency injection and finally the benefits of dependency injection right so now in this session let's look into how we can implement dependency injection using spring boot right so let's get started with today's hands-on so to start today's hands-on you first have to open your Eclipse IDE so if you want to know how you want to open Eclipse ID or maybe how you want to download Eclipse IDE you can refer to my video on installing Eclipse apart from that I'm going to use spring boot right so for that I've installed the software known as spring to sue so to do that why there is I went to help I went to excerpts marketplace after that what i did is i search for spring tool suit and let me just search and then you can see that you have already installed spring to suit right so when you want to use spring boot plugins into your Eclipse IDE you can just download this particular ID so you just have to click on this install button and then automatically you'll see that here in spring - suit is getting installed right so since I have already installed into my system I'm not gonna install it over here again now what I'm going to do is I'm going to start by creating a spring starter project so for that what I'll do is I'll right click over here go to new and let's say I choose project over here I'll choose spring start a project click on next and let's say you know we mention the name of the project so let's say we mention example right after that I let all the other stuff be same like you know the Java version group artifact or not because that's not what we are focusing on in the session after that I'll click on next and then I'll click on finish right once I click on finish you'll see automatically that you know a project has been created now if I just open this project you'll see various files suggest main Java main the source is test Java and so on so the main and dependencies file basically as all the dependencies stored for your project the Java file will basically have a package that we just created that was example and then inside that we have an example application run Java file right so this is basically a main file which will execute the complete project so let's wait for it to open all right so now let me just zoom in alright so as you can see on my screen this is basically my application file which I'm going to execute right now initially what I'm going to do is I'm going to create another class in the same package and then what I'm going to do is I'm going to connect these classes so for that what I'll do is to go back to the package over here right click over here go to new and choose class over here I'll mention the class name so let's say I mentioned customers and then I'll click on finish so this is basically how you create a class guys so what I've done is in the same package where the application file is present I've created another class known as customer right now what I'll do is I'll basically add few objects for this particular class let's say you know we add the customer ID customer name and course name so for that what I mention is I'll mention private int let's say cast ID let's say we mentioned private Spring let's say cast name and then private strength course name right so these are basically the three objects that I've created inside the customer class right now what I'll do is I'll generate the getter setter methods for all these objects so I'll just select all of them right click over your co2 source and then I'll choose the option of generate getter and setter methods right after that I'll select all and then click on generate so once I click on generate you can clearly see that you know automatically the getter and the setter methods are created for all these particular objects so I'll just put these objects at the starting of the code so that it's easy to understand until now I have two what I've done is I've created a project and then I have created another class known as customers into the same package right now consider a scenario where you have to create an object for the customers and you don't want to do it manually right so initially what I did was I created the objects manually now let's say you know you don't want to do it manually now in such scenario basically as I told you before you have to use the dependency injection to get the objects wherever and whenever you want right so next let's look into how you can use dependency injection over here so initially what I'm going to do is I'm going to go back to my application file over here and then what I'll do is I'll basically change this line so what's happening in this line what's basically happening is that you know a spring container gets created on this particular line and then it basically returns an object right so over here what I'm going to do is I'm going to change this line to configurable so let me just type in configurable application context and then I'll mention context is equal to let this be the same like a spring application run example application or class arguments right and now if you see an error that's because we have to import all right so I'm just gonna import this particular framework and then you'll automatically see that the error has gone so basically what's happening over here is that you know we got a spring container right so basically in this line of run a spring container gets created and now whatever is going to be present in this particular container is known as spring bean so basically what you have to do is whatever objects that you want to return will be known as spring bean and they will be present in this particular container so over here what I'm doing is I'm basically returning the context and then I'm initializing the container and in this container I'm going to mention all the objects right now which objects have to be returned or maybe which objects that you want to be present in spring container is what you have to mention so to do that what you will do is let's type in enter and then on the next line I'll mention customers see that is basically an object I am taking and then I'm mentioning context dot get beam right and then what I'll mention is I mention the class name right so that is customers dot class right after that I'll just end with a semicolon right so basically what I mentioning over here is I've initialized the spring container now I am telling what has to be present in the spring container that is basically I want an object of the customer class that we just created so that is what this particular line means guys now to check whether you know this is working or not what you can simply do is you can go back to the customers class and then let's say you know we add a new method let's say public void let's say we mention the method display and then let's say we put in we just print something so let's say we just print object created successfully right right and then I'll just end with a semicolon now what I'll do is basically I'll use this particular method into the application class so what I'll do is I'll go back to my application class and then I'd mentioned C dot display right so basically I'm using the method so what happens is you're calling the object of the customers class with a reference of the display method right so this is basically the object of the customer class and then I'm calling this particular object with a reference of this particular method display that I just created over here now let's see when we execute what what do we get so I'll just right click over here go to run as and choose spring putas and then I'll just click on OK all right now once this executes you see an error that maybe you see an exception that you know no such beam definition exception right so basically no qualifying beam of the type comrad example the demo door customers is available right now this is because you know you have created a class status customers and then you have an application class but you have not defined the class to be a spring beam right so what I mean by that is you have initialized the spring container but you've not defined the object of the customer class to be a spring beam right so to initialize that particular object to be a spring beam what you have to do is you have to go back to your customers class and then you just have to use the annotation component so this is basically a spring board annotation and then you just have to import right so once you import you see that you know there is no error so with the help of this annotation component what you're doing is you're telling the spring container that we need an object of the customer class and then when you use this annotation component the spring container automatically understands after that when you go back to the application class and then when you mention customer C is equal to context or cat P in custom class the compiler will automatically understand that you know it has to check whether there is a customer pane available or not if there is a customer being available it's going to return that particular objects method right so basically you'll see an output that we mentioned over here that is basically object created successfully so if the beam is available then the Spring Framework is injecting the customer object into your application right so basically this object is created by the Spring Framework which can be further used into your application so now let's just execute and see so if I just right click over here again go to run as and choose spring boot app just click on ok you'll see an output that you know object Lee it is successfully right so with this what I mean that you know automatically the spring framework has created an object so basically the compiler has checked whether there is a customer beam available or not if the beam is available the spring framework is injecting this particular customer object into your application right so when something is injecting into your application and then you don't have to create it what's happening that's what is dependency injection right so that is basically this is how you can implement dependency injection using spring food now if you have to just extend this demo and then show you how you can implement dependency injection using auto white annotation what you can do is you can create another class so let's say I create another class so I got a new over here and choose class and let's say we mention it to be technologies right and then I click on finish once I click on finish you'll see that you know automatically the class is created so let's say we create objects of Technology class let's say I create tech ID and text name right so I'll mention private end tech ID and then I mentioned private strength tech name now let's say you know we again just mentioned that getter and the setter methods for these particular parameters so I'll right-click over here go to source and then choose to generate getter and setter methods select all and click on generate write so automatically you'll see that you know the getter and the setter methods have got generated right so I'll just put these particular parameters at the top right so now next similarly to the customer class let's say I create a method so I'll just mention public void and let's say Tech right and in this particular method let's say I mention system dot out dot println and then I mentioned successful right and then I just end it with a semicolon right so basically this is how your code looks what I've done is I've created a new class technologies and then it has two objects or you can say parameters that describe it are int that is basically tech ID and tech name and then finally I created a method with the method name tech name which means successful now let's take a scenario where you want to call the tech method into the customer class right so basically what I'm saying is I want to call this particular method to the customer class then to do that what you have to do is you basically have to create an object of the technologies class so what you'll do is you'll go back to customers class over here and then you'll mention private technologies that is basically my class name and let's say I mentioned Tech TT right so this is basically my object that I've created for the technology class which I've mentioned in my customer class right now once I'm done with this let's say I just again get the getter and the setter method so I'll just go to source over here generate and choose generate getter and setter methods and click on generate automatically you see that you know the methods have got generated next what I want to do is I want to use the tech method right so basically that was the method that I created in the technologies class and then I want to use that particular method so to do that what I'll do is I'll go to the customers class and under the display method of this particular class I mentioned tech detail and then I mentioned thick right so this is basically the method that I created and this is the tech detail is basically the object that I created in the customer class for the technologies class right so that's what you can see over here where is it sorry I skipped it yeah this is basically the object right so I'll just put it over here so that there's no confusion right now to make sure that you know the tech detail object is instantiated you have to mention the added component in the technologies class that stands the same again as I previously mentioned basically you have a spring container and then you want to make sure that you know a spring beam is instantiated so that you know whatever object you want to return is returned right so for that I'll mention component that is basically the component annotation and then I'll import the required packages now let's say you know we execute this project so till now it looks fine to your eyes right we have created three classes that is basically our application is the main file and then we have created two other classes that is the customer class and the technologies class now what we have done is in the technologies class we have we have a method that is known as Tech and then I want to use that particular tech method into the customers class so to do that what I've done is I've created an object of the technologies class that is to take detail and then I've generated getter and setter methods with that after that what I did is I just used this particular object and I just created with a method name right so it looks fine to your eyes right so let's see what happens now if I just execute this so for that I'll go to one as alright I'll go over here I'll go to run as and choose spring boot app and then I'll click on OK now when you execute the project you see an error that is basically nullpointerexception right so that's basically coming because you know now the customer class is dependent on the technology's class and yet to the fact the customer class doesn't know the existence of technology's class right so what I'm saying is that you know class a is dependent on Class B but a class it doesn't know the existence of class B so you'll have to make sure that you know the class egg gets to know the existence of Class B right so basically to enable the customer class to recognize the technologies class you have to insert the add great auto wide annotation into the customers class I'll just mention a great auto wired over here and then I'll just import the required packages so once I include this annotation add the add read Auto ID what's happening is basically that you know you have a spring container and then you have an object of the class that is basically the customer class now the customer class is dependent on the technology's class and the customer class doesn't know the existence of Technology's class right so basically when you insert this particular annotation that is a great or two wired automatically the customer class comes to know the existence of the technologies class so the dependency of one class to other class is initialized and automatically when you just execute this particular project so you'll see an output of object later successfully and also the second method which prints out successful right so let's just execute this particular project so when I execute this particular project you'll see an output that you know object created successfully and successful right so that's basically how the dependency between classes is fulfilled over here the add great auto wide annotation played a role to make class a realize the existence of class B and the added component annotation is basically used to make sure that you know the spring container gets its spring bean and automatically the objects are created without you doing it manually [Music] in the session I will talk about how Spring Framework integrates with Model View and controller and I also suggest you to subscribe to add Eureka channel to QuickStart your learning and also has the bell icon to get the notifications for the latest update on the channel so without wasting any further time let's get straight into the module first I will tell you what a spring MVC and after that I will talk about the spring model-view-controller framework and the workflow of the framework followed by that I will talk about how to create a spring MVC project and how to include all the dependencies and how exactly it works with the help of an example and finally I will wrap up the session by telling you some of the advantages of spring MVC framework now without wasting any further time let's get straight into the module first let's see what a spring MVC so spring MVC is a Java framework which is used to build web applications it follows the model-view-controller design pattern not just that it also implements all the basic features of a core spring framework like inversion of control dependency injection F etc so now let's see what spring MVC provides us it also provides a dignified solution to use MVC in the spring framework with the help of a dispatcher servlet so what is this patch a servlet so dispatcher soffit is a class that receives the incoming requests and maps it to the right resource such as controller model and view so as you all might be aware that every web application integrates with Spring Framework because it does not require web server activation and bit spring and they see this web server activation support as built-in and you are not bound to any container lifecycle that you need to manipulate and that's why every one of them uses spring MVC web application on a spring tool suit now with this let's move further and see the models view and controller framework offspring so this is a model view and controller spring framework and it comprised of a web browser a front controller a model and view so now let me explain you all these components in detail so a model contains the core data of the application data can either be a single object or a group of objects and you have a controller it contains the business logic of an application you can use add controller annotation to mark this class as a controller so if you have a computer system and if you want to name which part of your computer is the controller then I can say CPU as a controller so basically all the work that is done by CPU in a computer is done by a controller and the spring MVC framework so I hope you understood what exactly it is and you have a view this is used to represent the information in a particular format like in our excel format and you can also use JSP that is your Java server pages to create a view page if I have to again compare a computer system and a spring MVC so I can say easily that the monitor or the display that you get is a view right and you have a friend controller and this the dispatcher servlet class works as a front controller so now let's see how internally spring integrates with the Model View and controller approach so you can see first when a request is sent to the dispatcher servlet the request is also sent from dispatcher servlet to handler mapping to controller to view resolver and to view so when a request is sent to a dispatcher servlet the request is sent to handler mapping and a controller so when it comes to controller the controller functions internally and sends the response that will be a model and a few back to dispatcher servlet and then this is patch a servlet sense the view to a view resolver and when it gets resolved in the view resolver functionality then it will be presented as a view so after all these functionalities then you will get the output right so basically the incoming requests are obstructed by the dispatcher servlet that works as a front controller and the dispatcher servlet gets an entry off handler mapping from the XML file and forwards the request to the controller and after that the controller returns an object of model and view and finally the dispatcher servlet checks the entry of the view resolver in the XML file and then invokes the specified view component so that's exactly how it works okay now let's move further and see a small example of spring MVC so there are various steps that you have to understand first you need to create a new maven project and add all the dependencies then you have to create a controller class and configure the web.xml file and after that you have to define the beam in the XML file and then you need to create a JSP page and execute the program right so now let's see how it works so as I have already told you first need to create a maven project in Eclipse just go to file click on new choose maven project just click on next and in this case you have to use maven Archie type web app because you are creating a web application and not a simple project so that is the reason you have to use maven Archie type web app then you have to click on next then you have to provide your artifact ID your group ID and everything when you present your artifact ID as you know spring this turns out to be your package name so you have to click on finish if you wish to know how to create a maven project and you know if you want to know how to configure your Spring Framework you can check out the video on our YouTube playlist on Spring Framework and you will understand all the fundamentals as I have already created a project called spring MVC you can see here it's a maven project it comprised of a deployment descriptor which is a Archie type created web application you have the Java resources as well and have Java libraries and I also suggest you to configure your Apache Tomcat server because I have to run a project on a server and that's the reason it is very essential again if you wish to know about this you can check out the video on servlet and JSP tutorial and you will understand even this as well and I have the maven dependencies which is very important and this is the source file inside I have my main inside that I have two folders that is a resource and a third back so inside the resource I have comm ed you Rekha and a controller that is edition dot Java and again I have a web app and that I have web INF the one is the bean configuration file and the other one is my web dot XML file for navigating through the web page and then I have a target again this comprise of web resource and I have my maven manifest so you can see here this is the structure that it should maintain and first thing is I have to configure my pond but XML file so you can see here this is a palm dot XML file as a page object model XML file and it comprised of all the dependencies so the dependencies RS require is the Spring Framework web MVC you need the my sequel connector char you need Java servlet and you need spring context and that's all so you simply have to navigate through you know MD and repository and you have to just copy the maven code that is the dependency code for example say I won't spring MVC I'll search for this so this is what I require I'll click on this and I'll get the latest release so you can see there are various options so you have a jar so you have a maven dependency code you have Gradle you have SBT grape build our menu thing but as I'm using maven I just have to you know select this copy and paste it in my pawn dot XML file very simple that's what I bet so these are the various jar files that I need so have a look at these Charles and also when I click on this project I'll choose build path and configure build path so you can see I have the maven dependency I have Apache Tomcat library and also I have the Chiara system apply and close so make sure your jar files your dependencies everything is configured before you jump into executing a project ok so after this the next step what you have to do is you have to create a controller class in order to create a controller class and using two annotation setters add controller and add annotation right so add controller marks this class as a controller and at request mapping is used to map the class with a specified URL so this URL that I'm using will refer to the view page suckers index dot JSP this is my JSP view page what I am doing and just giving welcome to Eureka so this mess say it should be executed on the browser using the soap where that is at Uncas / right so now let's see how I have configured is controller so I'm importing the controller and the request mapping package inside that I've just created a method called public string display and return the index index refers to this so it will return this message on if a browser so it does not end here I also have to configure the be in class and the web dot XML file so these are the configuration of all the be in class and defining the bean in an XML file is necessary to specify the view components in this the context components can element defines the base package where dispatcher servlet will search the controller class so this defines the base package and the dispatcher servlet will handle the controller class and I have a web dot XML file as well in this what I'm doing I'm specifying the servlet class which is a dispatcher servlet that acts as a front controller and the spring MVC and all the incoming requests for the HTML file will be forwarded to the dispatcher subject so you can see I am giving the display name that is a web app and the servlet name will be spring inside the servlet and the servlet class will be this because it's a dispatcher servlet that I have already told you and inside the servlet mapping I have to specify the servlet mappings as well so the URL pattern is this which is the same and my controller write the request mapping is used to match with the URL pattern so that's what I'm doing here and I'm also giving the servlet name so now when I run this project as run as run on sober let's see what happens it's asking for the server I'll choose server 9 and I'll click on the next this is my project and finish you can see that your software has started so you can see that it is loading all the definitions from servlet context and it's initializing as well it will take a while that's fine there's no problem in that so you can see here the servlet was up and running and it also navigated through the browser and it displayed the output that is welcome to Eddie Raycom message on the browser right so that's exactly how it works and this is how you need to configure your controller class your beam configuration file and your view page such as a JSP page so next let's see one more example where I will tell you how to enter the user name and then it will display the data as well in a particular format I have already configured a project for that as well and as I have already told you you need all the dependencies so I have the Java resources you have your maven dependencies I have already configured the format XML file and everything so first let's see the controller class so as I have already told you that controller annotation is used to mark the class as a controller and the request mapping will refer to the URL pattern from the web write xml file and an using the method called request method get to get the values from the view page ok and it's very simple first it will refer to the method and it will say the home page requested and it will print the local and then it will get the date format and this is the method how you need to configure the date format and finally it will display the time on the server whatever the time is it will display the time on the server and I have one more file here or username so this is used to return the username that you are going to enter on the browser so this is all about the controller first let's see how your web content file looks like so I have my view pages that is user dot JSP and home dot JSP so inside the Hornet JSP page in my h1 tag I have written hello please enter the username to log in and after you enter the username it will go to user dot JSP and it will print hi and followed by your username so first you have to use a dollar and followed by the username which implies your username will be displayed it will print the time on the server so again this is the format and here I am using the form action because I want to input the text that is a username and submit the value so for that reason I'm using of form action once at logins it will say hello followed by the username so these requests will go to your controller class so the controller first will return the username and then it will display the date based on this format and inside the URL pattern that is followed by this method it will say username page has requested and once it gets the response it will return back the user simple so this is how you need to configure your view and controller so now your model will be the pin class so you have to define your bean that has a dispatcher servlet and you have to define the base package sutter's combat eddie record or spring which i have defined it before and also there is a bean property you have to define views and your controllers in this class once this is configured you need to go back to your web rhotic symbol file and configure for launching the webpage and navigating to the particular request website and then performing the actions on the web page so again this is a parameter name and this is a servlet mapping that I have defined and this is a URL pattern so once when I execute the program let's see how it works again I'll choose the same server that as Tomcat 9.0 and I'll run the example so as I told you it first says hello please enter the username to log in and it will display the time on the server so I'll enter the username as ed Eureka and hit the login button and it's selling high followed by Adi Rekha simple again if I want to change the username and give something like this hit the enter button it will lucky navigate to this and you can see here on the server info first it is selling the homepage was requested and then two times I have requested the user page because two different times I gave two different username that's a reason now let's move further and see some of the advantages of spring MVC first it is lightweight a spring is a lightweight framework there will not be any performance issues and spring based web application next high productive spring MVC can boost a development process and that's a reason it is highly productive now talking about the security most of the online banking web applications are developed using spring MVC because of its high security and as you all right is MVC supported and that's the reason it is a great way to develop modular web application and also it comprised of a separate class for specified roles like model command validator etc and that's why it plays a role of separation so these are some of the advantages of spring framework let's start and let's see what we have got for you so what exactly is basically on the Spring Framework in the market so let's have a look on two first right so guys SpringSource was the company created by the founders of the Spring Framework right and it merged into covalent in January 29 2008 so SpringSource was purchased by a DMV for approximately 420 million dollars in 2009 right so this is something where we are trying to come up with some history so Springs dominance over the years has increased right and it basically is having one monopoly in the field of web frameworks specifically in Java Enterprise Edition so Spring Framework plays one important role so you can see a significant increase from 2012 to 2016 in the market right so if you can see so spring MVC is one of the most you know interesting part over here if you can see it has the maximum you know respondent's available so let's see some job trends when it comes to Spring Framework so in a country like India we have somehow you know demand right so it's it's like more of demand in India and if you can see various job portals so an approximate average is nearly 3000 right so in a similar way we got the trends in US as well right so again so US is approximately not on an average level of 6,000 right so we we are trying to come up and show you guys that spring framework is really into the market and we got not of job openings available into the spring trainer so this data is approximately 18 May 2070 so again if you can see some more trains coming in so we got the permanent jobs word says the contrary job right so the graph is rising so it's not falling off right so so as the years increase right so this graph is constantly going up right it's it's not coming down so again not the data we have for taken from you know the various job models so let's see what we have got in for the day so we will be discussing general questions with respect to the Spring Framework and we'll see the dependency injection and inversion of control right so here I'll try to demonstrate couple of four concepts for you practically yes good day after we'll see the beings in the Spring Framework how we can you know rather than coming up with the configuration XML files we can use annotations then what is your data access layer where you can integrate your JDBC or hibernate with the Spring Framework then we'll move on to aspect oriented programming and in the end we'll have one introduction to spring MVC so guys I'll let's start with the first one that's like the Spring Framework now this is very basic question when you are willing to start with interview right so we're taking it so what do you know about Spring Framework or what exactly is Spring Framework now that's like the primary equation we do Mike this so in an introductory part so what you can come up with is so it's a lightweight framework which basically focuses on lose coupling so when I say coupling it means are less dependencies right and it's an integrated framework which is created to address the complexity of enterprise application development so majorly you are going to come up with a spring framework when you are writing an enterprise level application right and other than that it's a layered architecture so we can be selective about what and all components we wish to use right and thereafter it's gonna provide us a cohesive environment for our application development in Java Enterprise Edition so let's see the advantages of sprinkling right so what can be the various advantages if you wanna come up with a spring framework so number one it's a layered architecture so here we got being model or Ojo that's like plain old Java object based programming then there are two powerful features in the core layer of Spring Framework that's like dependency injection and inversion of control so we don't have the control in the hands of our program so this objects they will be created by this spring container right so inversion of control means you don't create the objects you configure the objects and objects will be constructed by the spring framework so again it's an open source community so no vendors are coming in no licensing coming in so that's one of the major advantage of using any of the technology right which is open source now coming on to the features so it's lightweight right so when I say like it means we got Mamre optimizations then we got inversion of control we got the control of object construction to the destruction the entire lifecycle will be managed by the spring container we got aspect-oriented programming which is going to help us in writing the cross-cutting concerns for example security then we got a built in container and we got MVC frameworks so we got a pre you know available snippet as in MVC so which is Model View controller as a design part then we got transaction management and database part right so DD BC etc etc we can do lot of things in data access layer of our spring paper so let's see what and all are the modules available in the spring framework and what exactly they are right so the spring framework is a layered architecture so it's it's basically a layered architecture so layers can interact with each other right so in the aqua direction in the lower direction so the rock bottom layer is at testing later that's like you can have J unit testing or you can have unit is amusing jail unit even you can have integration testing right then we got the core container which contains the which is the core of the Spring Framework right so here we're going to play with the beans core context and spring expression language then after we got AOP that's like aspect-oriented programming then aspects instrumentation and a mastery in the data access layer we got over and that's like object relationship mapping right and other tools are also label and the meb layer we got mid socket web framework servlets food so it's basically a framework of frameworks now make sure you got lot of frameworks I believe it will you know see preamble now one of the major thing which is you know a core of our Spring Framework is the configuration file now this is an example file right where we will be configuring the values for the attributes of an object right so we are talking about inversion of control you are supposed to configure your objects in an XML file spring container will read this spring configuration file and will construct the objects for us now that's like inversion of control so let us see the different components of label in the spring application so guys we got an interface that will define the functions right the implementation that contains the properties like you can say a student is one you know a kind of a class which contains the attributes like a rule number name you know standard age except Rexach try long with a get hood settles or any of the methods right so you got aspect oriented programming and we got the spring configuration which is an XML file and we need to have a client program where we gonna use our spring container and we'll be using the objects constructed by it right so that's like some various components of any spring application so what are the major versions for the spring framework so guys we got 2.5 right so this guy is you know having annotation driven configuration right so which is possible in this 2.5 so before 2.5 we we were not having the annotation of supports available then in 3.0 so the use of Java of 5 improvements in the language right we got some enhancements in the language itself so in 4.0 it's like the first version which fully supports all the Java features right so that's like the latest version even in the market so what are the various ways how we can use this Spring Framework right so the different ways of using the spring member so we can have full-fledged spring web application that's like you model-view-controller we can have a middle tier can provide the help of a third-party web framework then we can use it if we are supposed to do any remote you know invocations then we got enterprise javabeans so wrapping up the existing plain old java objects right so that's like some of the different ways in which we can come up with a spring framework so let's now focus on the core part of the spring framework that's like dependency injection and inversion of control right so what exactly is this I you see so guys inversion of control over here means that you don't create the objects objects will be constructed by the spring primer right and we got ioc container which will do so so we're gonna pile other objects right we're gonna configure them in an XML file management of the entire lifecycle from construction till destruction will be taken care by this guy ioc container we can also come up with dependency injection like features where we can punch in the dependent data or you can say that we got a relationship like person has an address for extra person is one object and address is the other so having a dependency of address object and person object you know on each other we can come up with this dependency injection technique we we can put all this criteria right so it is basically for has a relation which has to be pulled so what do you mean by dependency injection so guys di over here means that we don't create objects but describe how they should be created now that's like the basic inversion of control feature right so we don't directly connect the components and services in the code so we need to come up with the configuration in the XML file itself right so our dependency injection is done in your XML structure itself so we got a container which is response Cybil for doing it right so we got spring container which will do so so how many days this dependency injection can be done so guys we got three different ways of doing it we can have constructor based injection right so it means that you cannot come up with constructors then we can have setter injection then we can also have interface injection so let's try to see what exactly we are you know intending over here consider that you got one class as person right and persons person can have one let's say ID person can have an email then person can have some age and we can have many a graph to use right so these are the attributes for person now we got one more class known as address so when I am going to come up with the address so address can have its own attributes so for example you can have a city right we can have one address line then you can also come up with some state right so you can have a country you can have a zip code so on and so forth right so these are the attributes available in my address now we need to come up with one relationship that person has an address so this is one has a relationship which we have created so person has an address so that is what we are trying to come up with so guys when there is a has a relationship then we are talking of dependencies so this is dependency now right so what we can do now we can have a person constructor where we can say address as in a new address right so let's let's talk of very general thing and in some client class there will have this guy main methods from not writing I'm just trying to undo it in a pseudo way so let's say we are going to come up and we are saying person e as in onion person right so I'm going to create one new person and then the constructor of the person we'll be executed and that time the object of address will be created it means that there exists a very high dependency that address object can be constructed only when the person object will be constructed so such a dependency is leading to very high coupling right so we got a concept with person object when it will be constructed only then the address object will be constructed so this is one problem statement and we need to find out a solution for this to find out the solution we will not do it like this we'll come down and we'll try to create one constructor and we'll pass one address and we'll say copy the a into address all what we will say we will say let's create one sector light set address and here we can have an address as in a right so this is what we would rather intend to do so benefit of this will become like you can say address a as in a new address and thereafter you can say P dot set the address as in a or in the other ways you can say person you have a new person and here in the constructor you can pass the guy D so this is like how you are resolving the dependency so address object is constructed separately person object is constructed separately so you can set the race either through a setter method or you can pass it as an input to the constructor as for your build so we are trying to come a bit constructor injection and setter injection in the configuration file link with these two key concepts right so we call this as dependency and here we have resolved the problem of hi coupling we have reduced the coupling level and we call it like we have solved one problem where dependency was very high so let's now proceed and understand so either you can do it by constructor or setter or you can even do it two interfaces right so if you want to come up with some polymorphic concept and if you want to come up with some you know runtime logics you can also write interfaces which it's going to be more flexible in our programs so what exactly is the difference I hope one of the key concept is very well clear now so what is constructor and setter based a dependency resolution so in constructor there is no partial injection but in cetera you can do a partial injection right so here we don't override the setter property but here you need to come up with the set of property defined variable so it will create new instances if any modification is done right so here you can't come up without an object right so you need an object for sure but here you can skip that and you can do it later so it is better for too many properties and if we got few properties you can always use the second injection right so these are some differences between constructor and setter injection all right so how many types of ioc containers are there in the framework so guys there are two major containers in the spring framework of ioc layer so we got application context and beam pattern right so guys the bean factory is basically one container right and application context is implementation and it's built on top of being factory itself so it is always better to come up with application context rather than the beam path so let us try to differentiate between beam path we know application context and let's try to write one program and see how we can do so right so let's see one program so let's start create one new Java project and I'm going to name it like my spring damn it right so let's say next and let's say finish so it's it's one project in Java which I have created and within my SRC directory what I am going to do I'm going to pick one class and this class is going to go under the package for edwiga and since we are on to entry questions let's think of employee class right so let's say finish so here we are with the employee I am going to come up with one employee ID right so that's like the attributes then we can have a name of the employee I can have some salary associated with the employee right let's say we got Department for the employee and finally if you want you can have let's see an email right so we can add a lot of attributes so these are a few of the attributes which I have taken now we can create one constructor by it so let's write one in constructor so it's a it is a default one so here I will just say C so employee object constructed right so let's say employee object constructed so coming up with next I can write source and then getters and setters right so let's write getters and setters for our class so select all and say ok so here we are with the getters and setters and quietly I will generate one to two stage method right so let's say a two string method so this is my employee class which is our written and we say that this is a pooja or Dean or model right so you can use any terminology right so Padre is the plain old Java object let's come back and let's try to write one more class and here I am going to say a client class with the mean so in the client class with a main method so what is the traditional way of creating the objects or what is the you know a general way of creating the object right so general way of creating the objects it goes like you say an employee as in a new employee why so this is the reference to the employee object so I can say II red door set of name for example it goes like John you say II left dot set off let's say e ideas 101 so guys uh these are some regular ways of creating objects right so either thought let's say set of Department so I'm going to say it's like Rd then let's say set of salary which is like 45,000 then you say here F dot set of email which goes like John at example.com now this is one traditional way or general be how we construct an object now if I say see so op eat it details plus e tip so if I print out the e-rep it's gonna call the two string method and here you will see later on it has a java application so you see it says employee object constructed and these are the details available in my employer it now this is one general way or traditional way how you can start ops but we need to understand how the objects are constructed using the Spring Framework to do so the very first thing which I am going to do is I'm going to link the jar files right so how I will do that I will do a right-click and I will say build path and I will see configure my bill rates man I'll see configure the build path I will be my libraries and I am going to say add external jars so let's add the spring yard so thanks I got this spring jars available over here so we need to add the core layer jars right so I'm using the spring core over here so I'm going to add these cool jar pirates and then say apply and you can say ok so here you can see the referenced jars available for your spring penguin now the next part is to come up with some XML configuration file in your SRC directory to do so so what I'm going to do I have it with me so let me open that so I'm here with this employee being dot XML we're going to copy this XML file and I'm going to paste it in my SRC directly so a point to be noted is that this is pasted in SRC directory but not in the package so in my SRC directory this employee be not XML it looks like this so it's basically one XML configuration file the root tag is beans and we got the tag as B so I am going to say bean ideas p1 and class is co dot add record dot employee so fully qualify the a name of my class it comes up like Co dot ed we cannot employ and the ID is even now we got few properties available over here tid name salary Department and E right so let's start with e ID so we got tid then you got name salary Department and email right so ii ID name salary department and email so we got tid name salary Department and email so I am mentioned this properly so what are the values which you want to asset in today so I will say one or two the name goes like Jenny and Sabri goes like let's see some way around to be thousand Department is HR and email is churning at example.com right so here we have X so now you can configure as many as objects you want in your XML file so let's configure one more let's say the ID is even now the ID should be unique right so this ID goes like to not one and the name goes like George so George is having a fifty seven thousand and this guy belongs to admin and let's say email-id goes like George at example.com right so this is one of the configuration files which we must have in a spring project right so what we got we got a POJO class we got a configuration file we got the jar files and we got a client in class to test our program so let's see the spring way so the spring wave goes like with the help of a concept known as inversion of control right so what exactly is this term inversion of control so guys in inversion of control I am going to give this write to my spring container so let's see the first container so I am going to say the first container over here as bean factory so let's see how to use this guy beam pattern first of all I come up with one API known as resource from a spring prima right if it's from the Spring Framework you say resource resource as in a new class path resource and here I will say my employee being dot XML so this is your XML configuration file right so if you can see you over here so this XML configuration file it goes into the class path resource and thereafter you say bean factory and let's say factory as in a new XML bean factory from the resource so guys this is how we use our first guy that's like being factory right so this is my first container so it's a spring IOC container so what's being factory it's a spring I have three country so this guy is responsible to construct objects for us I am just going to run the code so when I run the code so nothing happens just this relevant information is displayed now what we need to observe over here so when I'm execute the code Spring Framework the container that's like bean factory is not constructing an object for us but as soon as I will say employee and one as in factory dot get the bean on the basis of Eva right so here you can see we got the guy as GE one right and you need to typecast it back to the employee so when you call it snap that get being at that very instance Nam and I am going to run the code right so you can see employee object constructed only one object is constructed right so if you try to get the second guy as in e2 and when you run the code again you will find that both objects are constructing so being factory what it is doing it constructs the objects when we request that's like the spring IOC container build back so it constructs the object when we request now you cannot just say of C so on and one as well as m2 right so here we are so let's from the code and you can see the details coming in so guys this is the first bean factory as in your ioc container right so let's see the second guy that's like application context so it's also Spring Framework container right and let's see how it's gonna come up so it constructs the objects even when we do not request so let's see how how it's gonna do so so I'll comment out the above code and let's see the application context in action so I will say application context context as in a new class path XML application context and here you mentioned this employee being dot examine now it is very much equivalent to whatever we have done before now I am just going to run the code right so when I run the code I can see both the employee objects are constructed even though I have not requested for any object now that's one of the significant difference which you can see over here right as in the practical environment so if you don't request even then it's gonna create and if you are trying to request so you request it in the same way so the way you were requesting it before so guys if you don't want to typecast it over here you can just come down and you can say comma employee dot class right so you can mention the type over here so that's the second day so both the cases it's gonna work right so you can mention it over here right and you can just leave this from this side so here again so both the concepts they gonna give you the same outcome but the object construction if you can see right application context will construct the objects even if you don't request for them now that's like one practical example how we are able to demonstrate both the concept so let's see more of the differences between both of them so that was a quick introduction to the Spring Framework right what is inversion of control so guys be impacting application context so here we got lazy initialization and there we got aggressive initialization now that relevant concept I think is very wealthy whatever we have demonstrated so in a bean factory explicitly provided a resource object using the syntax we're here every resources are managed by its own so internationalization is not supported in bean factory whereas application context that's so application annotation based or dependency injection is not supported by bean factory whereas application context will support now that's also one of the major differences so some of the benefits of IOC so guys IOC will minimize the amount of code in application because you'll have one XML configuration file so any time if you want to change so you can change the configuration right so it's very easy to test our application and coupling can be you know reduced so it can be a loose stuff concept which we can promote and hence there will be least inclusive mechanisms with minimal efforts right so ioc containers they gonna support eager instantiation and lazy loading kind of services for you let's see now what our spring DS so as a beanies nothing it's your Java object which we have seen already in our program right so the metadata is provided in the XML file container will create an object funny so that's like your basic bean object so this configuration metadata can be given in an XML file it can also be supplied through an annotation based configuration or it can be even a java-based configuration so there are three of main models how you can configure your objects as in your ioc concept now what can be the various are scopes which can be supported by the spring primo so what is meant by this scope right so let's try to understand this spot so why is a scope over here means when you are writing your employee right so here you can mention a scoop so scope can go like a single ton which means for your entire application there will be only single object scope can go like prototype so by default it is single term right so when you say prototype every time a new object will be constructed for me so if you want to see a difference let's see one difference right so I am going to say the scope is a single term in my client so I am going to work on even right so in my employ dot Java so here in the two string method I am going to say plus super dot to strain right so I put one back slash end and I will say object address super dot tution so in my client when I'm going to run the code you will also find that alright so I think I'll even printed it so let's say C so of M point right so when I'm going to say n1 so it it will now say the address is 2 1 b c FF d 5 right so there is one object which is constructed at this memory location now let us try to 1 M 2 which is again we have to requesting even right so you are requesting only e 1 so again I am going to print m2 and now you will find that both of the objects are same right you can see the address so that is where we got the Dayak singleton let's change this to prototype so I'm going to change it to prototype now let's read on the code and here now you will find that this is a separate address and this is a separate entrance so that's like the scopes within your beans right so scope when your beans can be singleton prototype requests session or a global session request these for one HTTP request session is throughout one HTTP session and global session is valid in the context of a bed of air spring application context right so what is the bean lifecycle so guys we also have a life cycle of a bean right so what does this mean now spring pimper you got this entire lifecycle management happening by the spring IOC container itself so the beans gonna get instantiate the next property is gonna get populated through setter methods right so setter methods are going to get called right and any post processor if you want to come up you got a beam post processor API so we can have after properties being set and custom in it and custom destroy can also be implemented like we have seen mine hidden my destroy so we can also have post initialization like we got the post processor over here and once the beam is ready to use you can use it and bonds container is shut down so custom destroy matter is executed so what our inner being so guys inner beans are like if you have person know it has a relationship in student right so you can come up with the inner dude so you can have a bean property and then you can also create one inner beam and you can set its properties over here so what is bean violin so guys the bean wiring is something right which means that beans are combined together with the spring container so when we are wiring the the spring container must know what deals are required and how the container should use dependency injection to tie them together right so what are the different modes to wire the beans so guys we got my number by name by tile constructor and auto-detect so these are the different ways through which you can bang it so what can be the limitations if you are going to come with Ottawa so that is the possibility of overriding is there right and then primitive data types its becomes complicated it's challenging and it's again confusing in nature right so you need to remember what was the name over there and what has to be kept over here right so it's a bit confusing all right so we got spring annotations so guys the way we got XML configuration in a similar way we can have annotation right so you can mention after it configuration or at the rate beam so these are the different annotation Albania spring IOC container so how I can come up with the annotation widely so if you are supposed to come up with annotation vital you must support context call an annotation config you must write this property in your XML file right so how you can do so so you need to come up over here and you need to say context Colin annotation convict so if you are going to support your annotation you follow the standard all right so let's now see what are exactly the different annotations which are quite used in commercial or in industry so we got component controller repository and Serna so these are you know a kind of most use annotations so nice component is basically a generic stereotype right which can be use across the application controller is to represent your business spot service will annotate classes on the service level layer and repository is gonna deal with the database part so what is this add the rate required a notation so guys when we are coming up with the beam construction add the rate required is a kind of you can say a1 constraint right so name asked to be populated for sure else it's gonna throw a beam initialization exception so that's like back you can say putting up the constraints so add the rate auto wire annotation it's basically lotta wiring right so let's say you got an employee right and we got this guy named so you can put it as an average auto wire so whatever you are hereby nail so you need not to come up with a different dependency injection technique you need to use the same name so what is this guy qualifier so while there are some situation when you wanna create more than one being for the same type but we want to wire only any one out of them with a property so we can use qualified annotation along with auto wiring so that we can remove this ambiguous nature right so qualifier is used so that you can mention which beam is supposed to be auto white right in case you have multiple constructions for the same guy now we got a request mapping annotation so this annotation is particularly for your HTTP requests now we are trying to come something known as the best part of the spring member so a great request mapping annotation it can be applied and both the levels so class level and master level so in class level method is for a map the URL request whereas on the map and level it's going to also take care of HTTP method right so HTTP method can be either get post delay put trace any methods right so these methods can be taken care so let's see what is the data accessed here so guys a spring comes with data access a support right so we got all Dow as a design pattern concept which can be you know I used off in our Spring Framework so Spring Framework data access is aimed at making it easy to work with the data access technologies like hibernate e bat is JDBC or jelly or a java data objects so it's it's very easy to switch between the persistent technology so you can switch from one of the framework to the other framework very easily so core worrying about getting the exceptions that are specific to each technology so you can write a very beautiful independent code so what are the various exceptions so guys we got data access exception as the root right so there are various subclasses to this exception now so if you can see there there is one whole hierarchy in a similar way the basic code java concept if you talk about the standard edition you got trouble coming out with exception and then you are having a complete exception hierarchy as in runtime exceptions change versus antics etcetera so here we got a complete hierarchy coming in right so in order to remember that but you should always remember that there exists one painting exception class so what and all classes are present in the spring JDBC API so guys we got JDBC template simple JDBC template named parameter daily visit template so we got a simple JDBC insert simple JDBC call so these are some major important api is with respect to your JD so JD with the template is again now used on a very frequent basis so how we can use hibernate and spring framework so guys there are two ways so either you can do an inversion of control with the hibernate template API or you can extend the hibernate dao support and you can use AOP that's like aspect-oriented programming so what are the types of transactions our transaction management right or types which spring framework supports so guys there are two things one is programmatic transaction management and declarative transaction management so many times you as a developer might be confused over here so transaction is managed with the help of programming in programmatic transaction management so here we got an extreme flexibility right but maintenance is a bit difficult path but in decorative part so transaction management is not a part of your programming so it's basically annotations or XML based configuration to manage your transition of that's the core difference so decorative means either a notational based on XML so different or Enza are supported by the Spring Framework rights right all of the major orange right any relationship mapping for the objet fling book so mostly morning we got hibernate antibiotics which are you know use even JPA is also one of the core concept now let's see spring AOP so guys a o P stands for aspect oriented programming now this is one technique which allows the programmers to modularize cross-cutting concerns so what is meant by the cross-cutting concern will see in our upcoming in your slides so we can also modernize the behavior that's gonna cut across the typical divisions of responsibility now the core construct of the AOP is known as an aspect right so which encapsulates different behaviors affecting multiple classes into some reusable modules so we got a base for a core program we got an aspect and thereafter you wanna beat them together to get a final system wait so let's try to understand it in more detail what exactly is this die aspect so guys an aspect is basically a modularization of a concern that cuts across multiple objects right so you got a source method and a target method so when we are going to call the method so this aspect will have one point cut and one joint cut so what is this guy concern so guys for example security is one concern right so that's like fulfilled by aspect so let's see what is this joint point so when you are going to execute a method so if you can see in the method execution you got various joint points right so a point during the execution of program such as the execution of a master or handing of any exception right it is known as a joint point so in spring AOP a join point always represents a method execution right so any point in the method execution is referred to as a joint point so advice is something with is an action taken by an aspic at any particular join point right so for any particular join point we got an advice right within an aspect so there can be around before-and-after so what happens up before the method execution what happens after the method execution what happened around the method execution right so let's try to take one example here if you are going to login to the Amazon right so you you are going to come up with one request so at what time you wanna login so that's like before right and what time you log out that's a gap you take it like one method execution as one interval where you are surfing around the Amazon and that's like around right so that's one key example so many AFP frameworks we got model and advise as an interceptor and we need to maintain various chain of interceptors around the join point so let's see the different advices so we got before and after right so this is like coming up before the method execution and after the method execution similarly we got around the method execution we got after returning and after throwing engine so this is quite linked with exceptions now so let's find out the difference between concern and cross-cutting concern so guys what is the concern concern is the behavior we want to have in a module of an application where as a cross-cutting concern is a concern which is applicable throughout the application for example security from writing your code to maintenance to execution you need the security on every level right so that's like your cross-cutting concern so what exactly are the different QP implementation so like we got the spring AOP apache aspectj and JBoss AUP so these are the various AOP implementations available in the market so let's start a differentiate between spring AOP and aspect Jub so guys spring AOP we got reading through proxy right here we got a weeding through aspectj within the java tools so AOP in the spring will support only master level point cut whereas in a spec jup even the field level point cuts are also possible so we got dvd-based so it's like document type definition right and as per day is a schema based or a notational configuration again so what is this proxy all about so as what happens whenever you create an object so after applying an advice to a target object right so in respect to the client objects any target and the proxy object they are the same so you got an advice and you got a target object so in all all you get a proxy right so that's like what the proxy is intended so what is meant by this Devine when ever an aspect is going to be linked with application types or any object which has to be created as an advice object so this particular concept we call it as beaming right so this can be performed on three instances either it compilation time or at run time or even at the time of loading right so guys in Spring Framework when you are doing the reading performance reading process it is performed always at running back right so you need to call up for your proxy for your target and this particular weaving will be taken clear at run time so it's all dynamic so lastly we got spring MVC that's like Model View controller so that is the spring MVC framework helps us to work in a predefined Model View controller architecture so we got ready to use components which will help us to develop flexible and loosely coupled by application so we got different aspects of the application which are separated now so you can see over here we got dispatcher servlet as one of the key important ingredient we got the mapping handle mapping controller beauty solver and beam so these are the various components let's see what are these guys so guys dispatchers servlet is the one which will handle any HTTP request or response so spring MVC framework is designed around the dispatcher servlet only so what is this web application context so better application context is again an extension of application context or application context is the code container whereas the application context is a container on the web app layer right so this will an extra feature for your web applications so it is also capable of resolving some themes and it knows which servlet is associated to kind of resource right so that's like your bear application context so what is this guide controller so vise controller is going to act as a receptionist so whenever any new request will come from the user it will go to the controller and thereafter controller is gonna process the data from the model and then it's gonna give the view back to the user right so spring controller is going to act as a receptionist now right beard it's gonna handle your requests so that's like the whole agenda for the day so guys if you are going to come up with any spring interview right you need to follow these topics you need to prepare these topics so that you can crack your introduced right so that's all from my side and Ed Eureka so we wish you all the very best and we hope that you do Bill in your interviews so thank you very much keep watching us and keep reviewing us so once again thank you very much so this is a shot on behalf of Erica I hope you have enjoyed the session all the best rise and shine thank you very much guys bye bye I hope you have enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply them at the earliest do look out for more videos in our playlist and subscribe to any rekha channel to learn more happy learning
Info
Channel: edureka!
Views: 322,557
Rating: 4.7709994 out of 5
Keywords: yt:cc=on, spring framework, java spring full course, spring framework full course, spring framework course, spring framework crash course, spring course, java spring framework, spring framework tutorial, spring framework tutorial with examples, java spring tutorial, dependency injection, spring ioc, spring mvc, spring architecture, what is spring framework, spring interview questions, spring aop tutorial, aspect oriented programming spring, edureka, spring framework edureka
Id: VvGjZgqojMc
Channel Id: undefined
Length: 218min 25sec (13105 seconds)
Published: Sun Jan 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.