[2023] Spring Framework: A Tutorial for Beginners | in28minutes | Ranga Karanam

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
application architectures have evolved continuously during the last two decades we went from basic web applications to web services to rest API to full stack applications to micro services today we are deploying applications to the cloud we use a number of Frameworks to build our applications spring spring boot spring MVC hibernate string security spring data spring cloud and a number of others do you know what are the two most important Frameworks you need to learn to get started with building great applications in my humble opinion the number one framework you need to learn is this spring framework spring framework provides the most important features needed to build easily maintainable applications dependency injection and auto wiring number two framework I would recommend is spring boot spring boot mix using spring framework really really easy let's take an example let's say before spring framework it took about 1000 lines of code to set up all the important features of a production ready application with spring framework the number of lines needed reduces to 700 and with spring boot it comes down to 400 as you can see spring and spring boot improve your productivity a lot you'll be able to do a lot more with a lot less code and that's what we will be exploring during this course in this specific module we'll be focusing extensively on the spring framework beginners to Spring framework struggle to understand why spring framework is needed let's sample some of the terminology that is typically associated with the spring framework tied coupling loose coupling dependency injection ioc container application context spring beans Auto wiring component scan as you can see these terms are very very abstract it is difficult for beginners to understand what they mean I have been playing with spring framework for almost two decades now I'll be bringing in in all that experience to make spring framework really really easy for you we will learn all the above concepts with easy to understand Hands-On examples are you excited to learn spring framework I am rangakarnum and I'm all set to help you learn spring framework let's get started you can build a variety of applications using Java spring and spring boot you can build web applications rest API full stack applications microservices and a wide variety of other kind of applications irrespective of the application you are building spring framework provides all the core features that are needed once you understand spring framework you can learn spring boot really really easily as well and understanding spring framework helps you to debug problems very quickly and that's the reason why in this module we'll be focusing extensively on the spring framework what are the goals of this specific section the number one goal of this section is to understand the core features of spring framework and the approach we'll be using is a Hands-On approach we'll be building a Loosely coupled hello world gaming app with modern spring approach you'd have played with a number of games as a kid you might be playing a number of games right now as well you might have played Mario super contra Pac-Man these are all awesome games to play what we'll be doing in this specific module is to build an application to run these games we'll get Hands-On with spring and we will be understanding what is the need for spring framework and what are the different terminology that are associated with spring framework tight coupling loose coupling ioc container application context component scan what is dependency injection what are spring beams and what is auto wiring to understand all this terminology we'll be using an iterative approach we'll be designing a game Runner class to run these games Mario super contra pack man in an iterative approach in iteration 1 we'll be building tightly coupled Java code we'll be creating a game Runner class and we'll be using the game Runner class to run the game classes in iteration 2 we will understand what is loose coupling and we will try to bring in blues coupling using Java interfaces we will create an interface called gaming console and we'll have all the game classes Implement that interface if you are not familiar with Java interfaces do not worry I'll make it really really easy for you in iteration 3 is where we will bring the spring framework in we will go to loose coupling level 1 with spring framework in iteration 3. we will create a number of spring beans and we will have the spring framework manage the objects which are created and also manage the wiring in iteration 4 we'll be going to lose coupling level 2 with spring framework we'll be bringing in some amazing spring annotations and you will see by the end of iteration 4 spring framework will be responsible for creating managing and auto wiring objects some of the terminology you would have heard in the specific step might seem really really difficult do not worry it's my responsibility to make it really easy for you by the end of this specific module enough talk it's time to get our hands dirty let's start building a spring application in the next step in this app let's create a spring project the best way to create a spring project is to go to my favorite website start dot spring dot IO this is called the Spring initializer over here you can give a name to your project select the Frameworks you want to make use of and create a project quickly to get started we'll be using Maven as our build tool so what I would recommend you to do is to choose Maven project and the language I would recommend you to choose is Java the specific version of spring boot that we are making use of right now is 3.0.0 rc1 we are building this course with spring boot 3. so by the time you're watching this video you might see a released version of spring boot what is the released version a released version is something like 2.7.5 or 2.6.13 these things don't have snapshot rc1 or M1 M2 M3 none of those things are present in these specific versions these two versions are called released versions right now there is no released version for spring boot 3. that's why I am making use of 3.0.0 rc1 however by the time you are watching this video if you see a released version for example if you see 3.0.1 or 3.1.0 or 3.5.0 go ahead and use that at in 28 minutes we always ensure that our courses are up to date and compatible with the latest release duration of spring Boot and the latest Java version so do not hesitate in making use of the latest Java versions one very very important recommendation do not use Snapshot versions snapshot versions are currently being developed by springboot team and you don't want to use them when learning something new when you create a project you need to give it a group ID and an artifact ID a group ID and artifact ID are very very similar to package name and class name so the group ID I would want to give in here is com dot in 28 minutes and the artifact ID is learn hyphen spring hyphen framework I'll be using Java 17 you can use the latest version of java that is available I'll choose the defaults for everything else in here so make sure that you're using a maven project you're using Javas language you're choosing the latest released version of spring boot 3. you give a group ID of com in 28 minutes the artifact ID is learned spring framework and you choose one of the latest versions of java available that's all we are all set enough to create our spring project let's go ahead and say generate once you click generate a zip file will be downloaded to your downloads folder what I recommend you to do is to unzip the downloaded zip file to a folder on your local hard disk maybe something like C colon slash after that launch up Eclipse choose a default workspace and you should see a screen like this we are now ready to import the project in the way you can import the project into eclipse is by clicking file import type in maven we are using Maven as the build tool so we are going to import an existing Maven project I would say next after that you need to choose the root directory of your project I have imported the zip file into this specific folder so what I'll do is go ahead and say open you should see a prom.xml come up com.invented minutes is the package ID we made use of and learn spring framework is the artifact ID we are now all set to import the project in go ahead and click finish the import of the project will take 5 to 10 minutes if this is the first time you are making use of this specific spring version so have some patience if you are having any problems with importing the project in I would recommend you to ensure that you are making use of the latest version of Eclipse IDE for Enterprise Java and web developers if the import 12 you should be able to see a project structure like this Source main Java where we'd be storing all our source files in Source main resources where we will be storing all our configuration files in and sources Java where we'll write our test code in this step we created a spring project and we imported it into our IDE let's get started with playing with it in the next step are you excited to build your first application with spring let's get started right now we will get started with iteration one we don't want to build a game Runner class which can be used to run multiple games Mario super contra and Pac-Man what we'll do in this specific step is to focus on creating a game Runner class and creating a Mario class so let's go back to eclipse and over here I'm selecting the package com in 208 minutes learn spring framework I'll do a right click on this and I'll send my new class so we are creating a new class I'll name the class as app gaming for now we are making use of basic Java so I'll put app gaming basic Java in here I'll also want to create a main method so public static void main I'll select that let's go ahead and say finish so we have a basic class in here now what do you want to do in this class we'd want to create a game Runner class instance and we would want to launch up Mario what I'll do in here is to say where game Runner is equal to new game Rana class and inside the game Runner class we would want to be able to run the Mario game so let's create a where for the Mario game as well so I'll say Mario game is equal to new Mario game and to the game Runner class you would want to pass Mario game as a Constructor argument and at the end you don't be able to run it gamerunner Dot run now you might be wondering ranga you have not written Mario game class game Runner class now you'd see a lot of compilation errors come in right so you have a Mario game which cannot be resolved to a type game Runner which cannot be resolved to a type let's fix them right now if you go over here and press Ctrl 1 or command 1 you should see a suggestion saying create class Mario game let's go ahead and use that create class Mario game The Source folder will be Source main Java the package will become in 28 minutes learn spring framework dot game so this is the package where we'll be creating our Mario game class in I'll take the defaults for the rest of the things and I'll say finish so we have the Mario game class ready next we want to get the game Runner class ready that's the next compilation error in here and again to a control one or command one and say create class game Runner let's do that let's take the defaults in here The Source folder would be the same Source main Java the package comment management learn spring framework again we want to make use of the game package that we created earlier I'll take the defaults for the rest of the things and say finish awesome we have a game Runner class in the learn spring framework game package let's go back to the app gaming basic Java which is where we were writing the code now there is another compilation error which is coming in why because there is no Constructor with Mario game as the argument so if I go back to the game Runner class we do not have a Constructor in here so what we'll do in here is we'll create a instance of the Mario game so game Runner we won't be able to run the Mario game so let's go ahead and say Mario game game and let's create a Constructor so public game Runner and Mario game again what do we do in a Constructor this dot game is equal to game so this is pretty basic Java code this is just a Constructor that we are writing in here nothing complex and now if I go back to the app gaming basic Java class this line does not have a compilation error anymore now the game Runner dot run is giving me a compilation error so let's go ahead and fix that I'll do a control one and say create method run in the type game Runner and this would create public void run method in the game Runner class and over here for enough I would do system dot out Dot println and I would say running game and let's add game in here so we are running the game again there is one shortcut that we are taking right now we are making use of system dot out dot println in general this is not considered to be a good practice it's typically recommended to make use of some logging framework we will get to logging a little later in the course until then we'll be making use of system.out.println to write output to the console so let's get back to our app gaming basic Java class we have everything compiling congratulations we are all set to run this right click run as Java application if you don't see the console come up double click here you should see console in here and over here in the console you should be able to see the output running game come in 28 minutes learn spring framework game Mario game so we are able to get the first running version of our game Runner class typically whenever you are playing a game you press a number of buttons up down left right so that's what we'll be doing when we are playing a game let's add these features in so let's go into our Mario game and let's write code for that specific action so the typical actions are let's say public void up button what do you want to do when somebody presses a up button let's say in the Mario game we would want to do a jump right similar to that we can create other methods as well so down method when you press down button in the Mario game Mayo should go into a hole and let's say if it's the left button it should be go left and if it's actually should we go back and if it's the right button what you want to do is to accelerate so what we are doing here is implementing methods that would perform the specific actions when a specific button is pressed on the game and in the game Runner run method we can call those things so I can say running game this and I can call the actions I can say game dot up game dot down game dot left and game dot right when these actions are performed by the game Runner the appropriate methods are called on the Mario game now what would happen if I run this right click run as Java application and I'll double click in here and this is where you would be able to see the console output so you're saying running the game and the actions jump going to a hole go back and accelerate until now in iteration one we have written some simple Java code we have created a game Runner class we have created a Mario game class and we were able to run actions on the Mario game class using the game Runner now one of the interesting things you'd see in here is the fact that we are calling this tightly coupled why are we calling this tightly coupled let's find that out in the next step I'm sure you are loving the video until now do you know I write an amazing newsletter every month about cloud devops and other emerging Technologies do you want my opinion on what you should learn and how you can learn fast do not forget to check out my newsletter you can go to the website rangakatnam.com and you can find a link to the newsletter in there keep learning every day now let's get back to the next step in the previous step we wrote some simple Java code and we called it tightly coupled in this step let's understand what is tight coupling and we'll also understand how to write Loosely coupled code let's get started with the first question what is Type coupling why are we calling this specific piece of code tightly coupled right I'm right now looking at app gaming basic Java we are creating an instance of the Mario game and we are running the Mario game using the game runner in here now let's say I want to create a new game I want to create a super contra game so instead of running the Mario game I'll comment this out and I would say I would want to run a new game super contra game is equal to new super contra okay this is the game I would want to run let's create the super Contract game quickly so I'll do a control 1 create class super control game I'll use the game package again so com in 28 minutes learn spring framework game package so make sure that super Contract game Mario game and the game Runner classes are in the same package common 28 minutes learn spring framework game and I'll say finish and I can copy some of the code from Mario game so I'll copy this and paste it in here so enough we have the same methods that are implemented in the Mario game implemented in the super Contract game as well however in super contra game the same buttons will perform different actions let's say when up button is present I would want to just say up when you press down super contra needs to see down for example and if you say left super contra will go back and if you say right supercontra would shoot a bullet this is a different game so you perform a different action when a specific pattern is pressed now let's go back to app gaming basic Java so we have the super contra game ready the code and Logic for it is all ready however if I am trying to run the super Contract game what is happening in here I have a compilation error what does the compilation error say The Constructor game Runner super Contract game is undefined the thing is if you look at the game Runner class it's tightly coupled to the Mario game if I want to run the super contra game using the game Runner class I would need to make code changes in here what are the code changes I would need to do I need to go in here and instead of Maria game I need to put in super Contract game so let's put that in super control game and even this should be super contra game so we are making code changes in the game Runner class to be able to run a different game earlier I forgot to put private in here so let's make this private let's save this and now if I go back to app gaming basic Java and save this and right click run as Java application oops again I'm not able to see the console so let's double click see it in here up sit down go back and shoot a bullet so now we are able to run the super Contract game but the problem we are having right now is that your game Runner class is tightly coupled to a specific game if I have to go back to the Mario game then again I would need to make the game Runner use the Mario game this is what is called tight coupling coupling is a measure of how much work is involved in changing something if I want to run a Mario game instead of super Contract game how much changes involved in a specific class that is what coupling would measure coupling is very very important even in general life if you take the example of a car an engine is tightly coupled to a car changing an engine takes a lot of effort however a wheel is Loosely coupled to a car if you have a flat tire you can easily change the tire another good example is laptop versus a computer you can take a laptop anywhere you go it is not tightly coupled to a specific place however a computer on the other hand is a little bit more difficult to move coupling is even more important in building great software the only thing that is constant in technology is change everything changes business requirements change Frameworks change your code changes and therefore we want loose coupling as much as possible we want to be able to make functional changes with as less code changes as possible and starting the next step let's explore how Java interfaces and spring framework help with loose coupling in this step we understood the fact that our game Runner class is tightly coupled to a specific game if I would want to change from one game to another I need to make a code change to the game Runner class how can we bring loose coupling in that's what we would start exploring starting the next step starting this step let's focus on iteration 2 we would want to bring in loose coupling with interfaces we have the game Runner class so we want to bring in a game console interface and we'll have our game classes implement the gaming console interface so until now we have a game Runner class which is directly running the specific game Mario or super contra or the Pac-Man we have not yet introduced the Pacman game we will do that a little later but what we want to change to is this we want to have the game Runner class interact with an interface called gaming console and we'll have all the game classes implement the gaming console interface let's get started so let's look at the super contra game and the Mario game you can see the common actions which are performed in there up down left and right and right now we will create an interface an interface is something which represents the common actions that can be performed on a specific set of classes so let's go ahead and say right click new interface and I'll call the interface as gaming console it's in Source main Java make sure that it's in the same package as the other game classes so it's com in 28 minutes learnspringframework dot game I'll make it public and I'll say finish and inside the gaming console what are the actions that you would want to avoid up and the four other actions so up down left and right these are the four actions that I would want to be able to perform using the gaming console and these actions are already implemented in the supercontra game and the Mario game as well in the app gaming basic Java class right now we are running the super contra game so let's start with the super contra game and make it implement the specific interface so I'll go to the super contra game use the interface gaming console so I'll say implements gaming console you would not see any errors right now because all the methods in the interface are already implemented by the super Contract game now what's the advantage we get by doing this I don't need to make any cone changes in app gaming basic Java and I'll be able to run this as is run as Java application so that's cool now let's say I would want to run the Mario game right now we have different variable names for both of these so let's make them similar actually so I'll say game and game in here as well so we have the same variable named for Mario game super contra game and we are using that to create the game Runner class fair enough the thing is we are able to run the super Contract game will I be able to uncomment this and run the Mario game nope that will not work why because the game Runner class is still tied to the super Contour again what we'll do now is instead of making use of the super Contract game in here we'll make use of the gaming console interface so I'll go to the game Runner class and what we'll do is we'll change instead of super contra will make use of gaming console all the method calls would still work because these methods are part of the interface definition so we have done three changes until now we have created a gaming console interface and we have made the super contra game implement the interface and we have made the game Runner class use the interface instead of the direct class so what is the advantage of doing this now I can go back to app gamingbasic.java enough you'd see that if I go to add gaping basic Java and do a right click run as Java application it still continues to work let's see what would happen if I actually change this out if I uncomment the VAR game is equal to Mario game and comment vargame is equal to super contra game what would happen oops there is still a compilation error it's saying the Constructor game Runner Mario game is undefined so we have not really solved anything isn't it the thing is there is one step we need to do so if you go to Mario game and say implements gaming console so nav both Mario game and super Contour game Implement game gaming console and if I go back to app gaming basic java.java you'd see that I'll be able to run this as a Java application in a very very easy way right now we are running the game Mario game and you can see the methods in the Mario game invoked and if you want to switch to the super contra game all that you need to do is to comment and uncomment and that's it you are able to know do a run as Java application and you'll be able to see this in action the thing we are observing in here is that nav we don't need to make any changes to the game Runner class when we would want to switch from one game to another earlier the game Runner class was tightly coupled to a specific game if I would want to change the game I would need to change the game Runner class code however nav by bringing in a gaming console interface the game Runner class is disconnected from this specific game the game Runner class is no longer coupled to a specific game it is only coupled with a specific interface and therefore the game you are playing can be changed without changing the game Runner class it's time for an exercise until now we have created a couple of games Mario game and super contra game it's time to create a Pac-Man game so go ahead create a Pac-Man game and try and run it in here pause the video in here and unpause after you complete the exercise I hope you had a chance to complete that exercise this exercise should be very very easy right all that I need to do is to go and copy the Mario game Ctrl C Ctrl V I'll make I'll save all the files so say OK and instead of Mario game 2 I don't want to call this Pac-Man game and I'll say okay the Pac-Man game is in here and in the Pac-Man game let's make it simple I'll just say up down left and right one thing you'd already observe is the fact that this also is implementing the gaming console and therefore if I would want to run the Pac-Man game nav all that I need to do is to uncomment the previous code and say game is equal to new Pac-Man game the great thing oops a compilation error because I have not imported it in yet so import com into idiot insulin spring framework game Pac-Man game let's do that organizing Port should help you do that and do a right click run as Java application what is the game that we are running right now aha it's the Pac-Man game up down left and right in this step we understood the role interfaces play with loose coupling once you create an interface and make your game classes Implement that interface you can make use of the interface from the game Runner class and therefore nav game Runner class is disconnected or Loosely coupled from the specific implementation so it does not matter which game you are running you don't need to make a change to game Runner to change the game so enough we are at the end of iteration 2 where we introduce loose coupling using interfaces starting the next step let's bring the magic Offspring framework in I'll see you in the next step welcome back this is the current state of our code we are creating a Pacman game and we are creating a game runner in here we are passing the Pac-Man game or the game as an argument to the Constructor of game Runner let's dig deeper into this so what we are doing in here is one object creation so we are creating a Pac-Man game in here the next thing we are doing in here is object creation so we are creating a game Runner object however one very very important thing we are also doing in here is wiring of dependencies what do I mean what is wiring of dependency right a game Runner class needs to be able to run a game it needs a game to run and therefore what we would typically say is that game is a dependency so we'll say a game a specific game for example a Mario game or a super contra game or in general gaming console a gaming console is a dependency of the game Runner class and what we are doing in here is we are injecting the dependency in the game class is created and it is injected or it is wired into the game Runner class while this code looks very very simple there is a lot which is happening in the background of it when you talk about Enterprise applications you will have thousands of classes and you will have thousands of dependencies that are created and thousands of dependencies that are injected wherever they are needed now let's try and experiment instead of creating these objects instead of managing these objects instead of wiring the objects by ourselves manually how about getting the spring framework to do that that's the experiment we'll start from this specific step so what we are talking about in here is the fact that right Nerf the creation of the objects is completely managed by us these objects are created in the jvm Java virtual machine and the code to create these objects and wire them together is written by us what you want to get to is a state where we would have the spring framework manage all these things we want spring framework to create the objects for us and wire them together and before we would start with the game Runner examples what we will do is we will start with a much simpler example we will have spring manage easy objects for us things like name age person will create couple of persons and their address so are you ready to get this spring framework to manage these simple objects for us let's get started in the next step ooh I hope you loved the last few steps do you know more than a million Learners are pursuing in 28 minutes courses go to our website www dot in 28minutes.com and click on road maps to check out all our amazing roadmaps we have wonderful road maps to help you get started with Cloud devops programming and variety of Frameworks don't stop learning don't stop having fun now let's get back to the next step in this step let's create our first spring Bean what we want to do is inside the jvm you want to create a spin context and you would want to be able to have a bean with the name name created it would be a string value and we'll have the spring framework manage it for us let's get started this is the class we have write of app gaming basic Java what we want to do is to create another class like this before we go any further what I'll do is rename this right click refactor rename and I'll call this app zero one gaming basic Java this looks good I'll press enter and I'll say continue so the class is now renamed it's app01 giving basic Java and I'll also delete the application class which was created for us when we created the spring project we don't really need this for now so I'll say right click delete so learn spring framework application.java and go ahead I'm going ahead and deleting it so right now we just have one Java application that we can launch and that's app One gaming basic Java and if I run this you know what the output would be right right now it's running the Pacman game and you see the actions from and you can see the actions from the Pacman game in here that looks cool so what I'll do is nav copy and paste this Ctrl C Ctrl V and what I'll do nav is rename this to app 0 to so I'll call this hello world Spring so this is the hello world Spring app to help us learn spring and what I'll do now is to open up app zero to hello world Spring and let's remove all the code which is present in here we don't really need to worry about this code I'll organize Imports as well and save this so we already have a jvm running right if I run this application a jvm would be initialized and inside that we would want to create a spring context and we would want to manage a specific thing name we want the spring framework to manage that how do we do that so the steps which are involved are one launch a spring application or spring context as it is called and the next thing is to configure the things that we want spring framework to manage so these are the things that we would want to do so this is one and this is 2. one of the approaches that we can use to configure the things that spring needs to manage is to use something called a configuration class so we can create a configuration class and have everything defined for example name defined in here and we can use the configuration class to launch a spring context let's see how to do that right now let's start with creating a configuration class so I'll go to the common 28 minutes learn spring framework package and say right click new Java class and I would create a hello world configuration class so let's create a hello world configuration class and say finish and this is a spring configuration class how do you indicate that you can indicate that by adding an annotation at configuration I've added in add configuration and I would do an organize inputs you would see a import coming or spring framework context annotation configuration as you can see the Java dock in here it says indicates that a class declares one or more Bean methods so in here you can Define spring beans the things which are managed by Spring are what are called Spring beans and you can Define the methods to create the spring beans in your configuration class so we now have the configuration class ready that's that's awesome so we have this one ready we have a add configuration class now we would want to launch a spring context with the add configuration class how do we do that the way we can do that is by using a class which is annotation config so if you type in annotation config and press Ctrl space or command space you should see annotation config application context come up so what we are creating in here is a spring context so we would want to create inside the jvm a spring context and we are using a configuration class to create a annotation config application context so you want to create a new instance of this using the configuration file that we have created the configuration file that we have created is Hello World configuration the configuration is empty we'll fix that very very soon but for now let's focus on launching a spring context so I'll say hello world configuration dot class and I'll take this into a variable context variable context is equal to new annotation config application context and we are going to launch hello world configuration so the context is now ready let's see what would happen when I launch this up so right click run as Java application the great thing is there are no errors there are a lot of things happening in the background but we see no errors in here that's awesome so we are now able to successfully launch a spring context using a configuration file so what we have right now is this state we have a jvm and we have launched up a spring context and what we want to tell spring is to manage a bean I would want spring to manage a name object how can we do that let's go to our hello world configuration and over here I can create a method public string name is a string object so the return type is string and I'll say name and I'd say return and the value let's put ranga in here is this sufficient to make it a spring bean inside a configuration class I created a method the answer is no you need to do an additional thing called at Bean if you do organize inputs you would see org spring framework context annotation Bean come into picture and you can see the javadoc which says indicates that a method produces a bean to be managed by the spring container you can see that there are a lot of ways we are referring to Spring we are calling spring contacts spring container there are a lot of terminology which are used we will talk about it a little later for now the important thing to focus on is the fact that this specific method we have in here will produce a bean and this Bean will be managed by the spring container so let's go ahead and save this and what I'll do is go back to our hello world Spring make sure that you are running the right thing it's app zero to hello world Spring let's go ahead and run this right click run as Java application and you'd see that there are no errors again now how can we see our name object which is managed by Spring we have the spring context ready in here so what we'll do now is use the context to get the object so what we are doing now is we have created the name object and it is being managed by spring so what we'll ask the spring context is give me the name object and the way you can do that is by saying context dot get being anything that is managed by Spring is called a bean so we will say context.getbean and there are multiple ways you can retrieve values from a context one of the ways is to use the name the name that we have given in here is name the method name is name so this is exactly what will be returned in here as well so context.getbean name and what I'll do is do assist out of this so let's do a system.out.println of context.getbean name so what we are doing in here is we have launched a spring context that's done and tested we have configured the things we want to manage where did we do that we did that by creating a hello world configuration class and in the hello world configuration class we have The annotation at configuration and inside this we also created a name method with the at being annotation what we are doing now is retrieving beans managed by Spring and the way we can retrieve the beans is by using context.getbean and giving the name of the bin there are a lot of other methods we'll be exploring a little later for example by type and things like that for now let's keep it simple and do a context.getby name and let's launch this app right click run as Java application what do you see ranga isn't that awesome so what we are doing right now is spring is managing this specific Bean for us so we have our jvm and spring is managing a specific object name for us there are a lot of other wonderful things that you need to understand about spring framework in this step we got started with the spring framework there might be a number of questions at the back of your mind I am sure of that so let's explore all those questions in the next few steps in the last step we created a spring context and we have spring managing one custom object for us do you want to create more custom objects and we would want to get them managed by Spring let's get started with that in this specific step I'll go back to our configuration file so we would want to create more beans where do we create them obviously we'll create them in the hello world configuration class so let's say I would want to create another bin right until now we have created a string bean we want to let's say create a integer so this would be a public int age and I would want to put my age in here let's make me really really young I'll put 15 in here isn't that awesome so the age Bean will be managed by Spring and I would want to retrieve the value from the context how can I do that system.out.printellin pause the video and try it as an exercise it should be easy you need to just rename the name to age and you can do a right click run a Java application what would happen 15 is printed in here so we now have spring managing a couple of custom objects let's add more let's see if we can have spring managing an object of a custom class so what I'll do is to create a record so let's create record and let's say a person has a name and a h and let's add the curly braces and put a semicolon in here now what is the feature that we are making use of in here what is the record a record is a new feature which was introduced in jdk 16. typically whenever we create a Java class we would create a number of getter Setter methods Constructors we would need to create an equals hash code and we'd also need to create a tostring method however to eliminate the verbosity in creating Java beans this was introduced this feature is called record and when you're making use of a record you don't really need to create a Setter get a Constructor or a lot of things they are all automatically created for us over here we are defining two member variables string name and int age so a person Constructor with these two arguments is automatically created for us let's see that in action I'll say at bin and I'll say public we are going to return we are going to create a person so I'll say person and I'll name it as person you can call it ranga or whatever you'd want as well I'll just leave it at person right now and over here we want to create a person so where person is equal to new person of let's say I want to create a new person in here Ravi and let's make the age of Ravi as 20 little older than me so let's make him 20 years of age and now I can say return person so you can see that and Constructor is automatically created for us and you'd see that you'd be able to get the values as well so getter methods are also automatically created you can say person dot for example name or you can use person.age so these getter methods are also available for you let's not really worry about the records feature for enough let's focus on spring so let's get back to what we were talking about about spring so what we are creating in here is to create a spring Bean called person and we are having spring manage that by putting an ad Bean annotation in here now right now we are doing a VAR person is equal to new person of all that so let's not really worry about it let's actually go back to saying return new person of this and save this and now how do I get the person back in here so I can say system.out.printel and context.getbean person this would help me retrieve the person so right click run as Java application and you can see the person in here you can see that a two string method is automatically implemented for your record and it is writtening the values of the attributes which are configured for the person so name is equal to Ravi and age is equal to 20 is returned back now what I would recommend you to do is to pause the video in here create a record of type address let's say the address contains two string values first line and City typically addresses are much more complex than that let's keep things simple let us just say it has only two string values you can create a record with that and you can add it as a bean into your spring context how can you do that it should be easy record address ing first line comma string City and we already have the person example in here address and address and let's say I want to create an address of my favorite fictional character which is Sherlock Holmes Baker Street London okay this looks cool oops I see a compilation error what's the reason for the compilation error because I got the syntax right I forgot the braces in here so you need to have two braces in here if you want you can define a few custom methods in here for now we are not going to do anything like that so we'll keep it simple empty braces save this you should see that everything compiles let's go back to app zero to hello worldstring.java and let's say get bean address right click run as Java application you'll see it was printed in here first line is Baker Street cities London hunky dory in this step we have done a few boring things we have created more beans to be managed by Spring we also created a couple of custom classes and we have objects of theirs managed by Spring as well now this might be boring but this is the foundation for a lot of interesting things about spring that you need to understand let's get started with them in the next step in the last step we got spring to manage a number of beans for us name age person address this is the code that we have in here so we have name age person and address in here one of the things you would have observed is the fact that the default name of the bean is the name of the method what if you'd want to change this let's say instead of address in here I'd want to change the name of this to address 2. the way you can do that is by adding an attribute on ADD pin so you can say add bin name is equal to and you can configure your custom B name so you can configure whatever custom B name you would want for example if I would want to let's say call this address 2. I can do that so enough what would happen if I actually run this I'm looking for address in here so let's see what would happen if I run this so I'm running app zero to hello world Spring ooh exception what does the exception say so it's printing the person object but when it's trying to retrieve the address object it says no Bean named address available why because we have renamed it to address 2. let's do this and run this right enough and you can see that the address object is now printed however the address object nav has a b name of address 2. so you can customize the B names according to your needs the next thing we would want to explore is have you retrieve the beans back from Spring context over here what we are doing is we are making use of context.getbean passing the name as an attribute there are alternatives for example you can use the type as a parameter as well so instead of saying context.getbin atlas2 I can pass in a class so I can say address dot class I want to get a bean of this specific type address dot class let's see what this would print run as Java application you can see that the same address is printed again so once spring starts managing a bin you can either use the name of the bin or you can use the type of the bin to be able to retrieve it back to be able to get it back and play with it so what we are looking at right now is the fact that spring allows you to Define multiple beans and it provides a wide range of approaches where you will be able to retrieve the objects which are being managed by spring now a question you might be thinking about is what if there is relationship between these objects until all the beans that we have created are independent to each other there is no relationship between them at all now let's say I want to create a new person being so I would want to call this person as person two however for his name I'd want to use an existing Bean which is already managed by Spring there is already an existing Bean managed by spring with the name as name and there is an existing age which is in here as well I would want to use these two values and I would want to create a new person I would want to create a new spring Bean with a relationship to existing spring beans how can I do that there are two ways you can approach that problem the first one is through method calls so I'll call this person to method call so the mean name will also be the same person to Method call all that you need to do is very very simple call the appropriate method so name and each so name comma H so person to Method call let's now retrieve the new Bean as well so I'll say system.out.printle and context dot get bean what's the name it should be person to Method call so we are getting the details related to person two using a method call and that's why we have named it as bin and that's why we named it as percent do method called interesting let's run this nice you can see that the second person is printed with ranga 15. now let's take this a step further what we'll try and do now is actually add an address to the person object so what I'll do is I'll add address so we are adding a parameter to the person constructive as well so there would be compilation errors over here in case of Ravi let's say I'm hard coding the address in here so I might hardcore his address as new address let's say Main Street you clicked and over here I want to use the address pin which is already managed by Spring and the way I can create relationship is by calling that specific method so let's do a run as let's go ahead and run the application nice you can see that Ravi is created with an address Main Street Utrecht and runga is created with the address Baker Street London so what we are looking at is the fact that you can create beans reusing existing beans which are already managed by the spring framework over here we are making use of the name Bean age bean and address Bean and using them to create a new bin person 2. the approach which we looked at until now is to call the Bean method directly there is an alternative approach as well what's the alternative approach let me copy this paste it in here and I'll call this person three and I'll name it as parameters do you want to guess what it is yep it's as a parameter so I can configure parameters in here with the right names so let me pick up the names so it's the mean name is name in here so the parameter name should be name as well this one is age right so we need to have age in here and for the address the name is little different right it's address 2 so I would need to use address to in here even though the method is address the name of the bin is address 2 so we need to make use of address 2 in here so we need to pass in parameters in here so I can say string name and int age and address what should be the variable name it should be address 2. so let's get this right let's save this and over here actually I need to make another change over here I do not need to call the methods actually I would need to use the parameters so name age and address to so instead of calling the methods directly so instead of calling the methods directly we are adding in parameters this is an alternate way where you can take a bean which is managed by spring so this is managed with the name name this is managed with the name age this is managed with the name address 2. I'll take those beans and I would inject them in I will Auto wire them in and to create this basic bin these values will be made use of so to create one spring Bean we are making use of other existing spring beans and let's go ahead and run it what do you see no change in output now let's go ahead and add the new thing in here so person to Method call the next one is person three parameter make sure that you're getting it right so it's person three parameter right it's personally parameters actually so let's get that right percent three parameters and run it okay the person 3 is printed in here it has the same values as person two method call if you want you can make the values a little different as well so instead of actually having address 2 let's create an address 3 and let's say this is moti nagar Hyderabad [Music] and over here let's use address 3 instead of address true so address 3 and let's run this oops there's an error call this addressary and let's launch the application app error what does the error say expecting a matching Bean but but found two address two and address 3 which line is causing that error the line which is causing that error is the specific line context.getbean address dot class for now I'll comment this out I would recommend you to think why this is causing an error we will play with it a little later when I go ahead and run this and you'd see that the program would successfully run and you would see that the third person has a first line of moti nagar and Hyderabad and that's the new address 3 that we have created in this step we understood a few important things about configuring your beams the first thing we learned is the fact that you can configure your own custom names for your beans so instead of just using the default method name you can configure your own custom name the second thing we learned was the fact that you can retrieve the beans from a spring context in multiple ways one of the ways is by using a bean name the other one is to use the type or the class name as well the third thing that we learned is that you can use existing beans which are managed by Spring to create new beans so we are now creating a person 3 in here using existing beans which are managed by Spring framework while we are doing everything we also faced a problem we saw that if we uncomment this line of code there was a problem it was throwing an exception saying multiple beans found let's see how to fix that and a lot more in the subsequent steps in the last few steps we have played with string and spring beans there might be a number of questions you might be thinking about right now question one we use the terms container context a number of times what is the spring container what is the spring context what is ioc container what is an application context number two we use the term been a number of times as well there is a term called Java Bean and there is a term called Spring Bean what is the difference between these number three how can I list all the beans which are managed by Spring framework we have been adding a number of beans to our spring context how do I list all the beans that are managed by spraying framework question four what if multiple matching beans are available what do I mean earlier app to helloworldspring.java if I uncomment this line of code and execute it error it's saying I'm searching for an address dot class but I am finding two matching beans address to and address 3 y because over here we are looking for the type of The Bean the type of The Bean Is the class of the bin which is address dot class and if I go into our hello world configuration there are two matching address beams address 2 and address three which one of these should spring use and Print in here let's commit this line of code let's get it to be in a working state but that's the question that we are talking about in here what if multiple matching beans are available which one should spring use how can I tell spring to prioritize one over the other question five spring is managing objects and also performing Auto wiring awesome but aren't we the ones who are writing the code to create objects if you look at the hello world configuration who is writing the code to create the objects return new person return new person written new person in here written new address we are writing the code to create the objects but shouldn't ping start creating objects for us how do we get spring to create objects for us these are all amazing questions and we will find answers to each one of these questions as we go further in my opinion spring is one of the most difficult Frameworks to start with especially because there are so many Concepts to understand I'm really really delighted that you are making progress over the last few steps you understood a lot of spring Concepts and you'll be understanding a lot more as we go further in this specific section I'm really really excited to make spring easy for you and I'll see you in the next steps with answers to these questions I'm sure you love the last few steps do you know I love traveling and I'm a regular on Instagram go ahead check out our Instagram to know about all the fun things I do you can find a link to my Instagram profile on my website rangakarnam.com don't stop learning don't stop having fun now let's get back to the next step let's answer question number one in this specific video what is a spring container earlier we also called it spring context a spring container is the one which manages spring beans and their life cycle we have created a number of java classes and we also created a configuration file and we passed them in as inputs to The Container to the spring container so what do we have in here we created a couple of java classes we have created a configuration file a hello world configuration containing all the definitions of the beans these are the inputs to create a spring container the output of a spring container is a ready system it's a system of this kind inside the jvm you have a spring context which is managing all the beans that you have configured this is how it would look at runtime once you create the Java classes once you create the configuration the ioc container creates the runtime system for us that is the one which is creating the spring context and managing all these beans for us so a spring container manages spring beans and their life cycle there are a lot of different terminology which is used for spring container it's also called Spring context it's also called ioc container ioc stands for inversion of control we will discuss inversion of control a little later but for now the important thing to remember is that whether people Call it spring container or spring context or spring ioc container they are referring to the same thing they are referring to the thing which takes your classes and its input and creates a running system and when we are talking about spring containers or spring ioc containers there are two popular types of ioc containers the number one type is bean factory it is the basic spring container number two is application context it's an advanced spring container with Enterprise specific features if you're building a web application or if you want internationalization features or if you want good integration with spring aop or spring aspect oriented programming in that kind of scenario you would go with application context typically most Enterprise applications need all these features and that's why you would see that application context is the one which is most frequently used Even in our code what we are making use of is an application context we are creating an annotation config application context what you would see typically is that most Enterprise applications use application context application context is recommended for web applications web services rest API and microservices as well over my two decades of using spring framework I have built several applications using spring and I have not used bean factory directly even once the only use case I can think about for bean factory is some kind of an iot application where you are severely constrained for memory intercept we got a 10 000 feet overview or spring container we will discuss a lot about spring containers as we go further in the course one of the popular questions that I am frequently Asked is what is the difference between Java be in pojo Spring Bean let's explore that in this specific step I've already created a very very simple class you don't really need to do everything that I do in this specific step it's just to understand the difference between pojo versus Java Bean versus spring Bean just watching the video should be more than sufficient so what I've done I've created a Java class it's just any Java class it has a few member variables it has a method to string and over here what I am doing is I'm creating an instance of that specific class and doing a CS out so let's do assist out and say pojo now there's a main method in here so if I do a run as Java application it would print the values in that specific pojo it's initialized with null and zero so that's cool so this is stands for plain old Java object any Java object is a pojo as simple as that almost every beam that we have created in the course until now is a pojo a plain old Java object now if you want to create a Java Bean there are more restrictions so let's say I copy this and I would want to create something called a Java Bean Java Bean it's little bit more complex this is especially because of something called ejbs Enterprise Java beans if you go back 15 years ejb was one of the popular approaches or one of the popular architectures to implement Java applications not a lot of people use it right enough so ejps were popular earlier and ejbs introduced this concept called Java Bean and when you call something a Java Bean there are three very very important restrictions on it one is that it should have a no argument Constructor so this one already has a no argument Constructor by default even if I don't provide one Java would provide one for me so this is having a no argument Constructor by default so even if I remove this that's fine so I'll add it to be really really explicit so the first constraint is public no ARG Constructor it can either be the default no or Constructor or you can explicitly create one the next constraint is that you should have getters and Setters so for this to be a Java Bean I need to go ahead and say right click Source generated Getters and setters select all and say generate oops let's get them right so get us and setters Above This so this is number two so one is public Noir Constructor two is Getters and setters and the last one is something called serializable so you did go in here and say implements yeah realizable and serializable is an interface if a class wants to be serializable it can go ahead and implement this specific interface as you can see in here if I open up serializable there are no methods which are present in here so you just need to declare that it implements serializable now whatever we are seeing in here is what is a Java Bean so if a class contains a public no argument Constructor if you have Getters and Setters and if you are implementing serializable interface then an instance of your class will become a Java Bean to be really really thanked the concept of java bean is not really really important anymore because not a lot of people are making use of Enterprise Java beans the two important Concepts that you need to worry about are pojos that's basically any class that we are creating in here and spring beans in last few steps we have launched up a spring context and inside spring we have created a number of beans all these are what are called Spring beans so anything that is managed by Spring framework is what is called a spring Bean now that we have a little bit more background into pojo Java Bean and spring Bean let's compare them so a Java Bean is nothing but any class that adheres to three constraints it has a public default Constructor a no argument Constructor it allows access to its properties using getter and Setter methods number three is it needs to implement Java i o serializable pojo classes don't have any constraints any Java class if you go ahead and create it it becomes a plain old Java object no constraints at all and any Java object is a pojo spring Bean is any Java object which is managed by Spring as we saw earlier spring uses ioc container bean factory or application context to manage the objects any object which is managed by the ioc container is a spring Bean in this step we talked about the differences between Java Bean pojo and spring beans I'm sure you're having a wonderful time and I'll see you in the next step having explored question one and question two it's time to think about question 3 and question four how can I list all the beans managed by Spring framework and what if multiple matching beans are available let's look at that in this specific step let's start with question three how can I list all beans managed by Spring framework so the question is how can I list all spring beans which are also pojos right so let's get to it right now so if you want to get a list of spring beans the way I can do that is to ask the context so context dot get bean definition get bean definition names Returns the names of all the beans defined in this registry you also have a get bean definition count which Returns the number of beans that are defined in the registry if you want to check for a specific Bean you can also say get bean definition and pass the name of the bin as a parameter for now let's focus on get bean definition names and if you see the return value of bean definition names it's a string array and what I want to do is to loop around this and print all the things which are present in here and to be able to loop around them easily what I'll do is I'll use functional programming so I'll use java.util.arrays Dot stream and I would want to create a stream with this array context.getbean definitions dot names once we have the stream what I would want to do is to do a DOT for each for each element in this stream I want to execute system dot out dot println system dot out dot println if you are new to functional programming this code might look a little complex we have a fraction on functional programming in the appendix you might want to refer to it and understand functional programming a little more almost every Enterprise out there is using Java 8 and above and therefore understanding functional programming is really really important if you understand functional programming you'll be able to solve problems really really easily so what we are doing in here is a result stream and for each element in the Stream we would want to execute system.out.println how can we do that so I can put in system.out colon colon print Ln what what we are using in here is called a method reference and this would be sufficient for us to print all the bean names so right click run as Java application you should see all the pin names that are managed which are printed in here these are internal to Spring let's not really worry about them the ones which are interesting for us are the ones afterwards this is the configuration file that we have created Hello World configuration even that is managed as a bin and these are all the other beans that we have created in the configuration file name age person person two method called person three parameters address 2 and address three so that's how you'd list all beans managed by Spring framework the next question is what if multiple matching beans are available we saw that in action in here right if I uncommit this specific line of code let's comment 35 and 36 and let's uncomment 32 system.printel in context.getbean address dot class what would happen right click run as Java application what happens is a exception and it says I'm expecting single matching Bean but found 2 address 2 and address 3. now if I do something similar with person let's say context.getbean person.class let's run this what you'd expect or do you expect yep same exception but it's now finding three it's saying I'm finding person to Method call and person three parameters I'm finding three objects with the same class now how can you help spring framework give priority to one of them let's now go over to the hello world configuration class this is where person 3 parameters is configured and person three parameters right now is making use of address 3 Bean we are using the bean name in here what I'll do is I'll create another bin I'll create person 4 parameters string name intage and instead of address 3 I'll say address in here and I'll use address in here as well the thing is there is no bin with the name as address so we have a bin with the name as address to and address 3. however there is no bin with the name as address what would happen for this let's see what's happening let's run the app to hello world Spring you can see the same error no unique Bean definition exception and you would see the error something of this kind no qualifying Bean of type address is available expected single matching bin but found 2 address 2 and address 3. so whether you are doing Auto wiring of a method parameter like this or you are trying to fetch an object from a spring context if you have multiple matching beans these are called candidates if you have multiple matching candidates in that kind of scenario spring would throw an exception the way you can fix this problem is making one of these as primary as far as addresses are concerned we have multiple beans address 2 and address 3. so what I can do in here is I can make this a primary how can I make this a primary I can go ahead and say at primary and organizing ports org spring framework context annotation primary so this would solve the problem for address and now if I go and run the application app 0 to hello world Spring you should see only one problem so you can see that the address is resolved properly however the person still has a problem because there are multiple persons in here so you have person person two method call person three parameters person four parameters you have a lot of persons and you have to make one of them primary as well so let's say we make person for parameters as the primary one let's make him primary and let's organize inputs and let's run this now you'd see that the application runs fine so if you have multiple candidate beans in that kind of a scenario you can use add primary to indicate which of them is most important and that's what we are doing in here in addition to primary there is another option to resolve this problem that's at qualifier for example I can say at qualifier in here and give this a specific qualifier name so I'll call this address 3 qualifier let's organize inputs this would Import in org spring framework beans Factory annotation qualifier that looks cool I'll save this and now I can use this qualifier to outer wire the object so what I'm saying in here is when you are Auto wiring this address in I don't want to use the default one I want to use something different that's where I can make use of the qualifier so for example over here I'll copy this I don't want this to be the primary so I'll remove add primary I'll remove the comments and I'll call this person five and this one is using qualifier so personify qualifier string name intage address I want to use this specific thing so I don't want to use this beam which is present in here so what I would do is add in what I would do is add in this qualifier so I'll copy this and paste it in here so string name integer at qualifier address 3 qualifier so that's what would be Auto wired into address and we are creating a new person with the name which is coming in here and what we would do is we would get the value of person 5 qualifier so I'll print that as the last one so we would want person 5 qualifier being I'll copy this would be the last object which is printed so let's put it in here and run this you can see that the last one which is present in here has the values from address 3 moti nagar Hyderabad so if I go into this so address 3 has both in a Hyderabad if you have multiple matching beans in that kind of scenario the first option would be to make one of them primary one of them most important the other option is to also make use of qualifier you can put an ad qualifier on the specific bin and use the add qualifier when you are Auto wiring the bean into another Bean in this step we answered questions three and four how can I list all the beans managed by Spring framework and what if multiple matching beans are available I'm sure you're having a wonderful time and I'll see you in the next step in the last few steps we played around a lot with spring framework and also how you can configure different kinds of beans with spring framework it's now time to apply that knowledge to our gaming application we have built an app01 gaming basic Java earlier this is used to launch different types of games and now we would want to launch this app using spring framework what we'll start with doing is to refactor and move the hello world things into a separate package so what I would do to start off with is create a new package so I'll go here and say right click new package I'll call this dot hello world and say finish so we now have a new package what I'll do is I'll move the hello world files app zero to hello world and hello world configuration into that specific package so let's move them in and let's say okay okay so we now have app zero one gaming basic java.java and we have the game package where all the game related things are defined and we have a Hello World package where we have the app zero to hello world Spring and hello world configuration.java defined and if I launch this up and organize Imports and right click run as Java application you should see that it would run successfully that looks cool there is one more refactoring that I would want to get to before I would get to the actual problem at hand so if I open up app to hello world Spring one of the things that you would see in here is a error it says resource leak context is never closed what I can do is I can put a try catch block and in finally I can close the context so that's one of the approaches to fix this however there is a new feature in Java called try with resources let's make use of that to fix this error so what we will do in here is try and you can put parenthesis around and you can put this code in there so this code would put it in here so let's format this so that you can look at it easily so where context is equal to this this is the entire code that we had other than the same equivalent before I am putting it within type open parenthesis and close parenthesis and I need to put the rest of the code which uses the context Within open brace and close place so what I'll do is I'll remove this and take all the code that we had earlier and I'll put it within the try and let's also format it so that it's enough if I save this you would see that the warning disappears so this is called try with resources if there's an exception that happens inside the code context.close will be automatically called this looks cool so we are all set enough to do something very very similar to what we are doing in here for the basic gaming app what I would recommend you to do is to pause the video in here and try it as an exercise I hope you had a chance to do that as an exercise what you want to do is to do the same thing that we were doing in absolute giving basic java.java using spring let's do a control C control V of that specific class so I'll call this app to gaming actually it should be app3 because we already have app2 app3 gaming this would be spring beans so you don't want to create app3 gaming spring beans at school and we want to create all these as part of our spring configuration and to be able to do that we need to follow two steps one is to create the configuration file with this and then create a spring context using that specific configuration so let's start with creating a configuration file I'll create a new class and I'll call this gaming configuration and let's go ahead and say finish in here we want to create the beans right so let's take the easiest way so we'd want to create these things in here there will be a few compilation errors let's not worry about them so at configuration is the configuration annotation so this is a configuration file this is a configuration file containing spring beans so let's start defining spring beans at Bean so what's the mean that we would want to create to start off let's not really worry about rest of the things I'll comment rest of the things and I'll just create a Pacman game let's take it one step at a time so I'll come in this and let's just create public a gaming console game and I would return the Pacman came back let's organize inputs you would see org spring framework context annotation bean and you would also see the Pacman game and the gaming console imported in now let's also return the game back return game this looks good so we have a gaming configuration where we have a game defined and I'll go to the class which we created earlier so comment this code for enough and what you want to do is to create a spring context Let's do an annotation config application context we'd want to use this spring configuration which we have defined in here gaming configuration so let's go ahead and use that gamingconfiguration dot class so new of this right so that's what we would want to do and this is the context so where context is equal to this and now I can get the bin so context dot get bin and the type of The Bean I'll use the name of the interface gaming console dot class and let's call the up method on it for example so let's call the up method on the bin which is written back now what would happen if I run this right click on a Java application yup app is printed in here so what we have done until now is created a simple spring context right so we have actually created a configuration file gaming configuration where we have defined a gaming console game the specific game we are making use of right now is Pacman game and we are launching this as a spring context using the class annotation config application context as usual you'd see an error it says resource leak context is never closed let's put it let's put this in a try with resource block so try and the thing in here the code we want to execute inside the try is this and the variable is this so let's go ahead and paste that in and let's close this off let's format this a little bit more so we are creating a config so we are creating a spring context and we are picking up the gaming console from here now what we want to do is not just the gaming console right we want to pick up the game Runner so we want to be able to create an instance of the game Runner as well where do we create that yep we'll need to go to the gaming configuration again so over here what we want to do is to create a game Runner class go in here had been we'd want to create a game Runner so this would return a game Runner game Runner would be the name of the method and we would want to do this code in here that looks good if I organize inputs you'd see that there is just one error present how do you pass the game in how can we pass the game in one option would be to directly call the game method from here the other option we also talked about is to pass the game as a parameter so I can Define gaming console game as the parameter in here right what would spring do spring would take this Bean which is created with the name Bean and it would wire it in here so it will pass it as a parameter to this method call so when the game Runner Bean is created this would be this would be passed in as a parameter and the game Runner Bean would be created using the specific game so let's do a return game runner in here so what we are doing in here is creating a Pacman game and wiring it into the game Runner and I will remove all the commented code out here let's also format the code so that everything is fine you don't have any unused inputs in here let's go over to app03 gaming springbeans.java now we want to pick up the game Runner class and run it so let's remove the unused code and everything else which is present in here let's organize Imports it's format code ah I don't really like this formatting you can't see enter code so let's go back to the older State this looks pretty good and now what I would want to do is context dot get bean game Runner dot class dot run that's what you want to do you want to get the game Runner bean and run it let's see what would happen nice you can see running game and you'd also be able to see up down left and right so what we are now doing is we are launching up the game and the game Runner as spring beans so what we are doing right now is that game and game Runner are spring beans and we are picking the beams from the spring context and running them let's now get back to our questions again question five spring is managing objects and Performing Auto wiring but aren't we writing the code to create objects how do we get spring to create objects for us so if I look at the code right now in the gaming configuration we are writing the code to create the objects so the new Pacman game and the game Runner we are writing the code to create the objects spring is managing them for us but we are creating these objects so how do we get spring to create these objects for us a much more important question is question six is spring framework really making things easy or is it making it difficult right if you compare the earlier version of the code if I compare gaming basic Java zero one this is pretty simple code and we have replaced it with a configuration file having a lot of information and we have a app 3 gaming spring beans which has a lot of code as well so is spring really making things simple or is it making it complex we'll have all the answers to those questions in the subsequent steps in this section I am sure you had a great time learning some of the fundamental concepts related to the spring framework we learned what is Diet coupling how we can make code Loosely coupled we looked at how to make use of java interfaces and we brought in our spring container we created an application context and we made use of a number of spring basic annotations we looked at what is at configuration we looked at add Bean we launched up a context with a number of beans and we learned about a lot of other different spring annotations as well we also understood a few very very important Concepts in Spring one of the most important Concepts that we have learned is auto wiring we also understood the difference between a Java Bean and a spring Bean in my opinion if you understand spring framework really well you are well on your way to become a great developer the concepts you are learning now will not only be helpful in designing great string based applications but also in debugging problems for years I've been thinking about designing something awesome to help you learn all the important spring Concepts in highly Visual and very Hands-On way what you are seeing now is a result of that effort I had to run through an entire set of examples multiple times to ensure that everything is smooth as silk I am sure you are having a great learning experience now what are you going to learn next next section we are going to focus on making the code much more simpler right now there is a lot of code involved in creating a simple bean how do we make it simple that's number one number two we'll also understand a number of other spring annotations we will look at at primary and at qualifier in depth and we will also bring in a number of annotations like add component and and several variations of AD component we'll also look at a lot more terminology related to Spring framework we'll understand dependency injection and we'll understand the different types of dependency injections which are supported by Spring framework and at the end of the section we'll also look at a real world example with spring are you excited to learn more about spring framework let's get started let's now think about one very very important question what are the different things that a Java developer has to learn to call himself a great developer today he needs to understand the fundamentals of spring framework dependency injection inversion of control how to create a spring container how to manage beans using spring framework he needs to understand spring boot spring boot makes it very very easy to make use of the spring framework if you can do something with spring framework using thousand lines of code in Spring boot you can do the same thing with 500 lines of code so spring boot next using spring framework really really easy so you need to understand the fundamentals of spring framework you need to understand how to save data to the database spring data spring data jpa hibernate jpa these are all the things that you need to understand after that you need to also understand other spring modules like spring aop spring jdbc and a few others knowing the Frameworks is not sufficient you need to know how to build applications you need to know how to build a web application you need to understand what is rest you need to understand how to build a rest API and you also need to understand what are the rest API best practices and today it's also expected that you are a full stack developer react is one of the most popular front-end Frameworks so having good knowledge of react and how to connect it with a spring boot backend will be a great add-on you'll also need to be good at writing great unit tests in addition you need to know how to containerize your application you need to know how to build a container image and run containers and cloud is everywhere today you need to know how to deploy your applications to the cloud so whether you're building a web application a rest API a full stack application you need to know how to containerize it and you need to know how to deploy it to the cloud for example AWS as you can see there are a lot of things that you would need to learn to be a great Java developer today earlier in 28 minutes had almost 10 courses where we covered these aspects in depth what we found is that developers found it really really difficult to navigate through all these courses and that's why we decided to make it very very easy for you we have launched an amazing course which cover all these features in just one course so in just one course you will understand the fundamentals of spring framework spring boot hibernate jpa spring data you will understand some of the important spring modules out there and you will learn to build several applications web applications rest API full stack application and you will also learn how to containerize them and you learn the fundamentals of one of the cloud platforms which is AWS and you will deploy all these applications to AWS isn't that awesome our goal with this course is to make it very very easy for Java developers to become great Java developers the conceptualization and creation of this course took about a couple of years I'm sure you'd love it go ahead and check it out right now I'm sure you're having a great learning experience and until I see you again here's by from ranga at in 28 minutes
Info
Channel: in28minutes - Cloud Made Easy
Views: 9,980
Rating: undefined out of 5
Keywords: in28minutes, ranga karanam, springboot, spring
Id: f6DHAgL7FWc
Channel Id: undefined
Length: 103min 12sec (6192 seconds)
Published: Fri Dec 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.