welcome back aliens my name is z readyy and in this video we'll talk about spring framework when you talk about the Enterprise Market the most famous language there is Java because when you want to build huge application which are scalable secure Java is preferred and the one of the biggest ecosystem for the Enterprise applications is in Java and the framework the most famous framework for Java is spring now when I say most famous there are other Frameworks as well which are quite famous but not more than spring because it Sol solves the problem of developers see spring started in 2003 it's not a new framework right it's it's old it's been 21 years now the reason it started is to solve the problem of java e Java e is used to build Enterprise application and it was great the only problem is it was complex and heavy to make it lightweight to make it simple spring came up with a project and of course initially it was a project but later on they added some more projects so initially it was spring framework which provide some features and later on they started adding more modules to it and now when you say spring is not just a framework or a project it's basically an umbrella it has multiple sub projects to it and whenever you want to build Enterprise level Solutions and not just that if you even if you want to make a simple application you can use spring one of the issue a lot of people talk about about spring is it's heavy it's you have to write a lot of codes in Java not with the latest version of java and not with spring framework spring basically has something called Spring Boot and using that you can build your first API in minutes maybe 2 minutes is that fast spring framework it's fun it has multiple modules so let me just go head towards the spring framework or spring website so you can go to spring.io and it has details about everything so what is spring uh you can see they have different events coming up ignore that uh so what you can do with spring is you can build microservices you can build reactive applications you can build for cl now they can build web application serverless I mean multiple options are there but the beauty is if you go to the spring projects now this is a project and if you expand this it's huge when I say huge I just have to click on view all projects and every time I go to this place there's a chance that there will be new project added example we got spring spring boot I was talking about it is one of the easiest way to build projects uh we got spring framework spring Cloud spring data Spring Security spring AI so yeah we can also build AI applications using spring now uh we got uh things which I used bring web services and there are other projects as well which you can see here initial days we used to theyed to keep everything in one section but now we have separate sections for it the extra projects so this are the projects which we have in spring so basically it provides you a lot of different features one of the best feature about spring and that's how it started is dependency injection what exactly it is we'll talk about in the upcoming videos the plan is to actually create the entire series for spring and you might be saying hey we already have that on channel what we have on channel is spring five we're talking about spring six and for that basically I need motivation of course it will take a lot of time and I need motivation from you of course I will motivate you to learn and practice it you have to also motivate me to keep making videos you can do that by commenting on this video also like the video so that I will know that yes I have to spend uh enough time and energy to make this series and you are waiting for it so let me know that by liking the video comment something in the videos maybe spring or waiting for next video I need comments that give me the motivation and it also helps the YouTube algorithm to promote the video right uh so you can do that in the comments and maybe the setting the target is a new trend so maybe let's say 50 comments let's start slow and then we'll build up so yeah in the entire series The plan is to actually talk about dependen injection spring code uh spring web you can build web application using spring and we have talked about the project here so we have that in the spring uh we can have web spring web we can also have spring data jpa for for database connectivity uh we'll talk about spring boot how do you build application so the the idea is to also make videos on Spring Security uh we have talked about spring data also the cloud applications Cloud deployment and again I need your motivation there we have talked about what is spring and then we have discussed that we can build applications the Enterprise application web application with the help of spring framework and before we talk about the actual topic and discuss what other components are let's talk about the project we are going to do of course right when you are learning something you want to do some project side by side now this is the project which you're going to build of course this doesn't look like a good UI but that doesn't matter we are learning here spring framework not HTML CES in JavaScript right so let's focus on the features of the back end and just to show how it will look on the front end of course designing can be different thing you can help me in that but this is what we're going to build now we are trying to build a simple e-commerce website of course we are not we cannot build Amazon here that's a huge application but a simple e-commerce website where someone can lease their products and someone can buy it of course we're not doing the end to end part of sending the product from the warehouse to the client but at least a a person should be able to add to the cart or something like that so this is what we're going to build of course from scratch and why learning a concept so let's say if we talk about one concept and we'll see how do we include that Concept in this project and slowly we are going to build this so this is not what you will see in the first video after this uh you will see something blank and we don't have anything there we'll build from scratch okay but before we talk about this uh let's set the target so as I mentioned before for the first video of spring we set the target for 50 comments uh this time since we got a good response let's set the target for 200 comments okay that's how I'll be motivated for the next video so please motivate me make sure that we complete the target of 200 comments so coming back here this is what we're going to build so this is the home homepage now on the homepage I can see certain products here I can see a laptop or a fan or a telephone or do of course they should have some images as well but we'll see how do we put that here later or you can help us if you are good with UI you will find the link in description so let's say if I want to buy a fan so if I click on fan here this is the detail about the fan uh this is a fan icon we got the product name the brand uh description and the price the then we got an option of R to cart then we got stock available five product listed on this date and even I can update this I I should be able to update this detail maybe I want to say the description is not just top speed but also silent fan or something like that so in that case I should be able to update it and we should be able to delete this example if I click on delete now it's gone you can see fan is not there okay so we have to refresh the homepage yeah so the fan is not there so this is how basically you can add update and delete the products from the page also we can sort this by category example here we got do in fashion so you can see the category is fashion here and if you see this telephone this is in electronics so we should be able to sort the products based on the category it's not uh complete yet but we'll build it we'll build to categorize it and if I go back to homepage this is what you will see if I go to Tesco here that takes you to the Tesco homepage this is the official page of Tesco go back here and yeah this is what I want also I should be able to search the product and it's not implemented yet no problem will do that cool so this is what we have now this is what you're going to build now if I want to show you the code for this this is the code for the existing project uh you can see we have two sections here we got a back end and we got a front end here uh let me close all the code files because I don't want to confuse you with the code files okay so we got a front end and we got a back end here now front end is built with react again this is not a react playlist so we are not going to focus on the react one but we I will show you how the react code looks like but uh not in this video in the other video and then this is the back end this is what we need to build so example let's say if I give you just a front end of course you will see the page but then the page will not have any products because the back end is not there so we have to slowly build this back end to give something to the front end okay that's what you're going to build and uh let's say if you don't want to build a front end you don't want to work with react you can also test this with your uh rest clients it can be Postman or or some other clients let me know in the comments with what client you have used before apart from Postman okay so this is what we are going to build and this is the basically a web application but then when you talk about spring we're not going to directly start with web application we'll start with the spring code what if you want to build a stand application and you want to achieve the same thing where you should be able to list the products or Del the products how we going to do that so we'll start with that and then we'll move towards the web part and remember the target for this video is 200 comments also if I can get uh 500 likes that would be awesome now till this point we have talked about what is spring and then we have seen the project which we are going to build but when you want to learn a spring framework there are certain things which you should know and you should have in your machine the first thing you need to know in Spring is Java so the prerequisite is the Java language of course right when you're learning a framework you need to also know the language on which you're going to build so you need to know Java from start to end so when I say end there's actually no end but at least you should know uh till threads and exception handling and collection so basically I'm talking about the syntax of java Loops conditional statements how do you work with array working with exceptions threads and collection maybe threads is not that important but you should know it uh collection is very important and one of the most important thing is the oops concept so oops and interfaces abstraction uh those things are very important next uh when you talk about framework we also need a build tool so when you work on a project there are certain build tools which you have to use in fact any one either it can be mavin or griddle so in this particular series we're going to use mavin but if you know griddle before it will be easier for you to understand mavin uh and if you don't know any of this Maven is preferred for this series next uh you need to also know about database connectivity because anyway we are going to connect our spring application with database so if you know jdbc it will be easier for me to explain you the connectivity there then you have to understand hibernate see we are going to use spring data jpa here and then I'm not going to explain how things are working behind the SC because that's a part of hibernate concept so you need to know hibernate so that you can understand spring data jpa properly and also uh if you can work with XML files that will be better so get used to XML Json because of course this course is not about Json and XML we will be doing some configuration in XML and the passing of data between the client and a server will be done with the help of Json format so you should know what is Json now question arise from where you're going to learn this so don't worry you are basically covered so in the description you will find the videos for this basically you will find the video for a complete Java course where you get Java Maven hibernate in one video it also has spring but it has spring five we are going we are going to learn spring six here so till spring you can learn everything and uh for Json also there will be video XML also there will be video so make sure that you complete those so those are the prerequisites before you start with the course also you need to have certain things in your machine example you need jdk of course right Java development kit because you're working on Java uh then you also need a editor of course you can use notepad here but then maximum time you will spend in debuging and stuff so prefer if you you can use a ID which stands for integrated development environment now there are multiple options here and you can use whatever you want to use example uh we can use vs code we can use Eclipse we can use intellig idea and there are multiple tools but I can work on this three properties I'm naming this uh vs code is good but it's not optimized for Java they are still working on it it's not there yet uh so if you are happy with vscore please continue with it because I see ultimately we are using a mavin for a build tool and for the project structure it doesn't matter which ID you're going to use the project structure will remain same uh you can also use Eclipse which is open source and free uh and it's one of the best tool available for jav development so Eclipse you can use Enterprise version and then comes intelligy idea now the thing is intelligy idea is great but they have two versions one is a community version and second is the ultimate version the community version is free the ultimate version is paid now if you are sure that you're going to work on Java on different projects and if you join a company you're going to use intell idea I would suggest you to explore the ultimate version it's awesome especially for students they give you discount of free you can check it out but it's great Community version is also good and we are going to use Community version here and you'll be saying hey you know in community version we don't have all the features you know what we don't need all the feature when you're learning things and also if you're building a small project and if you have a small team Community version works but of course if you want some Advanced features if you want to boost your productivity explore the ultimate version at least give it a try for trial version so yeah that's about the tools the things which are needed in your machine and you're good to go for jdk uh the version is go for about 17 because if you want to work with spring 5 jdk 8 works for spring 6 minimum you need jdk 17 I have jdk 21 here in this machine so I'm going to use that uh again if you want to use it you can also use 22 version but LTS versions are better so stick to 21 and that perfectly works so yeah that's a prerequisite and the tools required for this particular course now to this point we have talked about what is spring we have talked about the prerequisites and then in when we talk when we talked about spring framework we introduced a term there called dependency injection but that was just a name what exactly this concept means in fact in this video we'll not just talk about dep injection but also ioc because spring started with this concept of course it has lot of features but then this Remains the code of it so what exactly these two terms are so before we talk about it the target for this video is 200 comments okay so let's talk about ioc see when you talk about a typical application it can be a web application or the entf application see ultimately you want to give this application for your clients right so you have a client now this can be a browser this can be your mobile application doesn't matter and then what you want on this screen is is data so most of the application are data driven application you use it for the data right and typically this data is stored in a database now doesn't matter is it a SQL database or no SQL database that's a different topic the point is the data is coming from database or maybe from some external service but then how do you send your data from database to client and that's where you write this server site code right now in Java we can do that with the help of servlet so somewhere in between you need a system which will interact with the database right so when a client sends a request uh you get the response back and then your application interacts with the database right now this can be a standard application this can be a web application as well uh if it is web application you use Ser in between so this box here represents a selet or this is done with the help of svet but now we are talking about spring right and a typical application how it will look like we normally create multiple layers so of course we'll talk about this in detail when we talk about spring web but typically we have multiple layers in between uh just to show you here so we got a layer here uh of course this will be a class in Java and then we have one more layer here of course this will be a class and we have one more layer here so the role of this particular layer here is to uh accept the client request the role of this layer is to do any business logic and the role of this layer is to connect database right so we have different classes for different work here right typically they are called as controllers services and uh repositories right so don't go with the short form doesn't matter we have different layers that's important now uh the dependency becomes here is if you have a controller here who wants to talk to a service class basically in Java everything is object right so basically we have to work with a concept of objectoriented programming and everything in Java is an object that means if you want to work with service in the controller we have to create object of a service inside the controller so what I'm saying is imagine there's a class called controller in which if you want to use service methods basically you need the object of service right so this is a class service and this is the reference so if you want to use the service inside the controller you have to basically create the object of it and Java is object driven same applies to the uh service let's say if service wants to work with the report repository imagine there's a class called service and in this class of course you need to use some features of repository we have to create object of repository here and mind you this is not the object this is just a reference we have to literally create an object by using a new keyword that means in Java this is only three classes right imagine if you have a Java application which has thousands of classes okay now that's exaggerated let's say you have hundreds of classes so you have to create a class you have to create object of one class in another class and not just for one we have multiple classes there so what if let's say we have a philosophy here by saying hey you know let me make the work of Java developers simple what if there's some external power who says Hey jav developer in your application you focus only on the logic let me handle the object Creations that's a philosophy right so let someone else take care of it and and as a dooper now you're happy is because you'll be saying what's difficult in object creation right we can simply use a new keyword and object can be created see it's not that simple when you say you are creating an object you have to manage the entire cycle of it it's not just about creation it's also about managing the object destroying the object right and we don't do that right we simply create the object depending upon how many requests we get for every new request we create new object and sometime we don't need multiple objects and still we do it we create those objects so what if you say hey let me focus on the logic let someone else in the world take care of it that's the concept of ioc which stands for inversion of control so what it means is typically we create the object by ourself that means we have a control on the object creation but what if you give it to someone else that is inversion of control you're giving the control to someone else right that's ioc but then ioc is just a principle it's a philosophy so we need certain technique to do it and that's where the concept of dependency injection comes here so dependency injection is the actual implementation of ioc so how do we Implement ioc in in spring or in Java is with the help of dependency injection it's a it's a Concrete technique so a lot of people get confused between ioc and Di and they get conf they think they are similar yes they do the same thing almost the same thing but ioc is a principle right and design the depense injection is the design pattern it's the actual thing which we do so in Spring to achieve ioc we use di depends the injection so what we are saying is we have some external thing uh here in this case it is spring spring says you don't worry every time you want an object just ask for the object I will give it to you that is injecting the object sounds cool right so that means if you talk about this particular example here where you have a controller and a service so let's say a controller here needs the object of service we don't have to actually say new service that's not our job here you can simply say inject you can ask spring framework to inject the object you just mentioned the reference spring will give you the object that is called the dependency injection so basically there are three techniques to achieve depend injection one is the Constructor injection so what you do in the controller class is you create a Constructor and in that Constructor you pass the reference of service and you say now since I need service here in inject the object that's one way uh the second way is seter so maybe you can create a seter methods for that service reference and uh you can do the set injection the third way of doing is the is the field injection so in Java we have a concept of loose coupling basically what you do is uh you don't have a tight you don't have a concrete implementation of one class in the other uh you code for the interfaces right uh so if you use a field injection somewhere you stopping it you're not able to mock test and all those stuff again it will make much more sense once we go forward uh so field injection is not recommended you can go for Constructor and Setter we are going to use field injection in this course but we'll also focus on Constructor and Setter so that's depending the injection that simply means someone else is injecting the object in your application and that's someone else in our case is a spring framework so I hope it makes some sense and once we start implementing it it will make much more sense so before we talk about spring boot the target for this video is 250 comments now till this point we have talked about springing framework right and we are excited to understand how this framework works and whatever things it promises how it deals with that so yes spring framework is amazing right and whatever application we are going to build now is with the help of spring so let's say you want to build an application this can be anything in the world even it can be a huge application or it can be a application for hello world typically what happens is when you want to to build the application of course you have your Java code with you and then to make it work spring says hey you know doesn't matter how many classes you have I will take care of the object creation and you are quite happy because now you don't have to worry about the objects spring says I will take care of it so let's say in your application you have you have three classes and you say hey Spring create object of these three classes and spring is happy to do that so let's say in your application now you have 100 or thousand classes can spring handle all the objects of course it can but the question is do you want spring to handle all the classes you know most of the time we don't even want the object of few classes let's say you have lot of classes here and out of all these classes you need objects of only few classes how do you talk to your spring framework by saying hey don't create objects for everything I don't want all those things I just want few spring says okay tell me which one you want and that's where when you talk to the framework we have to do that in the configuration file it can be XML file or it can be a property file but you have to talk to the framework that means just because you're using a framework you will not be able to run your code in the first go you have to first do the configuration next part let's say if you are building a web application now if you want to run your web application basically you need a server in terms of java uh we need something called a tomcat server of course we have multiple options let's say if you want if you're building a web application in Spring you need to have a tomcat server that means in your machine even before you run your code you have to make sure that you have installed Tomcat you have configured Tomcat then only you can run your application so you'll be saying hey what's wrong with that I mean of course we can do configuration we can install Tomcat see the problem is when you have a long project it makes sense to spend few hours or days doing the configuration because the project will go for a year but what if you are doing some experiment what if you want to get started in few minutes see most of the languages nowadays they are doing that if you talk about python or JavaScript they have their Frameworks and if you want to at least print hello world you can do that in minutes now this is what spring was lagging way back and Java was lagging way back and that's where we got something called a spring boot now spring boot says all your problems of configuration let me take care of it what if you can get your project running in few minutes and that's what spring boot gives you it's not a new project it's there from a long time but it basically solves your problem of building a project in less time so does it means that spring boot is different from Spring framework not exactly see underlying it's all spring framework right on top of that you have one more layer of spring boot so you can actually build application directly using spring framework or if you want to make it easy you can use spring boot again that's an optional stuff but this optional thing is very very important and uh it will make your work faster so basically we can use a spring boot which is a open unated framework which means it will give you certain things the way it wants and you can simply use it to run your application let me show you how what I'm talking about so let's say if I go to the spring.io the official website and instead of this website if I just say start. spring.io it will take you to a website now see when you talk about building a project in different ID we have an option of directly saying a new project and you can create a spring project in spring project basically you create a MAV project again we'll see that in in sometime or in the upcoming videos but basically you have to create a m project add the spring dependency a lot of different steps right intelly idea ultimate version gives you an option of creating a spring boot project directly but since we are not going to go for the ultimate version we don't want to pay for it we have a good alternative what you can do is you can go to Spring uiser which is this website start. spring.io and you can mention what kind of project you're building so I'm building a MAV project this is a build tool the language I'm going for is Java and then the spring boot version so this is update version so you can see we have 3.2.5 we can even go for the rc1 but I just want to stick to the table one which is uh 3.2.5 and depend upon when you're watching this you can just use that particular version the group I will say com. telescope and the project name is let's say uh demo app and then the packaging is I'm going for Java now this this is where the beauty lies you know if you talk about a spring framework or any web application in Java you can create a project and if you want to deploy it on the cloud basically you create a v file so v v stands forb web archive and then you basically push your W file in the Tomcat to run it multiple steps right spring boot says don't worry you can create a jar file but the problem is jar file we cannot run jar file on the Tomcat then how would work what if you don't need external Tomcat what if the project itself has a tomcat I mean that would be awesome right so basically spring boot says if you want to build a web project you will get a embedded tom cat so I will select ja and I'll show you what I'm I'm talking about and then I can select my Java version so in this machine I got Java 21 so I will go with that it is LTS version so it's safe to use and dependency so what are things we need so of course if you remember when we talked about spring framework I mentioned that there are multiple projects inside spring and we don't need all depending upon your use case you will choose one so I will click on ADD dependencies so you can see there are lot of options here to choose from and you don't have to know everything uh use whatever you need example here I want to build a web application so I will simply say uh spring web if you want to add database connectivity you can also add uh jpa but we don't need that at this point uh if you want to get a longbox support you can choose that if you want to uh use graphql you can use that uh so as I mentioned there are so many options here but I just want to stick to one which is bring web and click on generate now before I click on generate I will show you how this configuration looks like so it will give you the entire configuration we can see we got a dependency which is spring boot starter web here the Java version is 21 this is my project name and this is the spring boot version which we are using and behind this it will use spring framework show you the code so click on download you can even click on the uh generate here so you can see in the downloads I got this project which is demo app what I will do is I will just unzip it so unzipping is done on the other screen but uh I will just open that project now in my ID now you can use any ID here you can use Eclipse you can use vs code you can use intellig IDE Community version or the alate version so I'm using a community version here just to show you the proof I will click on not update I will click on about and you can see this is a community version and I will click on open the project so this is the project which I've downloaded click on open and voila you got your project and the beauty is if I expand the dependencies I think it will take some time to download okay download it too fast and you can see it had added so many dependencies here uh the for the Jackson for Jon conversion uh we got uh micrometer for observing we got Tom Cat you can see we got embeded Tomcat so B basically you don't need to have the external Tomcat here and if you go down uh we got spring boot but also we got spring framework you can see we got spring code so all the things which are spring required for spring code or spring project we got it here okay uh let me just drop it here and now let me create a hello world so what I will do is just to create Hello World now if you have worked with savet or any other language before uh basically we we do multiple steps right but let me show you the code in Spring which will print hello world I'm I'm not going to explain everything in this video how spring web works we have a separate section on that but just to show you how to build application I will get a class and I will name this class as hello I'll just simply say hello and then I will just return I will create a method here and this method is responsible to print hello world on the screen so I will say public string I will simply say greet and this return hello world a very simple method right nothing fancy for Server a simple class is just that if you want this to work you simply say here rest controller and we have to map it with again I'm not explaining it here we have a detailed topic on this what is request mapping what is rest controller we'll talk about it at this point just go for two Antion and say I will say slash so basically whenever you request on the homepage it will return the hello world that's what I want or maybe I can say hello world welcome to telescope okay cool and now let's run this how do we run this so you go back to your application uh the main file and simply say run behind the scene it will of course compile the code it will run this on a tomcat just have a look and when you run for the first time it will print this spring uh pattern here you can change it if you want uh but we are concerned about this or we want to focus on this so Tomcat started on port number 880 nowhere externally I'm using Tomcat okay a very simple stuff and I'm saying run on 880 I got it how do I test it of course you can use a rest client like Postman or I will just open the browser and here I will say Local Host colon 880 enter and if you can see we got the answer we got response which is Hello World welcome to the lco it's so simple right and how much time it took me to run this of course I was talking to the camera and doing this so it will take it took a lot of time but if you want to do this you can do that in minutes so you got your application up and running in minutes that's not the case with spring framework and you will see that once we start with spring later so this is spring boat behind the scene it is still spring framework now the question is is it better than spring of course right it will help you but there are certain issues with spring boot as well of course not a big issue one of the issue is by default it will do a lot of stuff for you so it basically follows convention over configuration because in Spring we do a lot of configuration here it says I will give you stuff you tell me what you want I will give it to you and most of the time it also gives you certain things which you don't want example if you expand this libraries here there are so many things maybe I will not even going to use this but spring boot says take it okay so that's one uh next since it gives you a lot of default stuff if you want to configure then of course you have to do those configuration by yourself that me if you want more control it is better to work with spring framework than spring boot again debatable uh personally I prefer spring boot if I want to work on a project I'm happy to do the configuration in Spring boot but yeah people might prefer spring framework for that reason it's still debatable but spring boot is awesome so yeah that's one of the issue and um yeah that's about it I wanted to show you what is spring Boot and people who cry about Java being slow Java being veros spring boot is here it will make your work easy let's try to implement dependency injection using spring boot now till this point we have talked about what is depen injection we have talked about what is spring Boot and then we have also created a simple project with the help of the spring initializer and we got the web project but we are not going to use this project basically we let's create a new project without the web and let's try to implement depense injection okay so what I'm going to do is first of all let's create the simple project for that I'm going to go to start. spring.io and here let's create a new project so I will click on Maven and I'm going to select Java here the version is 3.2.5 group ID is theis Project name is my app of course this can be anything but that's in my app uh the Java version I have is 21 so and the jar packaging I'm not going to add any dependency because when you say you want to go for web you have to add web dependency but if you don't add any dependency you will get a default springboard options I will click on generate and you can see we got the download and I've also unzipped it now it's time to open that in the IDE so we got this intellig idea Community version I will simply open that project so this is the project which I got just now and I will click on open uh in this window yeah okay so now you can see this is a default project nothing fancy there's no web part here uh if you go to the pom.xml we just got the spring boot stter nothing fancy a simple dependency right now what I want to do is let's experiment with dependency injection and we have talked about depend injection before uh basically when you have dependencies in one class normally what you do is you create the object of it now we want to inject it how we do that uh so let's create a main code so we already have a main file here so you can see we have a class and it says my app application weird name but that's fine for us and then we are saying public void main this is your main code which takes the string arguments and here we are running a spring application so what it does is here so when you say Spring application. Run It basically creates a container okay now you'll be saying what is container here see what happens is when you talk about your uh project so let's say this is your project in this project you have lot of classes uh so let's say you have one class two class three class four class and let's see you have multiple classes Here and Now you are saying hey Spring framework it is your job to create object of this spring will say Okay U that's my job I will do it but question is where exactly spring will do it of course objects are created inside the jvm so you'll be having this big box here of jvm I mean this is your project uh this is jvm and this is where you create all the objects now when you talk about spring spring itself has its own container inside it so spring will have have a container inside the jvm and they call this container as a ioc container so this particular section here this green box here is called the ioc container or you can say spring container that's fine now in this container you create all the objects so let's say from your project you don't want object of all the classes let's say you got object for this you got object for this so these two glasses you want the object and where exactly spring will create the object so spring is is going to create the object inside this so this is where it will create the object so it depends upon how many objects you want and which classes object you want but this will create the object inside this container that means when you want to run this application the first thing you need is not the object but this container and this line here is responsible to uh create that container So when you say spring application. R it will simply run that container for you okay so we have the container already right but then we want the object also and for the objects we want class so what I will do is I will create a very simple class here and normally I call my participants my students as aliens uh is just that I believe that we don't live in this real world we live in virtual world not physically but virtually because we create virtual Solutions right so we we live in virtual world and that's why I called alien and every time you see alien I'm writing imagine that's a developer or maybe I can also say Dev if that is not an issue so let's say Dev so let's say we have a Dev class here so in this world we all are objects right I know that's not a good thing to objectify people but let's say in this example every developer is an object so I'm my object you're the object and we came from the same class let's a Dev class and what is your job your job is to code so public void code and that's our job or maybe instead of saying code I will say build that sounds much better right uh see difference between coding and building is when you code you you write some straight St ments but will it run there's no idea but when you say build you are actually building something and now here I'm going to print working on awesome project again just want to print it some print something and that's why I'm doing this and this is what I want to call so I want to call this method build now from where of course the execution start from the main uh the question is how I'm going to call okay let me just put that in side by side so that you can see it so this is the method I want to call so this build method I want to call from here of course the way you can do that is you can go here and you can call build will this work of course not we know in Java if you want to call a method and if it is a non-static method we have we need object of it so that means to call build we have to create object of Dev so what you will do you will just come back here and you say Dev obj is equal to new Dev right and then using this obj you will be calling build and that's how thing works right so basically you call this and you run this example if I if I click on run it will create the object of Dev and you you can see we got the statement which is working on the awesome project is it a good idea of course you it works and basically this is what we used to do when you don't use spring basically you create the object by yourself but the idea behind spring framework is you don't have to manage the objects spring will do it for you because when you say new Dev here what you're doing is you are manually creating an object inside the jvm but not in the container that means when you create this object it is your responsibility to manage the entire cycle of it we don't want to do that we don't want to create this object by ourselves we want spring to create it and how will you do that it's very simple now since this is a spring project I'm assuming that spring might be creating the object behind the scene I just have to use it right maybe the object is already there maybe this is the object is already there why you have to get a new object for Dev uh so in that case what I will do is I will not be creating this object so I will not say new D because that's what creat the object but if I remove it you know your compiler is giving you some bad words compiler says hey what you doing variable obj might not have been initialized okay so we have to initialize it and one way to do that is just to uh fake your ID your compiler you can send null and your your ID is now very happy at least you have assigned something but of course when you run this you will get one of the most famous era in the world uh and we love it no we just I'm just kidding and the other is the null pointer exception we don't want that right so how do we do this null assigning n is not a good idea so we can get this object from the uh container now question arise how do you talk to The Container because now you are in this main code container is there with the jvm how will you talk to The Container we want to get a hold on it how will you get the hold on it maybe we can get a reference of it right so if you can get a reference of the container you're good to go so basically the type of this container here so let's say the type of this ioc container is of type application context so the type of this particular object of course right this is object even the container inside your jvm which is your ioc container itself is an object right so for for that object there should be some type and the type is application context so what if you can simply get application context and you can see it came from the spring framework application context from Spring framework. context and we can use the reference of it I can say context equal to okay we have to create the object for this now this is weird right because ultimately we are saying we don't want to create the object but now it says we have to create the object see not exactly see application context will work only when you create the object of it right but what if I can get the object from Spring itself remember when we talked about this particular line at the start and I I told you that this particular line creat the container for you it does example if I click on this run here you can see this run method Returns the object so I just went to the source code of it basically I've decompiled the file with the help of IDE and this run method which we are calling of Spring application. Class it basically Returns the object of configurable application context if I go here it Returns the object or sorry it extends the interface called application cont context that means this run is returning you the object of application context that means we we already have the object so what you have to do is you just get this cut put it here and say equal to so what we are doing is we are ass signing this object which returns from run to the context once that is done I can simply use the context and say so context has multiple methods here and you tell me looking at this in the comments before even I go forward I will take a pause which method you are going to use to get that object pause the video let me know in the comments okay so I I hope you have entered the answer so it's actually the get bean so in the get bean you have to mention which class object you want so I want the object of alien class that's it sorry not alien we are not going for alien anymore we are going for Dev okay so you can see we are saying that I want the object of Dev who has created the object it a spring that's what I'm assuming that the object is there the container I just have to use it and I'm doing the coding for that so now this will give me the object which is existing I'm assuming that it is there and I'm fetching it let's see if that works I will run this and if you see we got an error it says no qualifying Bean of type com. telescope. myapp dodev oh okay so we don't have this object in the container I was assuming that there's a there's a object but it's not there container is there there for sure because this is what creates the container but inside the container the object is not there and the entire video got wasted is because spring says I'm not going to create the object why is not creating the object now if you remember in the one of the topic or one of the video we have mentioned that Spring by default will not create object of all the classes and we don't even want it because if spring creates object of all the classes and if you have 100 or thousand classes we don't want the jvm to be burdened with all these objects which we're not going to even use it and that's why says I'm not going to create the object by default you tell me which class objects you want and whatever you say I will create the object and the question is how will you talk to Spring framework maybe you need a config class or in the spring boot or we can use Java based configuration uh we can actually use something called an annotation so on top of your class whichever class object you want just say this class is a component just by mentioning this annotation here your spring understands that this is the class which I have to manage so this is a managed Bean what it means is spring will create the object for you in the container so the moment you say component spring says now I know what's my job my job is to create the object I will do it for you and now let's see just by adding that component annotation is it working let's relaunch the code relaunch this application and it worked can you see that it says working on this awesome project so this is working right and that's how we get DEF injection so what we are doing is in this code we wanted the object of developer or Dev and spring is injecting that dependency okay now we can go bit more layers example let's say uh we got Dev and then Dev needs an object of a laptop of course as a programmer or as a developer you want to work on a laptop and you don't have a laptop here you're just saying working on a project but how in the air or maybe Oculus device or maybe Apple Vision Pro doesn't matter you need something to work on right and we don't have it so let's say in the next video let's try to create one more layer uh because in this we we got two layers right we got two classes so main needs object of Dev it is working but what if Dev needs object of laptop how that will work let's try to understand that in the next video but yeah I hope you got something from this video uh something about the div injection using spring boot where you are injecting a dependency in this particular section so now we know how exactly your spring boot helps you with the dependency injection but now let's add one more layer so there's a concept called Auto wiring and in this video let's try to focus on auto wiring with the help of one more layer Target for this video is 250 comments now which layer I'm talking about now just for example let's say when we talk about developers of course as a developer I need a machine to work with it can be a laptop desktop doesn't matter I just want a machine to work with so what I can do is I can work on the awesome project but while doing that maybe I also want to to uh compile the code so maybe I want to call a method called compile uh maybe I want to call the method uh debug so we got all these methods to work with and of course I can't do this in my mind we do that when you want to do a dry compilation but technically if you want to really build a project you need to have a machine where you can compile and debug so in order to call this methods I need a class where I can Define this and that class is your laptop class so let me create create a class here and time in I will just close this and let's create a new class so here I will create a class called a laptop okay and then in this laptop class basically I'll be having method like public void uh compile of course you need a compiler in your machine to compile compile something but let's say if I have a method called compile and it will say compiling with 44 bucks but now uh okay this is this can be a simp simple joke right where I say 44 bugs where you it's not found bugs not found maybe I tried maybe I failed anyway the point is I'm trying to print compiling with 44 bugs and this is what I want to print and now since I have a laptop class since I have a method now I can okay it still shows you a error okay it's the is here so basically if I want to call this compile of course I need the object of laptop here so I can simply say laptop laptop I got the reference not the object and using that reference I can call this I can say laptop. compile let's say we don't have debug just to keep it simple let's go with single method of course you can have multiple methods so I got the laptop reference and I can call it now there is no compile time issue okay you can see there's no problem here but by default when you talk about the instance variables so laptop is a variable which is an instance variable by default it will get a value which is null and we don't want it right we'll get the same problem which we got earlier I want to show you that I want to show you the error which we got earlier so I will just run this and you will get waiting for the error okay so you can see we got null Point exception because is because the laptop is by default null uh let me just put this side by side so how do we connect this so one way is you can create the object here so you can say new uh laptop right and then this will solve the problem is because now you don't have a n there you got the real object so if I run this it it says working on this awesome project also it says comparing with 44 bucks so our problem is solved basically it's able to call the laptop but then this is not what I want I don't want to say new laptop and even if you want to say new laptop there are multiple places you can do that we don't actually create the object when you define the instance you basically do that in the Constructor of this class of developer or in the setter of course this is a variable right so this should be private where you can create a seter method for this and you can do that in the seter if you want to use the new laptop but anyway I don't want to use new laptop anywhere what I want is I want spring to create this object and then automatically connect it here so the first thing is what I will do is so I will talk to Spring framework by saying hey Spring framework I know you are creating the object for Dev I want you to create object for the laptop as well but it's not doing that you know why because we forgot to do one thing which is ADD component now when you add add component here now your spring framework knows or your spring boot knows hey Dev is not the only class where have to create the object it's also the laptop so now spring boot will create both the objects and that's not a big deal the big deal is how will you connect this to now the developer says I want laptop how will you connect this see one way you can do is you can use the application context remember in the main basically we had this application context and if you can get this in the developer or Dev class you can do that but I don't want to invoke application context there I don't want to use that there so what you can do is you can use certain annotations one of The annotation we can use here or you can use one annotation which is called autowired now what this does is it says see as a developer or as a programmer here in this case not the programmer or in the example but someone like me I'm doing a code now I want to connect this to right so basically when you say connect that basically means wiring now since I want this wiring to to be done automatically I can call this as Auto wiring and that's what the the anation is when you say autowired now your spring boot says Okay I want to get object of Dev but Dev is dependent on the laptop let me connect this to so behind the scene it will connect it and now you actually got the instance for the laptop so using this it will create the object but using Auto wire it will connect it and how do we check it of course to check it we have to run it so we'll just relaunch this applic ation and you can see we got the same output without the new keyword so that's the beauty of Auto wiring okay and you can do that in multiple places not just here and basically when you do it here it is called a field injection so when you do that on the top it is field injection remember when we talked about depend injection we talked about three different types field injection Constructor injection set injection so when you do it here that's your field injection but let's say if you don't want to do that here what's the other option you can use a Constructor here so I can say Dev and in the Constructor you can pass the object of laptop and you can say laptop is equal to I mean this. laptop is equal to laptop if you're not sure what this line is U go through the video of this keyword on ja on YouTube so search for this keyword in Java and you will know what I did here okay so now this is how it does the Constructor injection you don't have to use Auto that which is optional so when you run this uh this also works so this is Constructor injection otherwise if you don't want to use Constructor injection see if you if you just uncheck if you just comment the auto and construct injection if you try this you will get the same eror which we were getting before so those things are working okay uh let me try this set injection so what I will do is I will just create a setup method which is public void set laptop and I will say laptop laptop this do laptop is equal to laptop okay so now I got the seter injection or seter method let's drive this works now when you do that it is still giving you an error so by default setup will not be going for auto wiring you have to say autowired here now when you mention autowire then it will behave like the way we want it will do the set injection we have not done for the Constructor you can do it it will not give you error but even if you skip that it is by default picking it so by for Constructor it is default it is optional but for field injection and for seta injection you have to use autowire which is better uh Constructor and set is better field is not but you you will see me using that in the upcoming uh videos is because I want to keep this simple so that you will understand the project or you will understand how spring framework Works behind the scene I want to do one more thing here so what I will do is I will just go with the auto wiring fuel injection and let me remove or let me just commend this Constructor and set up let's only focus on the auto wiring here now question arise how exactly is connecting how do it how your spring framework knows that when you say Auto it will connect with this class object not some other classes because in your project you might be having multiple classes right so how it knows that we have to connect with laptop only so what happens is uh when you say Auto wiring it goes for by type it is not searching for name if you're thinking it's because of this name no it's not because of the name it is because of the type of the class so since we are saying laptop it says laptop connected but the question is what if you try to apply loose coupling here what I'm trying to say is what if you create a interface out of this so I will say refactor extract the interface and let's code for the interface because see in this world there's nothing called computer it is either a laptop or desktop but both are called as computer okay do we got computer here or I don't know we just clicked on okay so we got computer here and you can see computer is not having any method so I will say wide compile and this will be declared let me just put that down here I know there are multiple things open here but yeah we don't have a choice you can see on the left hand side we got Dev here we got computer and here we got laptop and laptop implements computer so basically what we are trying to do is we are accoding for the interface okay so here we got the interface which has method called compile and laptop implements computer now what's the advantage of this see one of the advantages when you talk about develop Vel opers when you say you want to work on a project when you join a company your demand for MacBook right and that's the industry standards now everyone wants MacBook but you don't get it uh I'm still waiting for my MacBook but anyway the point is when you join a company they don't promise you that they will give you a laptop they promise you that they will give you a computer this can be a laptop or desktop and that's why a developer should not be dependent on the laptop this is hardcoring a developer should be dependent on the not on computer not on comp component on the computer and I will say comp here and let me replace this with comp so basically what I'm saying is as a developer you should focus on dependent on the computer not on the laptop and it depends upon the company what they want to provide you they want to provide you laptop or desktop that's their choice right now when you say computer will this work because see at component we are writing on top of laptop right but here we are looking for computer and as I mentioned before it goes by type so by type means it will search for the type of computer and laptop is a type of computer so if I relaunch it and you will see it it works so basically it is not compos that you should use only laptop you can also use computer and that's the good practice to use so we got computer here but with this I got one more thought what if now since it is searching for computer and you got laptop that's a good thing right but what if there's a confusion okay what confusion talking about let me copy this laptop class and paste it here of course it will we have to change the name of it so I will say desktop and now you can see I got a desktop class as well and so many windows open let me close computer now we know that computer is a interface which has only one method let me close it we got two classes here one is a desktop and one is a laptop and both are implementing computer and the the method also same compile and compile is because they're implementing computer here I will just write a different thing compiling with 400 44 bugs but faster and now uh you can see we got two different implementation right now my question to you we got two classes both are implementing computer so both have type of computer both have component on top of it so basically in your container you will have now three objects right so this is for the dev this is for the laptop and this is for the desktop say desktop here so basically I got three objects and now in the developer when you say autowire with computer Which object it will connect with laptop or desktop confusion right it's like you're joining a company and then they are giving you two options and you are confused because on the on the desktop you will get with the same amount of price desktop works faster plus it is connected to the high power source laptop is portable so you're confused there and that's what is happening with spring now so let me re rerun this and see what spring says okay so we have not got the error but we got I mean we have we got the error not the red one but we got the error the error says application fail to start reason field comp this one is uh in this particular Dev class require a single bean I mean of course right we want only one object but two are found and this is what happens when you are confused between two things uh one is desktop and one is laptop which one should I pick for and it's basically confused how do we solve this one of the ways you can delete the at desktop class it will solve the problem or you can basically remove the add component on top of desktop so now what you're doing is you're saying in the container you only got one object of laptop we don't have the object for desktop so there will not be any confusion there it will become up the laptop part but what if you have a component here and you don't have a component on top of laptop in this case it will pick up the desktop and you can see it says faster so this desktop but what if both has it now that's a problem right now in this case in case of confusion you can use certain annotation or one annotation so you know when you have two options sometime one thing becomes your preference let's say for me I prefer text TOS but maybe you prefer laptop so in this case on top of your laptop you can use one annotation called primary so what's the use of primary in case of confusion this class will be preferred and now when you say component on both but when you run it it will prefer the laptop you can see it doesn't say it's faster so that's laptop but what if you write primary on both not a good idea but let's say what let's see what happens when you say primary on both of both of them again there will be confusion but this time it is giving you different error but yeah it says the same thing it says more than one primary being found among the candidates so not a good idea but uh yeah this one one way you can put out primary on one class what if you don't want to mention primary what if you want to say component then you can decide in this level so you can also use something called a qualifier now in the qualifier you can mention the name of your class so you can mention laptop here okay not the name of the class but the name of the instance see by default the this instance will have some name or we say be name so for the laptop it will be your class name but uh without the capital letter the first capulator so if it is laptop it will be laptop but with small l so that's the name you have to pick up here okay so when you set qualify in case of confusion it will pick up the laptop so now if you done this we got laptop you can also do do it for the desktop and it works so basically this is how you do auto wiring so in this video we have not just talked about the auto wiring we have talked about primary and qualifier and we have seen how do we code for the interfaces yeah lengthy video but I think worth it so I hope you got some idea behind the scene how Auto wiring is working we have talked about spring framework but then when we started coding uh in between we talked about spring Boot and the project which we have created is done with the help of spring Boot and things are bit easy right but what if you don't want to use spring boot what if you want to work on a spring framework so there are two reasons why this video is important first what if the project you are working on in your company or maybe by yourself you don't want to use spring boot or you don't have a choice of using a spring boat so this video becomes important to understand how do you work with spring framework second reason for this video is in this video you will understand what is happening behind the scene right how spring analyze the configuration and what are different aspect of this uh configuration so we'll talk about it of course we have discussed those things in the spring boot as well but when you do it here it will make much more sense so the project which we have here again a simple project what we have done till now is we got uh three different classes we got developer we got desktop and laptop and then a developer needs a computer it can be a desktop or laptop and that's what we are doing here but if you see nowhere we have done any configuration basically this the the spring board Analyze This annotations and then based on that it will create a project for you and it will make it work for you but then as I mentioned there's no XML file there's no separate Java class where you're doing all this configuration so what if you don't want to use springboard I want to use a normal spring framework now so what I will do is I will just go back here and create a new project so let's go to here the intellig ID of course any ID will work you will click on new click on project and I don't want to create a spring project now because when you create a spring project it will be spring boot project so what I will do is I will click on mavin AR type so when you talk about a project like spring when you talk about different Frameworks you have to use certain build tool to create those project of course in Spring boot also we have used mavin here also we are going to use mavin of course we can also use griddle there uh but let's stick to mavin so here I'm going to create a project I will say demo spring that's my project name and I will keep that in download doesn't matter where it is next the jdk version so make sure that you first of all click on on this mavin Arch type or if you're using Eclipse it will give you option of creting a mavin project so select that the jdk version in this machine I have uh 21 I will just go with that and then it is asking you for the catalog see MAV as a build tool also has its way of creating a project it will give you a basic structure of a project now based on different framework based on different Services which you want to use they provide you certain Arc types Arc types is basically your project structure so they provide you different Arc types which you can use U of course you can build this Arc Type or you can build this project from scratch without using their particular uh template but they give you certain templates to use and there are certain templates which they have inbuilt and there are some templates which have been uh sourced so example if I click on catalog here you can see there's option of internal then also MAV Central so there are certain templates or Arch types you can use it from M Central so if I choose internal here and if I expand this Arch type there are limited options options you can see uh you can actually at least count it uh there is there's a template for a j J2 application for portlets for quick start we are going to use Quick Start and we have web app as well but if you click on M Central you will see lot of options you can see it is loading here loading done and if I expand now there are so many options here you can just sometime it is confusing which one to choose and that's why we are not going to use this maybe there is already application available for spring boot as you can see it is here spring board 3 rest API Arc Type so you can use this Arc Type it will give you a template to work with I will stick to internal and let's create a quick start project so you can just click on quick start here and the version just stick to it there are certain additional properties we can skip that advanced settings this is where you can mention your group ID which is already com. Tesco the artifact name is demo spring and I can click on create here and I will go I'm going to create a new window because maybe I want to use the old project code so this is my project this is my spring project now you know what basically this is a spring project but only with a name nowhere in the project we are using spring feature see when you say spring feature basically we have to add a dependency for spring and we don't have it here if you can see we don't have a spring dependency we have to add that so that's one next if you see the external dependency of course we don't have anything here we don't even have the configuration for spring we have not done for spring boot as well but here we have to create a configuration file to make it work so now what you what we will do is let's create a simple code in fact we can actually use those codes which we already have so maybe I will just try to reuse so let's go to the spring boot app and I want to use these two classes Dev and uh okay let's only use Dev I'll just copy this and paste it here so we got Dev class and we don't need all the annotations now we don't need uh Auto wire other stuff or maybe we don't even need a computer maybe I could have just typed it by myself sometime being lazy is a additional work you get okay so I just wanted these two things and I don't even want all these packages a simple class nothing fancy you can see we got a Dev class which has a build method and it says working on the awesome project maybe you can just type it instead of copying it from the older project and now I want to create object of Dev inside this app so this is your main code the main Java code first I want to check if this project is running I mean this is the step important step you should do whenever you create a new project without typing any code just run it to check if it is working that's important so here I don't want to print hello world uh we just want to okay I just want I don't even want the comments here so what I want here is to create object for Dev so that I can call build so the idea is to call build but for that I have to create object of De Dev so I will say Dev obj equal to new Dev and then I'm going to say obj do build so this should work because we are creating the object by ourself nowhere we are using spring yet so it says working on the awesome project so we are happy but then we don't want to say new jav new Dev here right this is what I want to get from Spring I want spring to create this object now can I use uh we have seen in the spring boot we can can you we use spring I mean can we use component here first of all we don't have this component option is because we don't have spring boot or spring framework here and even if you use spring framework component it will not able to configure that so this will not work so how do we make it work the first thing is if you want the dev here we have to work with the container remember we have talked about a project where you can have multiple uh classes and then you also have something called a jvm here in which you'll be having a section where you'll be having Heap memory and stuff and then inside that you will be having your ioc container and then uh inside this you'll be having objects but unfortunately we don't have the object yet so how do we got get this object so even before you get the object the first thing you need to get is the container how will you get the container and we have seen that right before if you want to work with the container we have to create object of application context you need object of application context and we don't have the object first of all we don't even have this interface in the project if when I say control space you're not getting the package of it reason this is not part of a Java this is a part of spring framework so if you want this to work we need to get the external libraries for spring and how do we do that so we have to add this spring dependency so we already have a Jun dependency apart from this we need to get the dependency for spring but not everyone remembers the dependency name and the artifact ID and the version number we don't know that right or maybe some super humans knows uh this by heart I don't know so how do we get it so it's very simple what you can do is you can go to one of the most famous plays for the dependency which is called a maven repository you can just go here and search for in fact it already had a spring context is it so famous or maybe they are tracking me what I'm doing they got so many uh libraries you know why is only promoting spring maybe even they're tracking me so here basically you have to search for spring context and you can see this is what I want so when I click on this I can select any version so normally I prefer the version which doesn't have any vulnerabilities and you can see they give you Waring as well 5.3 has theability we can skip that six is a version V which we are going for uh which one to choose first of all make sure that you don't have any uh security issue here second go with the version which is not latest but if it is latest and it is used by so many projects that's fine you can go with this 6.1.6 so you can see this version basically you can just copy this or if you're not using Maven you can even get it from The Griddle so you can just use that so I'm using Maven I will just copy this and replace this dependency which we have written with the new thing I don't want to Market this M repository so yeah so this is how basically you create uh you get the dependency oh but it's not coming here why it is not coming here it's because we have to reload the changes so in intj you have to load this mavin changes but in Eclipse it will happen most of the time it happens by default so I will click on load MAV changes and now you can see we got the dependencies for spring context and once you have that let's go to our app and let's say control SP SP okay there was some some issue with the importing it should normally work but I don't know what's wrong maybe I'm writing a wrong spelling or maybe intell has some issues anyway so I I got the context I will say this is a this is uh context equal to now how do we create this object so let me just comment this to for time being so how do we create this object so of course you can say new application context but application context itself is a interface so you can't get object of application context in that case we can go for the classes which implements application context and one of the class is class path XML application context we're going to use this because initially we start with the XML configuration we can do it with annotations as well or Java configuration let's go with uh XML and that's it you got this object and now once you do this this line basically creates the container so our job is done the container is ready but with that container how do I get the object it's very simple you can simply say context. get be and here you can mention what Bean you want maybe I want the bean of Dev docl class our job is done and it should work let's see if that works let's run this because we got the container I'm expecting the object is there I just have to use that let's run this and no it's not working so it says bin Factory not uiz or already closed so it says before accessing ban why application context first of all what is Ban Factory so the way you have application cont text the container is created with the help of bean factory it manages the bean the container so in the earlier versions of uh spring we used to use bean factory but now we use application context so it says something is wrong I mean it's closed I'm not able to create the container I mean you're just creating a container but it is closed now so how do you configure your container because in the container we don't have this object of Dev and that's where you have to create a XML configuration but how do we do that so it's simple actually in this bracket you can mention the XML configuration name so I will say spring.xml but we have to create this XML file okay and if you done this just by just by that statement of course this name can be anything it says the XML is not found and where it is searching for it it is searching for that file in the class path resource okay uh since it is a class path we have to go with class path so what I will do is in the main folder I will create a directory we call resources normally it is there but maybe it is not showing that in the intellig idea just check if you have resources folder in this resource folder you will create a file called spring.xml and this is where you have to do the configuration and if you just create a file and if you do nothing here and relaunch it this time it should not say file not found it should give you some different error it says line number one column number one premature end of file because we have not done anything but what is this configuration now we are basically trying to create a spring project without spring Boot and of course for that we have to do lot of configuration and to achieve that we have basically created the project and in that we do have a Dev class here in which you have a method called build and what we are trying to do is we are trying to call the build but for that we we need object of Dev and then we want spring framework to create the object which is actually not happening because if I run this this is what you are getting we were getting the error which says uh premature end of file uh which file I'm talking about is the spring.xml because if you want spring to create the object we have to do the configuration and this configuration will be done in the XML file and this is empty at this point I don't want this to be empty we have to write some configuration using which you can create the object and if you have this question in your mind why we are using XML when we are happy with spring boot uh there are two reasons one is what if your project which you are maintaining is a legacy project in that case of they will be having or they might be having XML configuration so you should know second is if you want to understand what is happening behind this INF for spring boat this configuration will help you there okay so let's get started now if you want to do this configuration even before that when you talk about this line number uh 10 here and when you say you are creating a context behind the scene we got the container so we do have a container already but in this container we don't have the object yet so if you want to create the object or if you want spring to create the object you have to mention that here and every class which spring manages they're called Spring beans right or managed beans so what I will do is since we want to create beans I will use a beans tag now this is how you work with XML if you're new to XML basically you can compare XML with HTML uh HTML stands for hypertext markup language XML stands for extensible markup language so basically in the HTML you have some tags which you can use and who understands those tags is your browser so in the browser it is it has a definition of all those tags which you're using maybe you're using HTML tag body tag or a P tag or a div tag so basically your browser knows those tags because they have the definition of it in XML you can use any tag which you want there's no compulsion that you should use this tag or those tags even if I want I can use the navine tag here I mean that's my choice I want to use navine Tag I can use that but when it comes to Spring frame workk we have to use certain tags which are which has their own definitions and we do that with the help of DTD files which is document type defin definitions you don't have to get into that much just imagine XML you can use your own tags provided you have a definitions for it now we are using some tags here and we do have a definition of definitions for it and I will show you that in some time uh but we have to use a bean tag because you'll be having multiple classes and multiple objects and inside this Bean tag you can create multiple Bean tag right so you can have one depend upon your requirement you can have multiple Bean now in this Bean you can mention which class Bean you want which class object or which class you want spring to manage so I can mention the class name here with help of class attribute and you can mention the class name which is Dev in this case now simply class name will not work you have to basically provide the qualified name for it so in this case it is com do theis do Dev which is the qualified name so you can see Dev belongs to com. as a package name and then optionally you can also provide the ID for it so let's say ID is Dev you can use any name doesn't matter I'm using Dev in this case now where this will be useful example if you go back to your app in this line number 11 what we doing is we were using a dev. class in the earlier example you can also use Dev here so earlier basically we were using dev. class here we can use in codes we can use Dev but you just have to make sure that since we are using Dev this get bin returns you the object type just typ cast it with Dev and that should work going back here so now we do have a definition right or we do have the bean created so the conf is done let's try so what we will do is I will just relaunch this and see what happens okay still it is giving you error it still says uh it is invalid but why this time it is giving you some different error it says line number four which is this beans it says cannot find Declaration of element beans oh this is weird so basically uh we don't have a definition for it as I mentioned before you can use any tag provided you have a definition for it for this be we don't have a definition so in this case you don't have to buy her definition what you can just do is you can just go to Google and search for spring 6 Bean configuration XML and look at this file or this link click here and it talks about the configuration just go down and search for this one and you can simply copy this and replace the beans because in this if you can see there's also Bean tag here or beans tag here just replace it and we are good okay beans not two times only once and I will just remove this so what we are doing is we are providing the definition for beans here okay and with this if I relaunch it hopefully this will work okay hopefully I said but it's not working there's some problem it says line number four there's some issue with the passing some problem with the first line is it the issue okay so maybe there was some issue with this line here okay I'll just simply get rid of this maybe there's some extra character which I can't see anyway so if I relaunch this we have provided a bin definition and this is working it says working on the awesome project so by doing this what we are doing is we are asking our spring framework that you are responsible to create the object for Dev and which we have done and that's why you are getting this object here so that's our first one how about one more class let's say in this particular project let me create one more class and let me name this class as laptop okay a simple class nothing fancy and maybe in this in this class I want to have a method called compile so I will say public void compile and in this I'm going to print compiling and now if I go back to Spring you can see we are just mentioning Dev here right and one more thing what I will do is I will just comment this section now how many classes we have in our project of course we we have three classes but then we are concerned about the two classes because app only has a main method so we have two classes Dev and laptop which we created a normal class like Dev a normal class like laptop and now if I run this tell me what just think about it if I just run this code how many objects will be created inside the container or how many classes object will be created inside the container is it zero is it one is it two so let me know in the comments okay so if I run this let's see okay first of all how we will see we we don't even have idea right we are not printing anything see the thing is if you look at the XML file when you execute this line it goes to the XML file in the XML we are creating the object only of one class or we are saying only one bean tag so it will create the object only of that class which you have mentioned here which is Dev okay and to prove that what I will do is I will just have a Constructor here I'll say public Dev which is a Constructor and in this I'm going to print Dev Constructor and I'm going to do the same thing in the laptop so I will have a Constructor here of course this should not be Dev it should be laptop and here also I'm going to replace this with laptop okay a simple class simple Constructor nothing fancy okay and now if I try to run this so if we don't get any output that means none of the object got created but let's see what happens so we only got Dev Constructor that means we got one object and that two of the developer reason in the SPL XML we have mentioned only that what if I create for the laptop as well so I can just uh reuse my code instead of saying copy paste reuse sound skirt and we don't need that much of space between being closing and here I will say laptop and here the class name is laptop and the moment I do that and if I run this let's relaunch it and you can see now it says Dev and the laptop as well both got created but we are not using them in the code right we are just saying execute this line create the container and as soon as the contenter is created it will simply look at the XML file and say okay we do got we do have two beans here mentioned let me create the object for them okay so and then in your container you basically have two objects one for your Dev and one for your laptop but the question is what if you mention the dev one more time so I will just copy this and paste it here and let's say d one we got different ID so now let me know in the comments how many objects it will create 1 2 3 of course it should be more than two or two or more than two so let's try and you can see it is calling the dev Constructor two times that means it it it depends upon how many times you mention be if you say once it will create one object it will if you say two if you say one more time it will create another object for Dev so we got two objects for Dev right on one for your laptop so that's how basically you can can configure your spring framework to create the object now uh we can go for the next step which is what if in the dev you need object of laptop now that will be tricky right because now you're Deva initially we were dependent on the spring container to give you this Dev right Dev object and now inside Dev if you want a laptop object now that's where the dependency will come so till this point we were able to work with spring boot but then we want to also explore how do you work with spring framework with without springboard and then uh to achieve that what we have done is we got this Dev we got this app uh basically we were trying to create the object for Dev so when I say we are trying to create the object basically I'm asking spring framework to give me the object as you can see uh we have done that uh in the configuration so if I show you the configuration which is spring.xml we got three objects here so every time you want the object from Spring framework if you are not using springboard we have to use some configuration we are using XML here you can also do that in different ways but in XML if you're doing that we can use Bean tags uh you can see we got a bean tag with the ID Dev and the class name is Dev uh we got another Bean tag which is Dev one and the class is dev then we got laptop of class laptop so in total when you run this application your spring framework will see this and say okay it's my job to create these three objects and it will do that for you and in the container basically in the ioc container or you can call it a spring container you will find this objects you can see we got uh three objects here two for Dev and one for laptop why this happening is because we have done that in the bean configuration you can see here three different beans and the IDS for the particular first object the ID is Dev for the second object is Dev one and for the third object laptop it is your laptop right so these are the ideas for it now what we want to do is let's say in the dev class I have some properties so in this particular video Let's discuss how do you work with the injections the seter injection and the Constructor injection to do that uh of course what I want ultimately is to have the laptop object here okay maybe I I mean I just want to introduce a reference I want spring to give this object because at this point is just a reference it's not the object so let's say in the build if you try to say laptop. comile which is a method of laptop class it will not work reason is laptop this one is just a reference we have not assigned an object here or we have not instantiated here or we have not specified the memory here so by default it will null and when you run this it will give you our favorite error which is null Point exception and we don't want it right so ultimately we want this object to be passed but at this point I will just comment it we'll use it later because this will this will not work and to understand how do you inject let's start simple what I will do is uh let me create a simple variable here and maybe the the variable name or variable uh name is age of course you can go with any name doesn't matter but let's say I'm saying age here and in this build I want to print the age so whenever someone calls the build I want to print the age or if you don't want to do that from here we can do one more thing see uh app is a main class right this is where you have your main method and if you say you got this object here let me just uncomment this you got this object here of Dev class and you're calling build let me say I don't want to call bill now what I just want to do is I want to print the value for age so what I will do is I will simply say it's out and obj do AG now since it's a int variable let me show you that let me just minimize this thing we don't need that uh let me just put that here on the side yeah okay so if you see age is a int variable and if you when you try to access this it will try to pick up the default value so what do you think the default value for age is of course it's a in value so default value will be zero and when you run this you will get zero let me just try this so I've run this app and now you can see it prints zero yeah we got this output as well is because we were working with the Constructor ignore that we got this zero here right now I want to assign some values to it and we can do that right so we can simply say int age is equal to some value 8 or 10 because nowadays children at the age of 78 they are doing programming uh it's fun and weird at the same point but anyway the point is we have assigned a value to to it and if you have noticed I'm not using private modifier here that's on purpose is because I want to use this variable directly here not a good practice but since we are into learning stage we want to see what are things happening behind the scene so I'm doing that here okay and so we can pass it now what I want to do is I don't want to I mean of course we should run this first just to see if this is working and you can see we got eight so you can assign the value from here or you can assign the value from here as well you can say obj do AG and you can set whatever value you want to want here let's say 18 and if I run this uh it should be 18 is because initially you have assigned eight when the object was creating uh but then later on when you are running this after the object creation you are replacing the value with 18 and that's why this 18 here now let me just make this variable as a private variable because that's the ideal way and I don't want to assign the value from here not a good idea to assign hard hardcoded values now since this is a variable which is private uh if you want to access this from outside example if here if you try to see that it says uh age has a private access so we can't directly access it we need a geta and Setter for that so what I will do is uh I will just create a geta Setter after this Constructor here so right click and say Hey I want a geta setus for this for this particular variable and I got it so you can see we got the geta we got the seta okay and if you want to assign the value this is not a good way you can simply say set age you can set the the age as 18 and whatever value you want to assign and here you can say obj do get age right and now if you done this what you're expecting is of course we want 18 and that's what we got here but what if you don't assign the value let's say we are not assigning a value here and if I run this you can see we got zero cool but now the point is I want to assign this value using spring framework so I don't want to do that here or here I want to assign some value with the help of spring so I want to inject the value how do I do that now since we are saying we want to we want spring to do it we have to do the configuration in this spring.xml file and how do we do that see when you are creating this object here what you can do is in this particular Bean tag there are multiple options you can assign properties because ultimately these are called properties right for for the object so you can define a property now this is a inbuilt tag inside the spring so you can say uh property and then in this property you can assign a name of our property so which property you want to change I want to change the value for the age property with what so I have to assign the value as well so let's say the value is maybe different value 12 I think this is the ideal age uh to start programming not eight so anyway so uh we got age as 12 and now let's run this and let's see what happens if run this you can see we got 12 that means we are assigning okay when I say V uh spring is assigning the value of uh 12 to H variable right so that's what that's what is happening here so when I say when I'm I'm creating I'm getting this object the object which got created inside your container now it has a age variable and the value is 12 so the moment you create this instance it will have the value 12 because of this setting here but what about the dev one is it applied there as well let's try so what I will do is I will just go back here and say de one and if you run this uh you can see is not for Dev 1 so when I say Dev one which is uh this particular thing here the value for age is still zero because we have not assigned the value for that and how do we know that so if you see the B we not assigning a property here okay and since we are not using it we can also remove it right so we can just go back here and say go back to Dev because we don't have Dev one at this point and run this it will go back to 12 so this is how basically you use set a injection but we have one more which is construct a injection how do we do that so what I will do is I will create one more Constructor here uh which is a parameterized Constructor so I'll just right click here and say hey generate a Constructor for me which will take int age as a parameter and you got it here and I want to inject the value of age not with the setter but with the Constructor so that means I will just go back here in this spring.xml file and here instead of using a property because property will use set up I can just come in that section and we can use something called call a Constructor argument and in this you can specify the value as whatever value you want let's say 14 in this case and inste of I will just say self closing tags yeah so instead of using a property I'm going to use a Constructor this time it will use this Constructor and you can see when you say Dev Constructor it was getting printed here right because initially we were using a default Constructor now since we are using a parameterized Constructor I will just say Dev one Constructor just to differentiate between the parameterize and the default one okay and now let me run this now what will happen is with the with the help of this Constructor argument tag we can assign the value with the help of Constructor and you can see it says d one right which is the parameterized Constructor output and the value is 14 cool so that's how basically you can use Constructor and seter okay but there are certain things which have to remember in the property we have used name in Constructor we are not doing that that it's because we have a Constructor which takes only one parameter but if you want you can also specify name here there's a name property here if you can use that you can say age but since we only have one parameter you can skip that if you have two parameters and if you want to specify the sequence you can also use index uh you can specify for index zero index one so at this point we just have one but let's say in case if you have two two parameter here let's say age and salary in that case you can specify the index zero for age and index one for salary so you just have to say Constructor arguments two times cool so that's how basically you use a seta injection and the Constructor injection but then that was for the simple variable right what about a variable like laptop now this is not a simple variable this is a reference variable right which is you have a class reference there and how do we achieve that so what I will do is I will just come in this Constructor so we don't want to work with ag anymore so I can just come in that section let's only focus on the laptop now when I say Focus what I want to do is in the dep when you say build right example in the app when you are calling this build build will call the method called compile and with those settings okay let let me also comment this age I don't want to print age uh let me run this there's no age now it's just a Constructor and you can see we got an eror it says the laptop they cannot invoke is because we got nullo exception it says this laptop is null okay null is because we have not created the object we just got the reference of it which is here so what I want to do is we know that in the container we do have the object of laptop we have it here we just have to inject this to the developer object right the instance and how do we do that of course we can use a spring here so don't you think laptop is also a property what I can do is uh for this particular Dev I can simply specify a property but this time not for the age but for the laptop right and then we can assign a value to it okay hold on now that's a problem for the age we can assign the value because that's a int variable that's a primitive variable laptop is not a primitive variable it's a reference variable you can't simply assign a value to it so what to do so instead of assigning a value we can use something so if I say control space you can see we get two options not one we get a ref and we get value so we can use ref for the reference but the question is what should we specify in the reference now that's a question if you go down we do have this laptop object here right and the ID for that is laptop so the same ID so instead of using laptop let me just try one more thing I was say lap one so this is the ID we need to use here I mean we can also use laptop I just don't want you to get confused between laptop laptop is it the class laptop or the instance laptop or the name laptop so I just change the name of a bean which is uh initially it was laptop I said lap lap one and that lap one I'm using here so what we are trying to do is we are saying hey I'm creating this object okay so when I say I spring when I say hey Spring create the object spring says okay let me try to create the object for Dev but the it's is trying to create the object it will create the object for sure but it will see at the property and say Hey you know we need to get the laptop uh reference here and where do we find it so the name of the reference is lap one so it will search the container by saying hey do we have any object of the laptop or any object with the name lap one and it will find that it was it is there right and it was try to connect it so basically what we are doing is we are doing a wiring here so we are wiring the laptop object into the dev object so that that wiring is what is helping to do that but will it really work let's try so what I will do is I will just run this and we got the error I was not expecting the error but we got the error let's try to solve it now so the error if I go up and know it's good to get errors you get something new every time uh it says being property laptop is not writable or invalid oh okay okay see when we specify the property for the age we have used data sets for it for the laptop we have not specified the get sets yet my bad so what I will do is uh we need to get the gets and Setters in fact we need sets not geta but we'll have both they come in the parcel uh so I say generate and get set for laptop and now we have it okay I don't think it should complain now uh let's relaunch it oh it's working you can see it says compiling so that was the issue have not specified the GS and what we have done is we have specified a property for laptop and we are saying that reference is lab one cool right so this is how basically you reference it now this is a seter injection for the reference variable what about the Constructor let's try so what I will do is let's go back to the same Constructor which we are using earlier but instead of saying int AG let me say laptop laptop and here this do laptop should should be equal to laptop okay so in of age I'm going for the the laptop now and in here we can specify a in fact I will do that here I say constru arguments and then instead of using a value which we have used for INT for the integer variable let's use ref and say the same thing which is lab one and our job is done so this is the Constructor injection let me try this and it works you can see it says Dev one which is the Constructor for the parameterized Constructor and we got compiling so is working this is the Constructor injection now which is better it depends if the variable is something which is compulsory when you create the object it's better to go with Constructor but if the variable is optional you can go with Setter because uh if you say you want to use a Constructor that means you need to have it when you are creating the object for Dev Setter you can set it later because we can call the setter method later not the Constructor so yeah that's how basically you work with Setter and the Constructor injection with the help of primitive value and the reference values so I I hope uh it was fun now to this point we were able to achieve Constructor and seter injection it's time to talk about Auto wiring if you remember when we talked about spring boot we have done this in Spring boot we have done with auto wiring but here also without spring boot let's try to explore it and even before we do that if you remember the spring boot videos we have talked about different classes and interfaces because in this case we only have laptop and Dev developer uh what we don't have is computer interface and desktop implementation of it so let's create those and let's get forward so what I will do is I will just go back here and say let me just copy and paste or maybe reuse the same laptop class but this time we'll name it as desktop and in desktop we'll change certain things I will just close this part and in desktop what I will do is when I say uh the Constructor should be desktop created and in the compiling I will say compiling with compiling in desktop okay so these are the changes I made so in the laptop it says compiling I will just change it to compiling in laptop okay uh nothing fancy just two different classes for the same concept and when I say concept these two are are the same type right so they both are computer so what I will do is I will just extract the interface out of it extract interface and we'll name this as computer uh you can do this in different IDs otherwise you can just simply create an interface and have this method which we want so if you go to computer okay void compile so I wanted this method I don't know what I made some mistake while refactoring it doesn't matter so you can see we got the interface which is computer and both these classes the laptop and desktop need to implement computer so I will say implements computer if this is not happening by default in your ID just try to manually type it that should not be a problem so we got the interface computer and then we got two implementation laptop and desktop okay just to go with this concept of coding for the interfaces now since we have made those changes I also want to change my developer now developers should not be dependent on a laptop right so developer should be dependent on a computer so I will just make mention computer here and here also I will just say this is a type I would say comp even comp works okay and then since we have made those changes I have to also change the in fact I will just remove this particular Constructor of parameters construct of Dev and then we have to make two changes here so when I said two changes basically I'm I want to change the geta sets not for laptop this time but for the computer so I will just say generate data sets for the computer I will say okay and now we got this two G set I me we got this two methods and then uh here also in the build I will say com. compile not laptop. compile because as a developer it doesn't matter which one you want to use which one you're using basically desktop or laptop it should work right uh so we made few changes and will this work that's a question let's try I have not made any changes in the uh spring.xml file so there's no confusion change let's see if this works and let's see what breaks when we do that so yeah we got the error and if you see the error it says error creating a bean def of course but why uh it says cannot resolve to a Constructor yeah because we have removed the Constructor and I think in the XML file we are still using this Constructor so what I will do is I will just remove everything since we know now how to we use it so let me remove everything okay so Dev is basically empty now and we got a laptop and the name for the ID for the laptop is Lab One what I want to do is I want to set some properties right so we have to basically set the property for uh the two things if if you go to Dev I'll just close this we got two properties we got comp and we got Ag and maybe we don't even need age now let's only focus on comp okay U of course even if it is there it should not make any difference but I just want to keep the code a bit clean so let's say we have only one dependency which is of computer here and if I go back to the XML file now the dev needs to have a computer object the property so I will say property the name of the property is comp but what should be the value of course you cannot set a Valu is because this is not a primitive type this is a reference type so inste of value it should be ref the question is what it is referring to now computer is an interface right so if you look at developer it is dependent on a computer and computer itself is interface so I can't simply create an object of the interface right so I have to look for the implementation and in this case we got two implementation we got desktop and we got laptop but if you look at the bean creation we are not creating the object for desktop we are just creating the object for laptop so basically in your container when you run this if it is it works basically Al you will get two objects one for your developer and one for your laptop okay so we will get this two objects now laptop itself is a computer right so can we just inject laptop in this developer when it is asking for uh the comp yes we can so what we can simply do is we already have the object created for laptop which is lab one we can simply refer it here our job is done right so now if I run this I hope this time it will work and if not let's debug it but it works you can see it says uh working on the awesome project laptop Constructor developer Constructor no where it's saying desktop Constructor because we have not geted the bean here and then it says compiling in laptop so perfectly makes sense but how about I want the object for the desktop as well so I can say Bean ID and I can say this is desk one and I say class the class is com. telescope do uh desktop and then in here okay we don't have any property there so now we got two objects and now if I run this look look at the output basically you will also get the desktop Constructor so it depends upon how many beans you mention here okay so now you even got the desktop object here okay so in total we got three objects but in developer Which object we are using we are using the lap one again we have done this before we know how it is referring now but yeah if you want to say this is desk one that's your choice I mean you can mention what property you want do you want a desktop or do you want a laptop so you can change that in XML file and now it says compiling in desktop so the way you change that it will basically inject that particular object in the developer so it will inject this not this one okay things are working out but what if I change the name here so let's say uh the ID for laptop is comp okay and will this work now of course I have to change here as well it will work because it will not be using com it will be using desk one but what if I say com will there be an issue if you have the same name uh of course not see this comp is basically referring to the developer variable name which is comp and the second comp this one is referring to the name of the Bean or the IDE of The Bean which is laptop be which is comp and now done this of course it will refer to the laptop and you can see it says compiling a laptop so our theory is working but don't you think these two names are same when you say name is comp uh the reference also comp what if I just skip this part I mean I spring is so awesome right uh if spring is that awesome I want spring to actually connect that automatically what we are doing is we are doing wiring and I want this wiring to be happening automatically we have done that in springboard here also if I just comment it and if I run uh let's see if spring is happy about it no no spring is not happy spring says it's still null null Point exception it's because we are commenting it and we are assuming that it will do auto wiring but spring says no no no I will not autom do it you have to tell me to do it so what you can do is you can just in the bean tag you can add one more attribute called autowire if you can see auto wire and you can say by name now what it will do is it will check okay so in this particular developer class we got only one property of comp the computer property the name is comp what I will do is I will search for this name in this particular container so in this container do we have any object with com yes so we have this name here so for this object we have comp for this object it is desk one so it will search hey do we have any object with name Comm and yes we do have it uh which is here so it will try to connect it and that's Auto wiring so if I run this your spring is Happy it says compiling in laptop is happy so basically that's how you use Auto wire but the problem is you can't use desktop here because you can't have two things with the same name example if you if you say both are comp uh there will be a confusion and you can see a huge confusion it says being com is already used right so we cannot use the same ID two times and that's why there are IDs right it should be unique uh so we can have it comp and comp one but let's say uh if you want to use desktop as well one thing you can do you can remove the IDS or uh you can change by saying hey I don't want to go by name I want to go by type even you can do that so this time I'm not saying by name I'm saying by type so when you're searching for comp is basically searching for these two components here and says okay we got laptop and we got desktop both are computer what I will do time is I will just commend this laptop and let's see if that works so by type it is searching for the type of a computer and desktop is a type of computer but the problem starts when you have both now if you see we got two beans of course they have different name but doesn't matter we are not even searching by name we sear searching by type the type of computer and desktop and laptop both are computers and now if you run this spring is not happy spring says hey you know I was searching for one but I found two there are two beans which are claiming that they are computers I can't I can't select one I don't want to be biased as a programmer we can be biased not the framework so what we can say okay in case of confusion give me the laptop so I will say primary is equal to true so you can use the primary attribute so primary says in terms of confusion go with this particular Bean which is the laptop so we are saying primary is true and we have done this in the spring boot as well with the help of at primary annotation but here I'm using the attribute and it says uh compiling with laptop uh so now I hope you are able to connect what we have done in the spring Boot and what is happening here of course XML is not something we love about but I just wanted to explain you what is happening behind the scene so now uh this laptop object is becomes the primary one we can do actually one more thing which which we have not talked about which is the app. Java in this if you see we are saying get bean and when you say get bean we are asking for the ID right I don't want to mention the ID what if I mention the type of the object I want I want the object of or not computer I want the object of Dev now will this work so we are not searching by name here we're searching by type and when you to search by type you don't even have to type cust it so it reduces the number of words you write and if you run this it works right so you have a choice you can you can get bean by name or you can get bean by type so in this case uh if you see the XML file the name is not important the type is important so you're searching for the object by type so that that's how basically you work with the autowire concept so that's it from this video where we talked about the auto wiring and I hope uh the spring framework without spring boot makes sense now uh enough of spring framework now let's focus on spring boot mode and then we'll move towards the project so I hope you're enjoying this series where we are talking about spring and spring boot uh and it's time to focus more on Spring boot now to this point we have seen spring Boot and spring and then we were able to achieve the same thing in both the projects so basically the demo spring the project which we have created is for the normal spring without spring Boot and then the my app is basically the same thing with with the help of springboard if you can see the classes we got the same number of classes and interfaces we got computer we got desktop we got da laptop and then the main class if you go to the demo spring the same thing we got app which is the main class and then we got a computer desktop Dev and laptop and we are doing the same thing so if you if you go back to your uh desktop or maybe if you go go back to your developer you can see in the developer we are doing the same thing we are calling a method of the computer and if you go to my app I think it will be done the same thing yeah so we have the uh computer object in dep and then we are trying to call compile both are same right but if you look at what is different is the configuration now both are doing the same thing but the the configuration is different if you look at the project with normal spring if you want to configure this we have to use a XML file of course the other option as well but we have gone for XML so in the XML we have to mention everything what we wanted if you want to get a bean you have to mention that if you want to uh have uh Auto wiring you have to mention that so everything need to be done in the XML file the problem is most of us don't like configuration even if you see sometime we don't have a choice we have to do configuration but then when you have an idea if you want to implement something and if you spend most of it most of your time in the configuration somewhere you lose a track of the project or you lose the interest or you spend time debugging your configurations example I have worked a lot on XML so I find it easy now or maybe I'm comfortable to work with XML but if you new to XML files or XML configuration even if a small spelling mistakes will create issue of course it will not give issue here example if I make a mistake uh here because of the ID I I'm getting the error but if you're not using an IDE and if you run this if you use a simple ID which is not showing you this error and if you run this and then you spend your a lot of amount of time debugging what went wrong right so that's one of the issue with the configuration on the other hand if if you talk about our project of spring boot there's no configuration file of course there is a configuration file which I want to show you which is the application. properties and of course you can set your properties here but at this point we have not done that we only got one property which is optional you can Al also remove this uh apart from that there's no configuration in the property file of the XML file we are doing those configuration with the help of annotations and sometime it is easy to work with annotations and at least this easier to debug so you know what is going wrong where now I just want to compare these two projects if you go back to your spring project you can see whichever class I open nowhere we are using any annotation not even here not in Dev not in laptop not in app there's no there's no annotation anywhere right and whatever we have to do we have to do that in XML in XML what we have done is we have created this beans and we are using autoare also we also specified primary here if you want to compare this with the spring boot we don't have XML but if you look at the classes we got Dev class on top of that we are using add component uh so instead of using a bean tag we using add component then we are using at autowire for the auto wirring uh we are using at qualify uh we can also use primary I think when we go for the desktop or laptop okay we have not mentioned but you can do that so you can mention primary here so we can mention primary here and that is say that is similar to what we have done here okay and so basically what we doing is we are using different annotations how do we start the container so if you go back to your main application file or main file uh using this line we are starting the container in case of spring the normal spring project we have to use this new class path XML configuration and this will start the container so both are doing the same thing but in Spring boot we are doing less configuration but the magic is whatever you are doing in Spring Bo boot behind the scene it is using the spring framework okay so yeah ultimately we using spring framework even if you're using spring board so those are the changes in these two projects and as I mentioned before we'll only focus on the spring boot from now before we go ahead I want to talk about more things see uh ultimately we are going to build a project right and we have discussed this in the first few videos so we are going to build a project and the project which are going to build is a web project right so whatever we have done till now that's basically a console project and now we want to work on the web project so whatever annotations we we have used or the configuration we have used that's for standon project now when you talk about web things are bit different how it is different first of all when you want to work with a web project and if you're not using spring if you're using normal Java and if you want to build a web project so what's so special about web project other thing is the web is hosted on the server and it will accept the request from the client so in this case uh we do have a client here who send the request to the server and server will respond back and this thing is happening on the internet with the help of HTTP protocol so we have to use this protocol uh to achieve this okay and this is host on the server so that means the application which we are building should be capable enough to accept the request over the Internet which is your uh HTTP request right so this has to be something special so you can't simply run this on jvm uh jvm is only for the machine now if you want this to work on the server basically we need something called a a web server or a web container basically and what you work with Java or what you use in Java is something called suets okay and cets run in the web container we also call that container as a serate container but which container we have to use which software we have to use which tool we have to use and the name of the tool is Tomcat so basically we have to use Tomcat to run the outlets but hold on we are talking about spring here right we want to use spring see even if you're using spring framework ultimately behind the scene spring is going to use svets okay and it will run on Tomcat so if you want to make a web application using spring behind this it will get converted into svets which will run on the Tomcat so it's important for us to use Tomcat as well now the question is if you're using if you want to use a tomcat here we have to install Tomcat we have to run our project uh spring boot says you don't have to worry about that in the project itself in the project folder of in your IDE you will get the embedded Tomcat so you will be having Tomcat inside your project so you don't need external Tomcat U those were the beautiful days where you have to do a lot of configuration for the Tomcat uh set the port number make sure it is running uh create a v file of your project then put it on inside tomcat and know listening to it looks lot of steps right and literally it was a lot of steps but now because of embedded Tomcat you don't have to about it with your project you will get a tomcat you can run it anywhere you want on the machine not on your car okay come back so uh basically we uh have Tomcat here next we need some layer which will accept the request uh so if you remember in the first few videos we have talked about different layers we're going to create we have a controller layer who is responsible to accept the request uh then we have to use service layer and repository layer now these are normal classes but then this controller is a special class because it was going to accept the request from the client and also respond it U and we have to understand how this works so even before we start with the project we'll focus on how do we create this controller and to achieve that we have to first understand spring MVC in fact spring boot MVC we'll be using spring boot here to achieve that now till this point we have worked with spring Boot and spring framework but that was for the standard application of console Bas application now we want to focus on the web see the project which we were discussing is actually a web project and of course we have seen the project before but uh if you talk about the layers we do have a client here right now this is a client and then we have a server with different layers and then we have a database of course we are using database here so that we can get some data and we want to store that data for the permanent purpose so that will store in database and then we can fetch it uh we are using a server which will accept the request from the client which will process the request uh maybe getting data from database so the different operations here the client is someone who is using this application so you are the client I'm the client so whoever opens this application or the website on the on the browser is a client now this client is not just a web application it can be a mobile application as well so maybe you can have one more client here which is your mobile application and then this mobile application can also send request and get response okay so basically in the mobile application the layout will be there what you are going to send from the server is just data and most of the time or most of the application which you are building now follows this approach where basically you only send the data so think about a mobile application maybe any uh any application which is your favorite example if you are watching a sport it can be any sport and if you don't want to watch the match you but you want to check this scod so we have certain applications which will give you this scode but if you open that application of course you will get the layout and you will also get data but let's say internet is not working so that app will give you the layout but that app cannot give you the data because data is coming from the server that means app will already have the layout what you are going to send from the server it is just the data same goes for your client see in the earlier days you know if you go back way back in the time uh when you send something from the server the server has to send two things first the layout and also the data right uh so when I say data it can be simple data but what about the layout layout has to be done with the help of HTML and CSS okay and that's what you will make your page look beautiful on the client side so server is responsible to generate both but nowadays what we are doing is we are using two different applications for the for the front end and for the for the back end so what you do is you use certain certain things like like react or maybe angularjs in this you basically have the layout ready which will go to the client and then you will send the request for the data from the server and then server will send the data and this data can be represented with the help of Json or maybe XML okay so that means as a back end you are responsible to send only the data the client can be a browser which has react application uh loaded of course you guys have to send that from the server itself but as a separate thing or maybe your front end or maybe your client is mobile application where the layout is already there what you will send here is again Json now there are different formats like Json XML uh Json is very famous which stands for JavaScript object notation uh which is a very simple and goodlook format data which you get on the client side okay so when I say good-look not exactly it will look good it's just that it is simple to understand and simple to represent okay so basically uh this is what we are building right now we are not concerned about react here of course you will get the code in the upcoming videos we are concerned about building a back end how do you build a web back end so we are going to create this controller this is what is important now how we are going to do this now this has to be a special special uh project not not the project which we are doing because in this project we don't have the web feature how do I know that see when you go to the pom.xml file we are saying that it is a spring boot startup project in the stter project you will not get web by default you want web if you remember when we went for the spring projects the web was a separate part so we have to add spring web here of course you can simply add a dependency that will that will make your job good but I don't want to add dependency now again it will be more confusing so what we'll do is and of course you can do that it's just that I'm avoiding it so what you can do is you can go back to your browser remember this this particular website which is start. spring.io this is where you can create a new project for web so I will create a m project and then the language is Java the spring boot version so when we have started the videos it was 3.2.5 now it is 3.2.6 upgraded version and okay so I will change the group ID which is com. telescope project is simple web app and then we are going to create a packaging of jar and the version which I'm using in this machine is 21 so I will go with that next we have to add dependency see spring starter boot or spring boot starter will be already there you don't have to add that what we have to add is web so we'll be using spring web enter apart from this I'm also going to add the dev tools see what happens is every time you make a change in your code you have to restart your server what I want to do is I want to have a live reload and that's what you get here so basically you can also it also improves your application restarts if you want to restart it will provide you live re live reload that will help to speed up so we just need this to of course we don't want to include database at this point but if you want to do that you can add one more dependency for databases maybe uh the MySQL driver if you want to use a postgress I'm going to use postgress but if you want to use MySQL that's your choice uh but at this point we don't need any of the dbms or databases driver so I will simply click on generate this will generate the project for me and that's what is here uh we just have to unzip it and load in your project so I'm going to unzip this so unzipping done and now we have to basically open that in the IDE so I'll go back to the IDE I will say open the project so this is the project which we got I will just click on open U I will use a new window or maybe this window I just want I don't want this window anymore and you can see uh okay we already have demo spring we'll close that so you can see this is your spring project and the beauty is this is a web project right and I have not done any coding yet in the normal world I mean before spring boot if you want to run your web application basically you have to run a server it can be a tomat server or some other servers basically first you have to run the server in your machine and then you can basically run your project after the configuration but now I have not done any coding still I will go back to my main if you can see we already have a main code here I'll just right click here and say run without doing any coding and it should work and it worked if you can see it says spring awesome this text here and it says the server started the server started on port number 8080 so that means even doing any code it is running at this point I will just stop it I just want to show you something I will just go back to my browser and I will just go Here Local H col 880 why ad8 is because we were running the project on ad8 I will say enter at this point it says this site cannot be reached is because the server is not running but what I will do is I will just run this now let's run the project and yeah bit fast cool uh it says it started on 880 I will just say enter can you see that we got something of course not the output we were expecting but we got something right we are not getting that page which says uh this site is not reachable you are getting something it says 404 because we are sending a request for the homepage and we have not handled the request how do we do that how do we handle the request maybe I just want to say welcome to telescope how do I do that here and for that basically we need someone on the server who can accept our request the request is going to the server but there's no one on the server side who can say Hey you know let me handle it and let me return welcome to the Lis let's create that particular person there not a person some service the way you can do that is you can right click on your project or you can right click on your package and create a new Java class and you'll be saying seriously we have to create a Java class to handle the request yes a simple class can do that let me show you maybe I'm just lying here let's try so what I will do is I will say uh maybe home I want to I want someone who can handle the homepage request now question arise who is that particular person can I say person here doesn't matter who is that particular person who can handle this and that's where we have talked about the controller so we need a layer here a class maybe who is a controller who can handle your request so I will say this is my home controller and in this home controller I can accept the request and respond but the question is if you want to do that in Java how do you how do you make something work so basically we need a method here so I will create a method and this method returns a string because I want to say welcome to the list Cod right so I will say public string it returns a string and the method name can be let's say greet so someone is coming to the hom page I will say greet and I will simply return welcome to theis score that's it simple class simple method and you just save this and restart and see the magic so I just restart this go back to my page and the magic nothing happened that's the magic nothing happened see the thing is spring is awesome spring is Magic but not entire magic we have to do we still have to do some configuration uh not much the problem is see this is a normal class right now spring has no idea that this class is responsible to handle request for for the homepage and we don't want spring to guess because once you start expecting your framework to guess something for you it becomes uncontrollable you you don't know what's happening behind the scene or uh if something is buggy it will be difficult to debug and that's why you need control so what I want to do is I want to say Hey you know this is a noral not a normal class spring framework this is someone who who is a controller so you just say add controller remember in spring board we have used add comp component in the same way if you want to create a controller just say add controller will this do a job uh see not exactly see on the website you'll be having multiple request we are requesting homepage maybe after sometime you want to have contact us about us add to cart or maybe all those features right everything is a request request with with different URL what URL I'm talking about see here you get URLs right like we can have about or maybe you can have contacts so this is important at this point I'm not passing anything so that's blank we can only say slash enter so I want to do it for the slash so for every request you can create a different method which will respond so at this point I'm requesting for this if someone request for the slash which is the homepage I want this to be get getting called and to do that we have to use a method called request mapping okay so this is annotation not method so we have to use annotation which is request mapping in the backet in the bracket of this we can say for which request for The Clash request I'm I want to call this so that's request mapping now will this work say we we learn from errors right and we will get errors here let's see but what error you get I will just refresh this and it says okay it still says 404 U that's weird but if you see it is also saying something it says no sta resource welcome to the Disco something different okay it says welcome to the Disco but it is not showing the text but it says no static resource welcome to teliscope see what happen is a controller behind the scene and this is how spring framework was built before and depending upon your use case you can customize it so what it does is it says okay I'm going to call this and I'm calling this method okay the method is getting called to show you the proof what I will also do is I'll say I'm here just to show you that this works I'll just restart the app and go back here say refresh is still not reloaded or done if I show you the console it says I'm here that means this method is getting called for sure but still it's not working the reason it is not working is what your spring framework does or spring web does is it says when you call this method and when you return welcome to theis it will look for a file called Welcome to theis but why file remember when I talked about the old days when a server used to send the layout with data and that layout is a page so you're searching for a page called welcome to theore in our project we don't have that page and we don't want to return the page we want to return the text right and to achieve this what you can do is instead of using a controller you can use something called a rest controller now rest API is a concept where you return the data from the from the server to client so rest stands for representational State transfer you basically transfer the state the data from the server to client not the layout only the data and if you want to do that automatically you can use rest controller this is one way uh the other way is you can send here you can use one more anation by saying hey I'm not looking for the page I'm looking for the body the data so you can say response body so what it will do is it will return the data now U I hope this will work if the tions are correct go back here and refresh and you got welcome to theis go so basically what we are doing is we are returning the data not the page but if you want to create the pages you can do that with the help of template or JSP you can create those pages here and you can return them by mentioning their names here but we don't want to do that we don't want to return the page because the page is there in the react application what you want to return is the data and I don't want to use response body from now what I'm going to use is rest controller because we just want to return data so we can say rest controller and even that works do I have to restart the application we'll do that quickly and say refresh it works so that's how basically return for these flash what if you want to do something else maybe you want to return for the about page so if someone is requesting for let's say slash about you want to return something for that slab will not work now is because we're not handling it so if you want to handle that it's very simple you can say public return something and I will say about method and you can accept you can return we can use our tag line we don't teach we educate okay so I want to return this this is tagline and then I'm going to say request mapping and this mapping is for the request about so you will say slash about here so for different request you specify different uh methods and there's no compulsion that this about should be same as this one it can be different restart the server reload and it says we don't teach we educate okay so that's how basically you can have different methods here and that's how you use spring web but then if you see this image we have multiple layers here we got the service layer we got the repository layer how do we create those things of course we'll discuss that in the upcoming sessions but just to give you a hint for different layers you will create different classes okay so for service we have different class for for repository be different class and they will also have their annotations not controller but something else next there's no compulsion that you should put all the request in one particular controller you can have multiple controllers here okay and that's the beauty so you can have multiple controllers and let let me create one for you so maybe I will just say this controller as um a login controller and in this I can handle the login request so request mapping not request request mapping I don't need this import request mapping for let's say login request and this has to be a controller AR rest controller to be specific and here we can have a method so I'll say public it should return a string of course it should not be a string but at this point I'm saying string and I will return something because we're not focusing on logic here we just want to check how can we have multiple controllers and I'm going to say login page okay nothing fancy just a login page uh and I forgot The annotation here okay now by doing this what what do you what do you think will this work and why I think it should not work in general is we have multiple controllers right and multiple controllers has different requests we got login here we got slash and we got about how your spring framework knows for which request we have to go to which controller don't you think that's a confusion we can have multiple controllers we can have 10 controllers 20 controllers we can have 100 controllers how your spring framework knows which one to call first of all let's check if this is working and then we'll discuss how it is working or I mean if it works maybe I'm not good with creating a suspense so I'll just restart this and about is working but what about login even login is working so how it's working so what happens is spring MVC basically or spring web basically has something called a front controller now whenever you send a request from the client the request first goes to someone who is here so there's one more layer here which you can't see which spring gives you and this layer here is your front controller and this is not something which you create spring will create this for you and this front controller sees all the request mappings so it creates request mapping for all the controllers and it knows for which request it has to send a request to which controller and that's a job of your front controller okay and then the request goes to the controller so that's about the introduction can I say introduction yes because when we start working on the project you will see more but that's how the controller Works maybe you have further questions what if you want to send data from the client to the server we have not done that we're sending data from the from the server to the client what about from client to server in that case we have to send data here this is where you will accept that data now to this point we were able to work with spring Boot and also we have created our first spring boot web project and of course we don't want to discuss everything about web we want to do that once we start with the project but there are certain things some Basics we have to understand in terms of Basics we have talked about how do you map a particular request so we have done for two requests here one for the homepage if you can see here and one for the about page and in fact we have done one more if you remember for the login as well we have done done this so in total we got three requests right now in this request basically what we are doing is we are sending a request from the client to server and then on the server side basically we are mapping it and returning a data now when you return login page or when you return uh this text like welcome to theis or we don't teach we educate basically what we are doing is we are sending the text back to the client right we are not sending a HTML page in fact we have talked about it when we use rest controller here it Returns the data but if you remove the rest from here it will return the page name so basically your spring framework will say okay it says welcome to the discod that means I have to search for a file called Welcome to the lisco right and then it search for the file but we don't have that file we just want to send data we don't want to work with the UI part here we just want to work with data that's that's more important right and it's working but let's say you don't want to return a normal text see most of the time when you talk about any particular application you want to return data for a particular entities right example if if it is a e-commerce website uh you want to return the products right and product will not just have a name product will have other details as well the product ID product name product price product category so it will have all those things now same goes for let's say if you want to book a flight in Flight as well you'll be having flight details you'll be having a timing you'll be having a flight number and all those details and think about any application we normally work with uh entity and in the world of java we represent that with the help of objects so that means if I want to return some details it can be multiple entities not just one so if you talk about the e-commerce website if you search for a particular product let's say if you want to search for a laptop now it will give you a list of laptops so not one list of laptops now the idea is how do you send that data from server to client in which format will you send as a normal text of course not right because on the client side as well if it is a react application or a mobile application they cannot accept a normal text it will be difficult for them to convert that into a UI or read read it from the text and that's why we have to use a Json format it can be XML or Json nowadays we are using Json so we'll use that so basically what I want to do now is I want to return the data not the data normal data I want to return the object the object will have the data which I have to return back to the client now this is tricky not in terms of spring this is tricky in general so what I will do is I will create a new controller and this controller is responsible to return uh let's say products so I will say product controller and we are going to use this later in the project so I will say product controller okay a simple product controller nothing fancy and then this is responsible to accept the request for the products so when I say public uh return a string at this point I will say string but we'll change it later and I will say get products so maybe I want to return all the products in the database and of course in this video we're not going to work with database we are just going to return the dummy data which we have so a list of products and I want to return something here at this point I don't have the data now since we are not working with database let me create some fake data and the way I will do that is first of all I need a class which will represent a product so when I say a product what it should have product ID product name uh let's say price let's say we have this three so I'll just right click here and say new I want to create a class which will represent our product and this will have certain things so it will have private we can also use lombok here then we don't have to type lot of things so what we can do is we can get the longbo so that in fact I'll first of all type this so I'll say private in uh Broad ID then private string Brad name and private in price let's say we have this three later on we can have we can increase this so at this point we got three and now I want to use longbo why do we need longbo is is because when you use a private variable of course I have to create data sets for this Constructor as well so instead of that I can simply use longbach Library which will help me to create those things behind the scene uh which will make it bit easy to read and how do we get longbo it's very easy you go to pom file and you add the longbo dependency here so somewhere here okay but I don't remember the longbo dependency or the group ID and the artifact ID what you can do is you can go to your browser search for mvn repository and search for lombok this is the project L lombok let's get this and go with the version which is look which looks stable so this one it got 3,000 use so I will just pick up this one copy this and paste it here okay now once you do that of course I have to remove this line as well uh I will save this and you can see the there's option of M and reload I will do that so that in my dependency I will get lombok as well so if you expand external libraries and if you search you will find lombok somewhere it should be in or Lok where is L okay project Lok is it yeah so you can see we got Lok here right now once you have that you can go back to your we can minimize this we don't need external dependencies ruce the size of it and go back to your product cont notu controller product and here we can add an annotation called Data so this will give you the gith this as well and now once you have this you can go back to your product controller and this is where you can return the data now normally if you talk about the layers if you remember we have talked about different layers right we got controller and if you if a controller wants the data now it will not ask data from database it will ask data from the service layer now this is where we have to introduce new layers because you can you should not be writing any business logic inside the controller controller is just for accepting the request and responding to the client not doing any business logic okay so we have to get a service layer which will have a business logic so what I will do is I will create a service layer here because see when you talk about oh what I'm creating delete this file not needed yeah so here from get products what I want to do is I want to get all the products from the service I don't want to write the logic here so let me create a new class and this class will be responsible for the logic so I will say product service and I want this to be in different package so what I will do is I will put all the controllers if you can see we have multiple controllers here controllers should go in controller package uh the service classes should go in service package and the product go should go in the model package so there's a concept of MVC model view controller so the job of a controller is to accept the request from the client responded so in view we basically return the page now here we we're not returning a page we're rning data but still it's a part of MVC the model represents data so if you talk about the product here this represents the data right so this is model package so I will say new I want to create packages uh one for the controller now this package will have all the controllers so we can just move that there refactor and I want to create a service package as well so I will create a package which will have the service because in future we going to get multiple service classes so I will just move that here and then I want to get model as well in fact once we start your database we'll also create a repo package not a class I want to create the package this is for model and we'll move product to model okay so now we are creating those packages it will look good and also easy to find so all the controllers will be in controller all the models will be in model all the service will be in service okay now in the service class basically I want to create a method which will return me the list of products so I will say list of products and I will say get products we can have different name but that's fine and we can return it now since product is not part of this package I will just import this and we got it and we have to also import the list so we got it and now our job is to return the list of products now the question is where do we find this list we don't have a database so time being we can create some dummy data so I can create a list of data so what I will do is I will say list of uh product and I'll say products is equal to and let me create uh the product like this ARS dot as list and I will say new product okay I want to have a Constructor as well which will accept this so what I will do is in the product I will add one more all argument Constructor here so that it will create a Constructor for me so this is a feature of Lok I'll just go back here and say I want to create the list of product which will accept the first one product ID then I I will say iPhone prices let's say 50,000 okay it's not accepting all arguments Constructor but anyway we'll try to run this and see what what goes wrong so and then I will also say new product and this time we add one2 and let's say we have have two now we just want to see right if it's working or not I will say Canon camera because that's what I can see on this on on the desk and then I will say price is 70 okay so we got this two data it's not working because of Lok I think when you run this it will it it has to enable the lombok uh thing just see if this working okay there's no compile time issue so of course that's a Lok problem and it says 80 Port is not working it may be because the port is busy with something else what I will do is I will change the port number so this is the problem with uh with my machine every time I close my idea it's not able to close the port number it may not be issue with you uh so it says 880 is busy so what I will do is I will change the port number so we can do that so if the port number 880 is busy I will say server. Port is equal to 081 so now this time it will run on 081 even 081 is busy that's weird that's weird I don't know where I'm using all these ports so I've not restarted this machine from a long time so maybe that's the issue uh and I've not closed the runtime properly so 8086 okay there's some other issue then it's not the problem of this so what I will do is I'll just try to solve this first okay longb is not working properly as expected I'll say mavin reload project okay what I will also do is I will also make this as a component and it looks like most of the ID problem not this and for the service class we can also use component or we can use service package or service annotation so basically for different layers we can use different annotations ultimately they are same even service package service annotation uh behind the scene is a component you can see when I go here it is also a component but uh it is similar but there are some differences which only works for a specific use case and it's still giving this problem so maybe lombo is not working so what I will do is it's fine uh we can just remove this and create gu ATS it normally works I don't know what's going wrong there so I'll stick to my basic thing so we will right click here and say generate and let me know if it is working for you on your machine there might be some issue uh right click generate Constructor for all okay so we got the Constructor now and now it should not be an issue and you can see this is solved some isue with the longb okay once we have done this uh next we have to return this products so basically we got the products we can return it in fact I we can add one more now since it it is looking good uh I will say 103 I'll say sure mic and price is let's say 40 I'm not sure what's the or maybe 10 okay so we got three products now and we are returning so as you can see all the logic we are writing in the service package in the controller nothing now what controller will do is controller will get this data from the service so in here we have to create a product service and we'll name this as service is is equal to new product service now this is what we don't want to do right we don't want to create the object by ourself we want spring to give it to us and once you got the service I can simply say service because service will return the products so when you're asking for the controller controller will say okay let me get from the service so service. get products but it will return the least of product not one it will return the list of products now this will not work is because we are not writing atate Auto wire so we need the object so we can ask for it so the object will be created inside the spring container because of this service annotation which is component behind the scene and you can simply autowire it now some some certain things are missing here on top of this we have to say rest controller and on top of this method we have to say request mapping and for which request so whenever I say slash products I want this so whenever you say SL products you have to return this so simple right uh will this work that's the question let's try so what I will do is uh this time I I hope it will work I will just relaunch this I don't know why it is saying the port number is busy okay okay now this time problem is different it says uh product required for a bean of type int could not be found the parameter is zero of the Constructor in model required are being of type in that could not be found okay so let me go back to the product it's fun to debug the applications right so it says prodad ID we are returning int for the first one and in this service we are passing int not an issue okay is it required to have a no argument Constructor okay this time we got another error it says port number 8086 was already in use okay let's solve this looks like there's some issue with the IDE what I will do is I will just close this okay reopening done of the intelly idea let me just P change the port number to 8090 I don't know why all the erors are coming today there might be some problem with the ID or maybe I've done I'm doing some mistake let me know in the comment if you can find my problem here sometime you can't debug your own stuff okay doesn't look like a problem of this server is something wrong with the code I'm not sure we are writing rest controller we are saying that this is a service we are returning the products mapping is done properly uh the product service we are returning the products we have add aate service here things are looking good I don't see any problem is it the issue with the lombok library let me just get rid of lombok library restart package lbo does not exist I'm not even using them remove launch NP I'm not sure what is going wrong and things are getting lengthy so I will just restart the entire system and it will work and let's try to see what was wrong okay now now this is a part which I'm recording later after some time in fact few hours and we know something right whenever something goes wrong if it is not working uh after trying for making some changes if it's still not working we what we do is we restart our system so I've done the restart and I've also ran it it was working so I'm just doing that once more in the video so I will just relaunch this nothing changed you know port number is still same I just restarted my system there was some issue with the uh maybe the system or the intellig I'm not sure but now it's working you can see it says stormcat started on 880 now how do I tested if this is working or not so one thing you can do you can open your browser and go to Local Host 880 and search for products okay still not working because the port number we have changed it is 8090 enter and now you can see we got our products uh so I I also installed a plugin in my browser which is pretty print maybe because of that I'm getting this option but you can see we got the products here I know this doesn't look good because in the real world we want to see the page right uh the design but then we have talked about it we are not concerned about design here we are concerned about the data and we got it here this is one way of doing it we can also get this data on another tool so whenever you want to test your apis basically what we doing here is we are building apis which will be used by mobile application or it will be used for uh a browser using react application or angular and this will send request to the API API will send data so this one way another way you can also use is Postman but how exactly we use Postman we'll talk about that in the uh next video but at this point uh we are able to run that on the browser just wanted to finish this video with working output so basically we were able to get the products right so if I go back to the browser and if I hit the URL for the products it will give you the rest of products of course I can predy it and you you will get in this format now basically you got all the thing which is your server returning to the client of course the data now basically we fetching everything right and that's what you can see here what if you want to fetch one what if you want to add something in this list now this is something we are going to do in this video but before we do that uh I want to talk about the other type of API tool which you can use not just your browser because see when you talk about rest API and if you want to basically work with data what we normally do with data is we send data so you have your client here and then this client will send data from client to server then it will also accept data from server to client what we are doing here is basically we are sending the data from server to client right but we do certain other things as well we send data from client to server right uh to store something in the database if you want to do that uh sometime we want to so basically we are fetching and we are storing right so we got two things Fetch and send or store next what if you want to update what if you want to delete certain things example the product here I want to maybe I want to delete this 103 maybe I want to update this particular price here so this thing you can do with the help of some type of request now rest API basically uses a protocol called HTTP we have talked about it before and it has certain methods to work with so basically your rest API uses a protocol called htdp which has certain methods to work with so if you want to fetch something we have a get method if you want to store something on the server we use post if you want to update something we use put if you want to delete something we have delete apart from this we have other methods as well but these are four we normally use now we also use something called a crud shot which is for create read update delete so we can create with the help of post uh we can read with the help of get we can update with the help of put and we can delete with the help of delete so these are the methods which is available so rest uses HTTP and we are going to use this now by default when you say when you pass a URL in the browser it sends the get request now this is what we have done but what about post putut delete now this is not something you can do with your browser right so you can't simply use your address bar to change the method name now this is where we have to use certain API tools to do that one of the tool we have multiple tools one of the tool is Postman so if you know how to use one tool it is easier to explore the other as well so here what we going to do is we are going to use this particular Postman uh if you want to get this so let me just talk about this so this is is a postman this the logo looks like so if you search on the Google uh I can also get take you to the website this is the official website of Postman you can get it from here uh it's free it they also have a paid version but we don't want it we are going for free version here and it works okay of course I could have used some Advanced recent tool but that's fine if this works so how exactly we going to hit the URL it's very simple you can say Local Host colon in 8090 that's a port number we are using uh Slash then you have to say products now if You observe we have a method called get here right apart from this get if you expand there are multiple methods we have postp put patch delete head options we are going to use only four so let's say we have a get request and when you say when you click on this send button it will send a request and you will get the response so this is a response you got apart from response you also get a status code here which is 200 now there are different status code options available uh in the series of 100 200 300 400 500 if you have seen one of the most famous uh code which you get is 404 for resource not found uh so that is a part of this status code it it comes under 400 series so if something is wrong with the resources it will send you 400 series if something is wrong with the server it will go for 500 series if everything is going well it will go in 200 series so that's how we have different series and we'll talk about it this series in the upcoming videos but we are getting the status code as well this this is a response time which is 200 and yeah and this is a size of data which you're getting back so basically this is your data here apart from the body you also get some headers from the server uh this is the header so the type of data you're getting is Json that means you can also ask for the XML data if you want but by default it will send the Json data so let me go back to the body and you got three response if you have more you will see more uh you can also check this in text format and this is how it looks like but we want to see that in Jason format which looks good and that's what you got here okay so this is what we have done to get data but what about you want to let's say get one not all one something like this I want the products for sure but let's say I want the product with the ID 102 can we do that so we can simply say sl102 this is how basically you also build your rest apis right so you simply say products which is URL for all the products and Slash the ID which is the which you want to fetch one2 and when you say send it should fetch only one record now this is not working and you can see we also got a status code called 4004 or 404 uh the resource could not be found we have talked about it and this is the error which you got again not a good-looking error but that's fine uh it says not found let's solve this I want to return one data how do we do that so I want to hit this URL now to achieve that we have to create one more method which will accept this particular URL products will only uh accept products not the slash and the number so what I going to do is I will say public and this time we not going to return the least of products we're going to return only one product because we are accessing the uh uni field which is product ID so I'm going to say product return one product and instead of saying get products I will say get product by their ID so we can just use this name you can also say product get product that works I will say get product by ID and here I will accept the ID so whatever ID I'm I'm sending from there so I will say prod ID whichever whatever you're sending from the client and based on that ID I'm going to fetch so who is responsible to give me that so I will say service hey service it is your job so I will say get product by ID I want this method in the service lay as well U and then I'm going to pass the ID now unfortunately we don't have this method called get product ID or get product by ID in the service layer and if you see the service class let me just close the application properties we don't need that now if you go to the service class of product we only got one method here right which is get products I want this as well so I can ask my ID to give me that so I can just click here and say create method and we got the method now but it has to do the actual work right now this is tricky how will you get this particular data now the data is something in the list right which is in this added list so I can return and this has to come from the products so we say products. getet now I'm not sure in which particular method I can get that I just want the product based on their ID right so basically what we can do here is we have multiple ways we can use normal for Loop enhanced for Loop to get the data or I can use the Java stream API if you're not sure what stream API is you can find the link in description if it is not there let it me know in the comments I will just put that so what I will do is I will say hey I got the list of products from this products I want to create a stream because using stream you can filter the products so I say filter dot uh I mean stream Dot and I can apply a filter now filter based on what for any product which I get from the stream If the product get product ID if it is matching with the product ID okay the moment we get this and I don't I I just I just want to stop this search once I get this product and I will say find First because stream has no idea that we we have a unique product ID stream says okay I'm I will filter this but there might be chance that you will get multiple so I will say find first to get the first element and then find first dot get now this will return you the product okay so this is the logic of course you can also use normal for Loop and if you have that logic with you put that in comment so that others can see it and if you're not sure what stream API is you will get the link in description okay so this implementation is done uh in the controller I'm asking asking the service to do that for me and now I want to check if this is working let's restart the application okay it's still working and done now where do we test it so of course I can do that in the browser so I will say products slash I want to mention 103 and we got the error okay still not working something is wrong now what is missing here most important thing we are missing the request mapping so the request mapping has to be done for the Slash products slash for 103 right we have to do the mapping there and now after doing the mapping of 13 let's see if this works and this time I'm going to test that in the postman so I will say send the request for 103 send okay it's still not working it says optional uh in parameter product is present but cannot be translated to null due to declare as a primitive type okay so what's the next problem okay first of all there are two problems okay u i I wanted to show you that problem but then then I realized I have to first talk about this one see the problem here is you are passing you're you're writing one3 right which is the harded values don't you think we are not going to search for One Z3 always we might want to search for 102 or 101 or 1010 later how you're going to change that so I don't want this to be static I I don't want this to be dynamic so what you can do is you can use a syntax called you can put a curly brackets here and you can put your variable name or the some text there which is for ID not the number now what spring will do is whatever is is coming in the browser so which is this it will convert your 103 or it will store your 103 here it will map it and that has to be mapped here in the prod ID apart from this you have to also use a annotation called path variable because we want to match it now so you want to match your prod ID with this BR ID so you have to use this anation called path variable okay there are lot of lot annotations will be coming up and once you get used to it you will remember but since we are accepting the value from the path of the address bar or the address The annotation name is path variable and by making those changes we have solved two problems and now let's see if this works with 103 send it worked for 102 send it worked for 101 it worked for 105 which is not there it will give you the usual error but then this time it's not saying that F it's not found the request has been sent it's just that the data is not there right U you can also solve this you can hand this handle these exceptions by uh handling it on the server side but we're not going to do that now okay so this is what you got uh you can do one more thing if you want uh in the service when you say get here you can say find first dot or else you can pass a new product with a dummy value let's say one 100 no item and the price is zero so what we'll do is if the item is not found because 105 is not there in this right so at this point you can return with uh no item so now if I restart the app there is one way of one way of handling it not the best way but one way now if you send request for this you will see you got 100 no item zero price okay but again not a good idea or not the best solution so what we have done now is We have basically fetching the data with the help of Slash with the product ID next maybe I want to send data and store that in this uh in that in this list here so how do I do that what I want to do is I want to create a method which will add the product so basically we have to send this product from the client to server so I'm doing that in the service now so I'm going in the reverse order uh for the get we have we went from client to controller controller to service this time we are coding from service so I want to basically first of all accept the product and I will say prod here and I want to save this product in the list the list name is products dot I want to say add and I want to add let's say I mean I I want to add this product okay so this is done uh now we have to make the changes in the controller so in the controller as well I will create a method which will return nothing and we can say add product which will accept the product from the client and this is beauty right because on from the client side you are going to send Json on the server side it will be getting converted automatically in the object format in fact if You observe when you are sending the list of products or even a single product you're sending the object format right and what you are getting on the client side is Json who is doing that someone is converting the data from java object to Json from Json to Java object and that's where you got a library here so when you add a spring web you by default get one of the library called Jackson so if you expand this there's a Jackson Library here now this Jackson library is responsible to convert your object into Json from Json to object so it does that for you so I will say product Brad and we can simply use the service layer service. add and pass it so if You observe we are not doing anything in the controller right controller job is just to accept the request if you want to do certain things ask your service cl to do that you don't do it right so this is fun but but we have to do the mapping as well so I will say request uh request mapping and this time we'll do it for the product so we're not saying products we are saying product or we can also say products that's fine sending a request for the products right but here's a problem if you see this products and this looks same right that means for two different methods we using the same URL will it work it will not there will be confusion right and that's where you have to you can use those methods remember post get but we have not used this method right this is get request this is also a get request we have not mentioned it it's because by default the methods are get get methods if you want to use something else we have to mention that so you can give a comma here and you can mention the method type or uh you can use some specialized annotation here so what you can do is instead of using request mapping you can say get mapping so here also instead of saying request mapping you say get mapping and for post instead of saying request mapping we are going to use post mapping now these two mappings are different is because of their methods the URL looks same this one and this one but the methods are different right so the controller and service done now let's try to work with the client side see for the Post request we can't simply use the address bar you can create a react application using which you can send this data otherwise you can also create a form but again you have to do that in the JavaScript you have to do some code to achieve that but we're not not going to do that we are going to Simply use Postman so after making these changes I'm going to restart the application and I I think it might fail because of the way you have defined the list let me just check that later uh let's go back to the postman and now instead of sending a get request we have to send a post request what is a URL the same URL the products right we not changing the URL between the get products and uh add product we're just changing the method here uh this is a response we not not supposed to touch the response we have to touch the uh URL I will click on send and you can see we got an error unsupported operation uh okay it's it's with the list right so if this is the error I'm talking about but okay we'll talk about this error later but one more thing we are not sending any data right so basically we have to send a data as well in the body here okay now how will you send data it's very simple you click on the raw here and select Json because you're sending Json data and you can enter your Json data here but if you're not good with Json that's fine what you can do is let's send the request for 101 uh so this is a history you can just get your old URL and you can hit that so we got this I will just copy this and uh go back here for this for the Post request go to body raw and Json put your data here and update it so this is 104 I want to create a new one let's say um what I have in my on my table let's say AC remote and the price is let's say 2,000 I don't know how what's the cost of AC remote so let's say we have 2,000 and now we just have to send it again we got the same problem so we are sending data there's no problem with the sending data the problem is I think when you create as list it creates a immutable list so what I'm going to do is I'm going to say new add list and I'm going to pass this add list inside this so it will make it mutable is not able to add the data so new add list that's what I added here let's restart the app and now hopefully this will work I will say send okay something worked we have not gotten a response because the in the controller we are using void right it's not returning the data but you got status it says 200 how do I verify this that's tricky right the way you can verify that is by sending a request for the get let's get all the products and okay no it's not working you can see we got zero null and zero it's not getting added uh what's wrong let me just check my controller I know the error I got to know the error but I want to show you that what I will do is I will just try to print whatever we accepting on the client on on the server side and let's see what you are getting that will solve the problem so server is start done I will send the same request which we have done before the body is still intact I will say send we got the same response but if you see the okay what my bad my bad my bad now since I thought we are using lombok which we are not doing that I should have added two string method because in Longo you get that by default now since I'm using long from a long time it normally works so I say two string and restart it was printing the address I don't want the hash code uh let's say send and this is what you're getting on the server so that means what you're sending from the client is not actually getting received on the server side it's because to achieve that we have to use one annotation called request body now since you are sending a body from the client to the server you have to use this anation called request body now this will try to match the data which you're sending and put that in the product uh the prod thing here okay I hope this time it will work I hope and you learn from it right you know now why why certain things why we are using certain things that where we are and now send same response but on this client side if you see or on the console if you see you are getting the data what you're sing from this from the client and now uh let me send the get request send go down go down and you can see we got 104 good right the only problem is we are using a list in the code it's not sh in in the database the moment I restart the application that you will lose this data but anyway the point is we it's it's working so now we know how to use get we know how to use post we also know how to get the data using one how about put and delete uh let's try to do that in the next video so to this point what we have done is now we know how to use Postman to work with the apis uh we have sent a get request and post request it's fun so just want to show you the product controller once so we got three mappings uh get mapping get mapping and post mapping now it's time to write the code for for the update and delete so what we have done till now is basically we have this products uh page or the controller and we are sending a request to get all the products we are getting that on the client side it can be a browser or it can be a API client like Postman and then we are also fetching one product details or we are able to add new products and we have done that in Postman so if I open my Postman so this was the request right so we have sent the request is using which you were getting the uh products we have also done with the post request in the post basically we are sending data as well so what we'll do is uh before we go for the update and delete let me add this product and then we'll do that and I can't do this now so if I send a request it says the server is not running so to make sure the server is running so I will just restart the application and hopefully this time there will not be any issue uh because I'm recording after a long time and if you see here uh it's running it's running on port number 8090 if I go back to postman first of all let's say get the products and this time it will show only three because see we are not storing this data in the database every time you restart the application uh you will lose your data so basically we have the hardcoded link list or we have the hardcoded list which is in the service this is what we are returning right this products now I want to add a product so if I click on the post which is having this data which is 104 and when I click on send and you can see we got 200 okay but again if I say Get it should be having four records and we have it so now uh let's try to update something let's say I want to update the price for this show mic I don't want the price to be 10,000 let's say let's make it 12,000 how do I do that see for the update what we have to do is we have to change the type of the request first of all let me just have this data with me I will just copy this and next I will change the type of method so instead of saying get and post we'll go for put so put is used for the update and I will go to the body and I want to say I want to change this data so this is the new data which I which I have okay so how do I make it work if I click on send now it will not work because we don't have any method which is accepting a put request so you can see it says method not allowed so how do we solve this it is actually simple what we can do is let's go back to the controller and here let's create a method for the put so I will say public void I don't want anything return or maybe we can simply return the data or we can avoid it so we'll say void void void okay that was not intentional but anyway so I will say update product and here so when you say update basically you are sending the data as well right so we have to accept the data in the product prod it can be any name doesn't matter and I want to use the request body annotation we have used it for the post as well uh this is this is responsible to get data in the request and and send that in this reference so here what we can do is we can say hey Service uh I got this request and I want you to update the product okay so I'm doing that and I simply say prod here and that's done but the thing is we don't have this method in the service so what I can do is I can just click here and say create a method update product and we got it this the tricky part when it comes to doing this manually with the help of this list It's Tricky see in the normal world we this data in database now because of a module called Spring data jpa it becomes very easy so we'll see what exactly spring data jpa is in some time in the upcoming videos but here we have to do some manual work but trust me once we start with spring. jpa we just have to change one line and it will work okay but then here I'm not sure about the code let's try so I will say products dot uh I don't want to add now I just want to update so do we have a method which will update this unfortunately we don't have it so we have to go in a manual process so if I want to update a particular list I will say get can I set the value to Yeah so basically this is what we want to do we want to set the product on a particular ID but I don't know the ID where to set this that's a tricky part so the way you can get the ID is this so you can run the loop maybe I I can use a normal Loop here I can just run the loop here from int0 to I less than products. size i++ I just want to get the ID right or the index so I will say in index I want the index index will be originally zero um but this is a problem here right so okay we'll talk about the problem in some time basically I want to iterate and if I want to check if the products. get I do get product ID is equal to brad. get product ID if this is matching I will set index to that particular I value right since for only has one thing we can just remove the cly brackets and I got the index and the product so basically what we have done is we are setting So when you say set it it will replace basically we are changing the data or the object with the new object which we got here so we have we are writing such such a lengthy code right if you have a better solution let me in the comments this is not the best solution but once you start with data jpa this will become easy okay uh I know I'm getting suspense here but you will see that later and let me know if you have a better solution to update the product list okay will this work let's try first of all I want to map it also so I will say put mapping and here I will accept the request for products the same uh list or the same URL but then with a different uh method and once we have done that I will just refresh this the problem is once once I restart the application we are losing that fourth uh object because again we have to add so let's say we only have three records and now if I go go back to my Postman let's send the get request first of all and see what other data we have so you can see we have this data and I want to change the let's say this time let's let's change the second one okay and let's change the price for this and for this I'll be sending a put request you can see we have a put request for the same URL and the body I want to change it with this but then the instead of saying Canon camera I will say Canon only let's remove the camera and says send okay it says unsupported media type okay if I check the data okay let's debug this so what I will do is I will just try to print something here and I just want to check if this is getting called not sure what is going wrong and click on send okay same error but is it printing that now it's not even coming inside this so you can see it's not printing I'm here that means the update method itself is not getting called oh I think I got the problem so I'm sending text I should send Jason maybe that was the issue oh such a simple problem so I was thinking about something else not looked at Json here it was text and now we have solv it so you can see we got an update says 200 and now after making the changes I will say get products and boom can you see that in this second data we only got Canon now we don't have Canon camera so this is working so put we have done the next step is to do delete so now how delete will work so basically for delete we have to send a delete request and okay question arise how we are going to delete this so we'll say product slash and then we'll give an ID so instead of sending the object to delete we can send the ID product ID so maybe one1 one Z2 one Z3 let's say I want to work with one2 and I want to delete this so when I click on send it should delete that particular object or the product from the from the products okay so now it says method not allowed that means we have not done the mapping yet so let's do the mapping so here I will just create a method so public void or maybe if you want to return something you can do that or you can return the status but at this point I'm just trying to keep it simple so I will say delete product and here instead of accepting an object we have to accept the value the same thing we have done here right so we are accepting a particular value and we can do that with the help of path variable so let's do that here so here I can say I want to store that in a value called product ID but we have to use an annotation called path variable to accept the value and then the mapping should be okay what do you think what should be the mapping the mapping is delete mapping uh we have to specify the URL which is products slash and then we have to pass the number right so we have to accept that with the help of this cly brackets and we have done that you just have to make sure that this name is similar to this otherwise you have to mention in the bracket what new name you're using since it is same we don't have to do do that okay next how do I delete it I don't want to delete that from a controller controller will send send a request to the service service will say okay it's my job to delete but I don't have this method so we'll create that method so I will say delete product accept the product ID and delete it but unfortunately we don't have this method in the service so I will click here and say create a method in service now how do you delete so we got the prod ID but we are not sure the index number because if you want to delete from the list which is products dot if you say delete or if you say okay what's the method for deleting remove so we can say remove and we have to pass the index number okay but I don't have the index number so how do we get the index number we can use the same logic or maybe we can create a common method where you can pass the product it will give you the ID right or I can just use the same code multiple times not a good idea okay this is not good I'm repeating the code so basically uh I'm not following the drive principle if I'm if I'm correct do not repeat yourself I'm repeating myself but you got the point right so you can basically create a method which will accept the object or the ID and it will return something but in this case I'm not getting the object I'm getting the ID so first we have to get the ID so from this product ID I just have to match with this so instead of saying this I have to say product ID and here I can pass the index okay so what I'm doing is I got the product ID I'm running a loop to check if the product ID is matching with any of the product ID if it is matching then remove the index see one of the problem I was talking about here as well is what if the ID which you are passing is not found in that case what it you know what it will do it will delete delete the first element uh technically you should return by saying that the item not found but at this point I just want to keep it simple and whatever idea you mentioning I'm expecting this should it is there uh so it will work but again not a good code not a perfect code but here we are not we are not here to work with the logic we here to understand how spring boot Works how spring MVC Works how the layer works right logic is not that important at this point just to keep it simple but you got to point I'm showing you the problems as well you can solve it and if you have done that uh let me know in the comments what solution you have used okay so this looks good uh I'm not sure if this works and that's how the development life is right you're not sure what code will work and what will not work so you run it and say it so let me go to my Postman first of all let's get all the products you can see we got three products and now I will send a delete request send no problem we got the status code AS 200 and now let's send the get request once again to see how many dat we have we only have two now you can see one2 is gone so that's how basically we achiev the C operations and that's what we have done right so we have done with the get request initially we have talked about the post as well now we have done with the put and delete so that's how we achieve with the c operation using springboard now in this video we'll focus on Spring data jpa so why do we need it see let's go back to the project which we have which we are doing and we have done a lot of things here and just to reiterate basically we have uh we are building this application this is a general architecture where you have a client in this case we are using a client as Postman so that's our client right now we can either use a mobile application to interact with the server we can either use a client like your react application or we can use a API tool called Postman or any other tool using which you can send data to the server and you can get the response and we have done that right the request goes to the front controller then from that it goes to the controllers and we do have our controller D so if you see we have a product controller which has multiple mapping happening and then this controller if you want to do some work any logical work that will be forwarded the request will go to the service now it is the job of a service to process any data or if you want to calculate something if you want to do some processing it everything will be done in the service layer and service layer is expert in this things so when I say expert basically you have to code it right so that's the job of a service layer and then what if you want to work with database now at this point we have not worked with database you can see the data which we have we have basically hardcoded it in the service layer not a good idea don't do that we want to store this data in the database right so of course we have to use some database it can be H2 it can be post it can can be Mas your your choice doesn't matter we can even switch this dbms when we want to do that but let's say I want to work with H2 here right so when I have all these layers I mean when I when I have a service layer controller layer they're doing their job and now I want to talk to the database now that's where we have to work with the repository layer this R here is your repository layer now the job of repository is to connect with the DAT database okay so let's say if a client says hey you know I want all the products so request goes to the controller controller says Okay I want all the products it will ask this service now till this point we are hard coding it right but now what I want is I want this data to be coming from database and to do that we have to do the coding in the repository layer so this is where we have to do all the coding now if you go back to the old days the way you work with repository layer is this basically we have to use something called J DBC which stands for Java database connectivity Now using this jdbc we can basically connect the Java code with the help of database with with the database right and to do that we have to write seven steps now it goes in multiple ways basically you have to uh load the driver we have to connect to the database or dbms then you have to basically uh create a query because when you talk about dbms it we're talking about databases here we have to use something called SQL so we have to create the query we have to execute the query then it will give you some results which you have to process and then that's how you basically work right you have to follow seven steps and all this code I know you have to add leny code this will be done in the repository layer so basically this is where you have to do all those things now things got bit simpler with the help of uh spring jdbc now there's something called spring jdbc inside spring framework you can use this and it will help you to uh reduce the number of lines you have it will make it more standard to reduce the bugs and it's good so instead of using normal jdbc we can use spring jdbc it will help you with the uh jdbc template it's good but what if you want to make it more easy see ultimately if you want to just send data from your uh service layer to database layer because what we are doing in this application is just mostly crud right so if you just want to use crud which is create read update delete uh we can use some easy way to do that and that something can be done with om so there's a concept of o RM which stands for object relational mapping okay so what we are doing is we have objects so in the Java world everything is object right so if you talk about a laptop if you talk about anything anything you want to represent even humans in the Java world are objects bad thing but yeah so everything in the Java world is object because it's an objectoriented programming so if I want to represent this remote that's a object if I want to represent maybe this bottle that's an object right everything is an object and the way you create object is with the help of class so first thing you do is you create a class which is a blueprint and then from that class you create the object if that makes sense now this object will have two things the object will have the data uh so the object will have the data and the object will have the behavior with the help of methods so in this case we don't want to talk about methods let's focus only on the data now when you say data what data we have so basically we have U example let talk let's talk about this product here now if you go back to any particular product which we have it here a product will have the product ID a product will have a product name it will have the price right this is a data I'm talking about so every object will have a data so in this case we got uh product ID we got product name and we got price right so we got these three things this is the data I'm talking about this is the Java world in the database world what we do is we have a table right we have a table structure now this table will have a name so table name uh then this table will have the number of columns and they will have a column names right and then they have data as well so each data will be one row so this is the rdbms world this is the object world now what we want to do is we want to basically connect them we want to map them that is object so this is object these are called relations and we have to basically map them and if you can do that that's om now how do we map it it's very very simple right when we talk about object object has data as I mentioned before so just an example the object will have data so example we got the ID I will say p ID and the ID we have is 101 then let's say we have a name for the product let say p name which is iPhone in this case and then we got a price and the price is 50,000 so if you can see this data is stored in that particular object right here is the object I want to store this data in the table of course you can write WR the queries if you are familiar with SQL uh we have something called insert query so you say insert into product table where not where but values are this 101 iPhone 5,000 you can do that but what if you don't want to write the queries I mean think about this you just have to learn Java you don't need to learn any other other language I'm talking about SQL here so can we do that so yes that's where we can use om who simply says hey you know give me the object it's my responsibility to store that database so it will basically convert your or not convert but translate your object into SQL query and then you can save that in database so your object data here becomes one row so one1 goes here the P name iPhone goes here and your price goes here so that's your one row if you have multiple objects you will get multiple rows but the question Still Remains who is responsible to create a table who is responsible to specify the number of columns who is responsible to name the columns are we going to do it what if I say you don't have to do it someone else will take care of it and who is that someone else of course the orm tool so basically we have to use some tool to make it work now that tool is it a magician to know everything see the thing is the table name can be we can get the table name from the class name right we have a class name product so we if you can see we do have a class called Product right so we can get the class name as your table name what about the columns each variable becomes a column we got the Colum as product ID we got the column as product name we got the column as price so the whatever variables you have you make them the T the column names and what about the rows each object is one row so if you have 10 objects of 10 different products you will get 10 rows that's how basically you do the mapping and who is responsible to do all those things for you is the orm tool examples we got hibernate is a very famous one uh we got eclipse link uh we also got my Bettis my bet is not fully urm but yeah you can use it uh but the most famous one is hibernate it's full-fledged uh om tool and you will find everything there so this is a good one good tool to use so we got hibernate but if you see the title of the video it talks about jpa not hibernate right so why see the thing is hibernate is a good tool but let's say in future you want to move from hibernate to some other tool you basically have to do lot of changes to your code is because all these tools don't follow the same standard or maybe they are but how they following the same standard so what we got is we got something called jpa which stands for Java persistence API so jpa are just standards and hibernate basically implements those standards Eclipse links basically implements those standards right so all the most of the autm tools Implement jpa standards so in future if you want to move from hibernate to Eclipse link it will be easier imagine if you if you know how to drive one car it is easier to drive other car is because they follow the same standards right that's why we have something called jpa now in the world of spring now spring since is a big project it's got a lot of modules inside it they got a special tool or special module called Spring data now when you talk about spring data it deals with data but we want to specifically mention I want to use jpa so the module name becomes spring data jpa and when we use it the storing of the data in the database becomes very very easy okay you will see that in the upcoming videos but I just wanted to give you the work through what are we going to do and why jpa is so important so with the help of spin jpa we can store this data which we are doing in this service layer in the database which D which database we are going to use we are going to use H2 which is uh in database easy to set up easy to use and for learning purpose it's a good tool so we are going to use H2 and we are going to use sp. jpa to store this data in the database and to fetch data it will be fun and easy to work with so now let's Implement spring data jpa in this project so the ultimate aim is to basically store this data in the database and also fetch from it now if you can see we do have a controller for all the different uh requests we got get uh post put and delete basically the request goes to service and then from service we have done basically we have learn the hard coding of the values and that's what we are getting right now we want to work with database so what are the things we need the first thing we need is a database so basically if you see we have talked about different layers we want to work with database so we have to choose a database now and we are going for H2 as I mentioned before uh next to connect database with our spring application we need uh the connectors as well but once you get uh so yeah we need a connector as well so we'll get that so we need a connector between these two uh so that's that is basic basically your hed2 driver so we need this also we have to create a layer repository layer which will make it work so again let's try to do that in this video or we'll divide into two parts let's see how it goes so the first thing is you need to get H2 and the driver also the spring data jpa capability now how you're going to add that see if you want to add a basic feature in your project or uh any external libraries you have to do that with the help of pom.xml file so if you open this what we got till now is we got web and that's it that's a main thing we have of course we have Dev tool as well but that is optional apart from this web we need to get the data jpa and H2 now when you create this project for the first time you know if you remember how we get created this project so I will open the Chrome and we'll go to start. spring.io uh remember when we created this project we have to we have to specify some values here and then we have to add the dependency if you don't do that let's see what dependency you get by default so I will click on explore and I will increase the size a bit so this is okay this is griddle I want the mavin so I will say explore okay now you can see by default you only get the spring boot starter not even web so for that you have to add web so what you do is you go to add dependency and you say spring web that's the first thing you add and once you add spring web you will get spring starter web so you get this but what if you want to work with jpa in that case you click here and search for jpa so you get an option of spring data jpa you can click on this and now you got two so I will click on explore and if you go down uh yeah so we got spring data JP this or something we need apart from this I also need H2 so we got H2 database driver click on this and it will give you this driver if you click on explore U not just driver it will also give you a time so basically it will give you the H2 database see H2 is a in database right so if you work with some other databases let's say postgress msql you need to basically install them on your machine then do some configuration it's a good thing because in the real life project we do that but since we are into learning phase H2 works for us it's a inm database and you don't have to do a lot of setup you just add the gpy and your work is done it will give you some default settings just use it and make it work okay so we don't have these things in our project right so one thing you can do you can again go to start. spring.io and copy this dependency so you can copy this and you can copy this uh that's one way or instead of going for start. spring.io we can search for MAV repository and here you can search for it so I'm searching for spring data jpa I will click on this that's the first one we got and then we have to choose a version so which version to choose here now you have to choose a version which you are already using in your project for web so I will go back to my project and in fact you don't have to mention the version because we have mentioned the version here as a parent so you can it will simply use this so you can mention the uh driver or you can mention the dependency without the version so I can pick up two 3.2.6 and copy this looks like this is not the one we are looking for okay that's that's one of the confusion we have uh if you see we have two options spring. a jpa and spring boots start a jpa now since we are working with spring boot project we have to use this not that one and again I will select 3.2.6 uh yeah this looks good so I was looking for this boot word uh this is important so I will just copy this go back to the project and paste it here and of course I can get rid of this particular version number and reload the mavin changes okay this is done the next thing you need is the hed2 so I will just go back here and you have to get for hed2 uh again the same website I'll search for hed2 click here and which version so you can see there are lot of verities you can go with this one or maybe latest one also looks good I will click on this uh I just need to verify if I'm getting the right one so yeah so just com. hed2 database H2 artifact okay I'll get this and paste it here so these are two things we need so I will just remove this and now by doing this of course you have to reload the mavin changes by doing this you are basically making sure that in your project now you have dat APA and also you got H2 how do you verify this what I will do is I will start the project to see if certain things are working out otherwise we have to make some changes okay so we got the eror it says uh failed because we have not specified the URL see whenever you add any driver we have to make sure not just driver whenever you add spring data jpa basically it will ask you for the URL for the uh database see when you con database we have to specify certain things uh it's not just for H2 any dbms you have to mention the jdbc steps where you have to specify the connection URL so that's something it is it says it is missing so where should we put the URL so that should be done in the application properties so let's go down so in order to do that you have to say spring. data source. URL and you can mention the URL here so what's the URL so is DBC colon then you have to mention the database name if it is MySQL seml postgress postgress but here saying H2 then in H2 we have to mention it is a inmemory database M uh we can also get a full storage it is also option permanent storage option is there but you're going for in memory so we will say ma' and then whatever your database name is so I will say test DP or maybe I can say teliscope that's our choice we can mention whatever database name we have so that's done right now by doing this let's start the application because that's what it is it was looking for and I hope it will work okay we got another error it's not able to find the driver uh should I remove this version I remove the version let's also mention the driver name I thought you would pick up let's drip it so we'll say spring. data source. driver class name certain things you have to mention for the properties so it's org. H2 do driver I hope this will work now so basically we do this in the DBC as well so I have to mention uh the URL you have to mention the driver name also username password but I want to go for default one if if it is not accepting we'll type it we don't have a choice so let's restart the application so let's learn from the errors okay more errors okay looks like I made a mistake while mentioning the driver name it looks like it's not o it should be okay just com dot it is there cannot load driver let me check the dependency once okay looks like I've got it it should be runtime because if you see when we were trying to do this it says runtime in the dependency I got it test so I forgot to change that the scope let's see if this works now after making those changes um so basically you have to make sure that your H2 is there available in the run time okay looks like there's no error yeah there's no error and the toet is running so yeah so sometime it's good to get errors you learn certain things there and now what we have is we have the edge to ready we don't have data in there yet but the database there but how do I access that database so since we we are not installed in the system one way to access it is using the URL so I say Local Host colon 8 890 because that's a port number we using slash H2 console so to access the database we have to use a H2 console enter it will give you this page and you can see there are certain things which are mentioned here so we have the driver name which is this the URL we have the database name name as theis I will use that uh the username is by default essay I've have not specified that in the properties the password is blank test connection it says successful I will click on connect and that's our database nothing is there because we have not done anything yet but at least we have the jpa uh connected and we got hed2 connected and now it's time to create that layer the reposit layer so now we have our ed2 ready you can see we have the we have the entire console here but if you see we don't have the tables which we want so it got some tables but not something which we want so if you expand this nothing is there for us what table I'm talking about if you remember the theory we have talked about the table because we have a class called Product and I want a table which will have this data so the table name should be product the column should be the variables of the product and the data will be of course rows so how do I get that how we create this table and for this we have to do some changes in the code the first thing you will do is we'll make the changes in the uh basically we'll create a repository layer see service is not responsible to work with data it is responsibility of the repository so what I will do is I will create another package here where you will have all the repositories so I will just right click here and say new uh package and we'll call this as repository or we can also say repo that work box now in this package you will have all your classes interfaces for the repository now the question arise what kind of file we are going to create is it a class or the interface see logically it should be class right because we have to write a lot of code uh to work with database for fing the query and having different methods for different actions but what if I say we can do that with the help of interface and you'll be saying okay we have talked about that in the in the theory uh spring. jpa will help you in that definitions and yes so what you can do is uh you can simply create the interface here I would say class but we'll select interface here and we'll name this as product repo okay now simple interface nothing fancy now in this interface we are going to define the methods oh we can't Define methods in the interface we can using default but we have this interface and we can declare methods But ultimately it has to be defined somewhere but jpa will take care of it or sping JP will take care of it we don't have to worry about those definitions but what should be the method names let's say I'm not even mentioning the method names let's keep it blank but one thing I will do I will just say add repository here remember when we talked about uh controller we have on top of that we have ADD controller on service we have add service repository layer we'll have ADD repository it gives some extra features to it but since I want Sprinter a jpa to take care of everything what I will do is I will extend a class called jpa Repository in which you will mention the primary key so basically you have to mention two things uh first the class name which we are working with and second the primary key type which is the integer so just these two things and our job is done okay what are the methods let's skip it let's say if you don't enter it let's see what happens now that's one thing we have done with the repository next in the service I'm going to use this repository so I will say come back here and we got the product repper let's create the reper as a reference variable and let's say wir so that we'll get this object first of all we don't have a class right so spring is responsible to create the object of this class or this interface so basically someone has to create the class of it and then spring will create the object of it and who is that someone who will create a class for it it is your spring data jpa I'll go back here and now since I don't want this I will just commend this part we don't we we don't need those things and when you say get products who is responsible to give you the products let's see I will say repo dot the moment I say dot can you see that we have so many methods now mind you we are using that repo variable which is the type of product repo and in this interface we don't have any method s still when you say dot it is giving you a lot of options question arise from where you're getting all these methods if you go back to repo we are extending with jpa repository now this particular interface has all the methods which are which we are getting there right uh if not in this if you see we have also it also extends least uh C repository this has some more methods uh if you go to C repository this has some more methods if you go to repository this has nothing but yeah but you got the point right so we we getting all those methods because of this jpa repository which you have mentioned here so let's go back here and let's see which method will help us so find all looks like a method which will give you least of products and that's what we want if you can see I say find all okay next uh we to replace this and as I mentioned before right we don't need to write this all this lengthy code once we start using jpa uh we want a product based on a ID so I will say repo do find uh all okay not all I want one so I will say find by ID and pass the ID here but it is giving you an error it is it it returns an optional so I will say or else return me a blank object that works for me next we have to add a product so of course we don't have that now so again I will use the repo to add the product now which method we are going to use so if you can see if you scroll down some method should be there which looks good yeah save looks good right so I will use Save and pass the object no errors we're happy update lengthy code right let's remove everything repo dot now the thing is we don't have any method called update if you see if I click on if you type I update there's nothing the only method you can use is save for both for saving as as well as updating we use save so what this will do is it will check if the data is there if the data is there it will update if it is not there it will create as simple as that delete no lendy code just remove this and we can say repo do delete delete by ID because we have to pass an ID so I'll say PR ID and our job is done it's so simple right look at the code here in the service we have we don't have any hardcoded values everything is coming from the database looks good okay uh is it done yet let's try to run this and let's see what errors you get run this and okay what do you think we will get the error or not oh we got the error let's see what the err is it says not a managed type which one product is it if you go back to the product this is a component but still it says not managed why because if you see the error is because of the hibernate see under under the hood it is implemented with of hypernet it follows GPA standards so if you want to have a class of which you want to create a table we have to use one more annotation called entity okay so this is The annotation we have to use now it should be happy with it but I think we'll get another error let's see and we C it this time it says okay not the same error you can see it says entity product looks good has no identifier oh that's weird see the problem is every table need to have a primary key in this case we know it should be product ID but we have not specified it so you have to mention this as ID so you have to mention hey this is a primary key okay so Pro is a primary key Pro ID let's rerun this I hope there's no problem let's see let's see let's see okay no problem if you scroll here Tom Cat started to test it I will just refresh this and say connect once again okay but if this time if you see we got a product table this is what we want right let's run the query so I will say select uh star from product and as of now it is empty we want to add some data how will we do that of course we can run the insert query here or we can go back to our Postman remember Postman yeah it's here so first of all let's get all the products and let's see what we get so we got empty because we don't have any products and table it is empty let's add some data so I will say post uh we do have a body here which is this I will I will start with 101 now send okay it says okay there should be one product now how do we test it let's go back to console run hey it's working we got database connectivity it is getting stored in database let's store one more I will say two this time I will store a water bottle and let's say 200 click on send it works let's try to fetch all the data and this data is coming from database as you can see let's also verify in database and it's working but hold on we have not implemented any of these methods where is the query how you getting all these queries if you can see this is empty and we have not even created a class and that's the magic of spring jpa uh which is creating a class for you behind the scene but maybe you want to see the queries here we getting the products but what queries it is firing behind the scene if you want to know that you can add one more property in the application properties so we can say spring dot data source. show SQL equal to true I hope this is a property name if I'm not wrong let's relaunch it the problem is every time you relaunch you will lose the data because you're using in database let's see I will say get we have not got any data and we have not even got the query maybe the property name is different is it spring. jpa do SQL okay yeah it is show SQL if you can see U it is actually creating a table for us right so this is the quy I'm talking about so for every query now so even if you say let's say if you want to say get all the products and see the query here it says select query what if you want to add a product so I click on send we have added the product and look at the query it is insert query so that's how basically it is working behind the scene we can see the query as well what if you want to change the username password of your hed2 we can do that so I will say spring. datas source. username and let's say the username is naven and spring for the password I will say spring. datasource do password I will say the password is teliscope now after making those changes just restart just to secure your hed2 console okay it started and if I want to access the H2 console now if I use a default username password if I click on test connection it failed so we have to use naven and telesco as a password connect it worked so that's how basically we can change the username password and we can do all the setup for H2 so I hope this makes sense uh there are certain more things in h in Spring jpa maybe if you want to customize this methods uh we got default methods but what if you want to have some customized methods uh let's try to achieve that once we do the project itself you know we can do those changes now till this point we have talked about what is spring framework we have talked about spring web spring boot now it's time to focus on the project remember in the first or second video we have talked about the project which we are going to build now to build this project of course we have two sides of it so if you see this diagram here or this drawing kind of so on the left hand side we have this thing right which is your client a client can be react application a client can also be a post man to test your apis or maybe can we can use squag a Swagger but when you talk about client here which we are using we will be building a react application and of course I don't want to code the entire stuff we have a code let's understand the code first uh what we have already done so this is actually ready for the first video this is something we want to build of course right we want a database we want to have different layers and when you want to get something from data database we have to build all these layers if you even if you want to store it now of course we'll do that step by step but what we have ready is this react so in this video Let's understand only what the front end should look like and uh we already have this things and this is how it was looking in the first or second video but now since we want to build the back end as well since we don't have back end now we'll build it but if you run this front end without the back end it will give you some issues I'm already running it but what I will do is I will stop it now and let's read do again so basically this is your front end project which we opening in vs code and if you see we have this this is a simple react application with the help of weight um and if you open the package.json these are the things which are needed there nothing different in a simple react application what are things we need those things are here and let's head back to the flow now people who are very new to the world of react of course this is not a react session but just want to give you some intro uh basically if you want to build a front end in the earlier days we used to have in with Java basically we used to use gsps or theme leaf or maybe simple HTML CSS but then nowadays what we do is we try to separate these two and we try to connect them with the help of apis so that the front end can change it can be uh for mobile we can have mobile applications for browsers we can use react angular vue.js there are multiple options maybe in every six months you get new framework there and then we also have uh API clients to test it so the back end is p back end there's no front end code there right now if you talk about react why react got so f is because it focus on the component building so whenever we talk about any any application we can build the application but then there are lot of moving Parts if you change one thing it might affect other and maybe you are reusing the component but basically you're not reusing it let's say you have something on one page and then the same thing is there on the other page you have to basically rewrite it and let's say after after few days you want to change it so you will change in one part and you will it will not reflect on the other part so what if you can build components which you can use it anywhere so it's like building components like Legos and then use it where you want so that's the basic idea and this component will have its own layout they have their own functionality so you build the components and just use it and the Beautiful Thing is uh basically you just build one page it's not like you're building multiple pages so we have this thing right which is Spa which is single page application basically what you do is you create one page and you have everything there every time you click a button or there uh it the data changes in the same page so basically you don't see that your page is reloading because you're just changing the data of the page so the way you can do that is you can build components so we have this common page which is index.html uh that's we also have that here so if you see we have index. HTML and throughout the website you will see only this page which is index.html and what is there in this page is whatever given by your root okay so we have this root div and this thing will keep changing okay why we got this okay so my co-pilot is running know that disable Okay so I'm disabling my co- pilet completion okay so we got this rout and then the way you can change this data is with the help of main. jsx now jss is a new thing uh for normal HTML CS designers or developers we got jsx where you can have your own tags now when I say your own tags uh it's not like you can have any tag basically you can have tags which you can Define and use it so I'll will show you in sometime what I'm talking about so basically this is a new format now this file is responsible a Javascript file you can say this file is responsible to load the data right in this page now what this file is doing so if you go to main. jsx it says this basically will be filled with the help of app now look at this this is a tag I'm talking about now this is not a simple HTML tag that we don't have tag called app uh so basically these are the custom custom tags which we use and if you can see all the react tags starts with capital letters capital A here for react we got capital r here but what is this app now if you see here if you know JavaScript a bit it says import app from app.jsx so basically this file is used again I'm talking in terms of Java developers we're not into JavaScript so someone is watching from JavaScript where they're like hey what are you talking about I'm in a perspective of someone who is new to the world of react and JavaScript okay so I'm going moving to app.jsx now this is where the actual fun happens uh so in the JavaScript world everything happened with the help of functions right so we have we got a function here and the name of the function is app now whenever you want to return a component your component is something which you return to a page right so this is where you are returning it so this is your component which you're returning now in this basically you just have routes for homepage and for add product now if you remember the image or the page which was running we got different buttons here one of them is home and one is uh add product when you request for the slash page it it will return you the home P page when you click C on ADD product that's a URL then you will get add product but what is this home again for this home this is a component so I'm jumping on the component section a separate component section and if you click on home now this is responsible to fill the homepage how it is doing that see in the homepage you'll be having different products right there'll be list of products and to do that of course in the UI you're not storing data data will be coming from the server of course you got layout uh you got color scheme everything in the UI but data is coming from the server how will you request data and that's where you got this URL which is Local Host colon 8/ API product when you hit this URL you will get data and that data you will basically set it in here so if you see when you get that data we are calling this method called set products okay I'm I'm looking at this code with you okay so ignore that yeah so set product is here basically this is a use state where when you call this method it will change the value for this products here and with those products once you got these products you will run a loop so you can see we have we have a map function basically so it will iterate between each element and it will basically show that on the page so what we are showing we are okay so we are showing the name then we are showing the brand and with lot of CSS in between we are say we are showing the price now this is what we want to show on the page and of course you also have a button all add to card okay but then if you run this so let's run this we are not talking about add product now because in this video Let's only understand the homepage now how do we run this so once you you will find the link for this project in the description so what you do is to run this you open this into your vs code or any different ID and go to terminal on the same path so you can see this is Ecom frontend one for the first video to run this you will say npm install you will say npm I you can also say install but I works I will say enter so this will basically download the packages which you mention in the package.json and once you have it you can basically run this so you can say npm run Dev and of course all these things are actually mentioned in the package.json so if you can see uh in the script we got Dev so basically this is the thing we have to use so npm run Dev enter and it started so your front end is working on this particular port number so I will click on this and this is what you got it says something went wrong you know what went wrong the back end is not there okay how do we check that so it's very simple you can also use this URL which you have used here and you can just paste this in the browser and you can see it says this site cannot be reached or if you write this in Postman it will say 44 or the Ser server is not there so we want to make sure that the back end is ready how will you do that of course it's a very simple thing you go to your spring uiser and create the project so in this video we just understood the front end what the code is and of course with every new different video the front end might change I will let you know if if I'm using a new front end and that link you can use it in the or you can get it from the description so now it's time to build the back end for this application so we got the front end ready and we have talked about it in the last session or in the last video but now let's build a back end so I don't want this MCH message which says something went wrong let's make it work okay so when you say backend how do we do it it's very simple we have done this before we have to create a springboard web project a new project we don't want to make changes in the older one so I will say mavin project Java the version remains same the group ID is as usual com. Isco the project name the artifact ID uh I will want to make it as Ecom project I will say one uh okay let's say com project we'll make keep making changes in the same project and the jar packaging the Java version 21 now it's time for the dependencies now since this is a web spring web project I will need web I don't want to create a ref repository here the way we have done in one of the video is because I want some customization so I will prefer web uh the next thing is I want database connect as well so I will say jpa is needed uh next I will need uh Dev tools for the fast reload and longbo also let's try longbo something went wrong last time when we tried it if it doesn't work we'll remove it uh and okay what else we need uh H2 database is also required right so I will say H2 of course you can use a database like Po or masle that's your choice I'm using H2 here which is lightweight for our use so we got spring web we got data jpa we got Dev tool and lombok and2 only these five dependencies with this I will click on generate it will give me the project and you can see we got the project here so I will just unzip it and open that in intelly idea okay so this is the project which we have to open this is what we have downloaded and now I'm opening this in intell idea Community version so I will click on open and uh it's here okay so basically this is your blank project of course nothing is there and the port number which is by default port number is 880 and even before making any changes I want to check if that Port is empty so that I can use it uh so what I will do is I will just open this main file nothing changes right simple project U it might not work is because in H2 we have not specified data source but let's see if without data source does it work so run this and okay it's running great and it's running on port number 8080 this is what I wanted so we are good uh port number 880 is working and now we can make the changes now what are the changes I want to make first of all of course I want uh database so I will just make sure that I have the properties for database uh so the first thing we have to set is the URL so I will say spring. data source do URL is equal to jdbc colon we using H2 col M colon uh then we'll mention the database name so I I will still go with telis database uh next I want to specify is the driver class name so I would say spring. data source and we have done this before so we can just directly use it driver class name it is org. H2 do driver perfect now what are whatever the things happens uh with the jpa I want to see the query as well so I'll say spring. jpa do show SQL I'll make this true apart from this uh there's one more thing right so who is responsible to create a table in the database so basically in the jpa world the jpa takes care of creating a table so there are multiple options there when you say jpa should create a table uh we have an option of create which will create every time run this or it will update so if the table is already there it will simply update it in the terms of H2 we don't need to set that but if you are using post or Mas which is per database uh we need to set it to update so I'm doing it so that if you want use it for post or MySQL you can use that so I will say spring. jpa do hybernate doddl auto let's keep it to update so that it will not create every time you restart the application so I think we are good uh everything looks good these are the properties we have to mention now once you have your properties ready what's the next thing you will do so of course now we have to create the controller the service layer and the repository layer right so we'll do that step by step but there's one important thing the model how will you represent your data which is a product so basically now we need to represent how your product will look like so what exactly when you say we have a e-commerce website we have multiple products what are the details we need in a product of course there are multiple properties will be there one of them is image but let's not use image now because it's a complex topic so in the upcoming videos we'll see how do we work with images as well but at this point just to keep it simple the things which I need is ID so the first thing is ID which is also your primary key for the table uh then we'll name a name of product so we'll need a name of the product uh I will also need a description for a for a particular product a long description we we don't want to show that on the homepage or the first page but let's say if you see if you want to see about a particular product in that case I want to see the long description uh so that should be here so this two things will be string this is also going to be same string uh then uh I need the brand of the product let's say if we talk about a car so let's say if I I drive espresso which is Suzuki so that's the brand then also the most important thing is price every product needs a price there so this will be integer so brand will be string but this will make it integer then also uh we might want to categorize the products right based on is it a toy is it a car is it a lap I mean electronic device so in that case I want a category as well and on which day it got released so I don't want to do anything with the release date but let's say in future if you want to sort by which product came first and which is the latest one you can do that with this now for this we are going to use date uh bit tricky here is because by default the date format will be uh us format we want to use the Indian format which is ddmm y y y uh so we'll see how do we do that and also if the product is available of course lot of properties but you have to just Define them once so availability which will be Boolean if it is true or false and the last one is quantity how much how many how much quantity we have that product in our stock right so this is something we'll keep as in category will be string so these are the properties we'll need apart from this we'll also need image but at this point I don't want to put image and we put putting question mark there but we'll work with image data so we'll update our product class and we'll add image there as well but at this point I want all these properties so let's create that so the way you can do that is the first thing you need different packages so I need a model package then let's get all the packages here just making our project set up properly we need the controller oh I created a class so I need a controller package then I need the service package then I need a repo package okay uh again for repo you can say it's a dow or you can say repo repository you can use different names there I'm using repo here and in the model basically I have to create a product so I will say in fact before I do that I want to make sure one more thing if I'm getting the output on the page so what I will do is I will create a simple controller here so I will create a class and I will call this as a product controller simple stuff and on this controller I will say this is rest controller and request mapping by default every request should be passing from API so the URL will be Local Host col 8/ API SL whatever you mention in the methods so now now I want a simple method which will greet me so I will say I want someone to greet me right so I will say greet return hello world now when this will be called so whenever there's a request for home so say request mapping this is for the home request okay this is what I want to return if this works then we'll continue right otherwise if this is not working then we have to uh debug what is going wrong so let's do this step first so it's running the project and how do I check it of course I will I can do that from the uh Postman or let's use a browser so I will hit this URL but without products I just want API enter and we got hello world so this is working right at least our spinning application returning something but I don't want Hello World of course when I say products if it is products there it should return the products which is not there yet so I will get the controller data but what what I want to do now is create the model let's complete that first so I will say class I want up what went wrong uh I will say new class product now in this product I want all those properties right and I also want to use a longbox so I will be using that so I'll say private in ID private string name what else we have description so private string description private string brand we also need price private in price then we got categories so I will say private string category uh then we got release date so I will say private in fact price should not be int it should be big decimal because the price can be any big amount right uh so let's keep it big decimal and here we'll say date uh release date we have to import the date so I'll say control space from util yes and big decimal came from math okay release date then what I want I want available and quantity so I will say private in quantity okay so so many properties right and I don't want to create geta St for this so I'm I'm going to use lombok so on top of it I will say data and I also want all arguments Constructor and no arguments Constructor that's it so this is basically the product which you got now will this work of course not uh if you want to get the table one thing you have to do is you have to create this as entity because jpn needs entity and on top of ID we have to say atate ID right that's your primary key cool looks good right but will this work U of course you cannot send request from the client is because the control is not ready we don't have products controller uh but at least I want to see the table in the H2 database so I will just refresh this to see if something happens okay there's no error and if you can see it is also Al creating a table so it says T create table product and you got available uh brand category description so it goes in alphabetical order and that's why if you can see the sence is not matching we got first uh the primary key which is the ID then it starts with this uh alphabetical one a b c d n p uh then quantity release date and then primary key which is the ID okay so uh these are the things we need and how do you verify this it's very simple you can just go to your console H2 console so you can say Local Host 8/ H2 console and we have not set the username password so default will be sa and no password click on connect and you can see we got a product table which has all these values of course data is not there but how we are going to load the data let's see that in the next video so basically in this video we just make sure that we have our structure ready for the project which is ready here and uh the database is empty We'll add the default data some data to start with and yeah looks good so now it's time to focus on the controller because now we want to return the products right now first of all we don't have products in the table but let's see what happens when you don't have a data in the table and still you when when you try to fetch it of course will get blank but let's do that so let's go to the product controller and here we want to create a method which will return you the list of products and the URL would be/ products we have tried that before right so if you go back to your browser and here we are asking for products it is giving you 44 we don't want this so I will go back here and let me say create a method which will return the list of products because we don't have one product we'll be having list of products so list of products get all products of course you can have any method name doesn't matter but still let's give it proper and we have to also import this okay let's set the URL so we are going for the get mapping because we are going for the get method so get mapping and the URL will be products so the actual URL will be API SL products so we are seeing this for the first time in this particular the last video and this video but we are using the request mapping on top of the controller so basically whenever you talk about this controller this controller follows / API SL products okay so now let's return the products now but who will do that of course we don't want your controller to talk to your database directly right so we need some layers in between so I want my service layer so I will have a service object here and I will say hey get me all products but unfortunately we don't even have this object we don't have a service object so let's do that quickly because now we know how do we create those layers how what annotation we use so I will go back here in the service package I will say new Java class and this will be product service and this product service will have an annotation called service now since we have this as a service I can create a reference here by saying uh private product service and I will say service of course I can use field injection here which is on top of the variable or you can use a Constructor or you can use Setter to do that I will use fill injection not a good idea but it looks it keeps your code smaller I would encourage you to use uh this Constructor injection so create a Constructor for controller or a seter in the controller and then implement it okay so now uh we got this autowire so I can simply use this but I don't have this method called get all products in the in the service so I will ask my ID hey you know I want this method in the product service it will say okay we do that but give me the code but again I don't want service to talk to database and I don't even want to return the hardcoded values so this is where you need to uh basically create object of the repo we don't have it so in the repo let me create uh interface class interface we have talked about this before so basically we have to get the interface and here we'll say product repo repository whatever name you want to give and we got it and this will basically extends jpa repository which will take two two uh types one is a type of class you're working with or type of entity you're working with and next is your primary key the primary key is of type integer so let's stick to it and now let's have the methods in fact we don't need to add methods because we have that from JP repository right we can simply use those methods in the service but we need to use an annotation called repository again I'm going bit fast because we have talked about this in the previous sections so let's talk about product service here and now in this let's create the object object for product repo I will say repo and on top of this we have to say autowired okay now once you got your repo I can simply call the methods right so I can say hey repo uh you will be having some methods to give me all the products so it has it has find all uh and then this will return you the Leist of products so I have to say return the least of products so simple right uh so database says I'm done we have a database ready service layer says okay I will get it from the repo repo says okay I will get it from database controller says okay I will get it from service and it will return the products to the user that's what we expecting right and expectation is not a bad thing so let's try let's try let's see what goes wrong and what goes right if something goes wrong we'll correct it so let's get back to the browser and here let's say refresh it worked okay we got a empty array reason is because we the table the database itself is empty how do we get uh it solved okay so what we can do is we can add some data in database of course we have multiple ways of doing that one you can basically add the product from the from the page here there should be add button uh but I can't see that I have this buttons here nothing is working but let me just refresh this still not working okay there's something still broken we'll solve this we'll solve this problem uh but then we want some data when you actually hit this URL I want to see that here so that I can use it in my front end in fact I also want to check this in the Postman so I click on send uh it says not found because I have to hit the API if I click on send it says method not allowed okay we are using post my bad I will say get and we got empty okay so we want to have some data in database how we are going to do that so multiple ways are there as I mentioned you can have a button in your page or you can go to S2 console and from here you can insert some statement but there's one problem you know if you insert the queries from here from your H2 okay I will show you what I will do is I do have the query ready with me okay and this is some random data which I have about cars so I will just use that query here so I can you can see I can I'm adding different cars here again all this data is given by my team so I'm I have not actually read what they're talking about cars what is their perspective towards car uh but yeah so we got this we got ton xon uh Swift crer thir so we have different cars here so I want to add this data data so when I click on run of course you will have this data in database but we got an error it says description not found okay so basically we are using uh DEC and my team is using description so let me just use that okay so now let's run this and okay it says product available okay there a lot of changes in the query so we'll say available also there should be issue with the quantity because I'm using quantity and click on run okay still missing id id id id okay it should be autogenerated I have not done that so basically uh it says not null because in the query I'm not assigning the ID if you can see I'm ass I was assuming that it will be autogenerated and for that we have to do something so let's get back to the code and this is how you do development right nothing is straightforward so here I will mention that I want to do autogeneration so for that I will use generated value annotation from jpa persistence and here I'll mention my strategy which is control space generated type identity okay so now we have to restart the application so it will reload your S2 basically okay that's done let's get back to this I'll also copy this so that I don't have to type it again refresh connect and paste and this time it should work and it worked can you see that it says something updated count five I will check that the database okay we have to file the query to get the data so I will say I will just have it somewhere where should I keep it I'll keep that in Sublime okay Sublime is messed up with this okay uh not copied properly okay no issue let's let's make the changes later but uh I want to remove this now I will just keep it copied and I will say select star from uh product and now if I run this we got the list of uh cars but I want this cars on the browser so I'll go back to the browser which is I'm already in refresh and you can see we got the data now so this data is actually coming from database but one thing if you realize the format of the date is not something I want I want a simple date format where I will see the day first then month and then year so we want ddmm y y y y y y YY feature or the format okay so how do we do this so we'll do the format thing later uh currently what I'm going to do is if I restart okay if I where is my ID did I lost my Aid I don't want Sublime close it don't save okay we got the ID it should be full screen okay so now if I restart the application see what happens every time you restart the application and if you see the data it's gone even in H2 is it is gone now it's because H2 is a temp database right so every time you restart you will lose the data we don't have this data here now I can run the S query for you uh go products and if I click on run it says product is not there it is product run okay so you can see it is empty now so how do we solve this problem I don't want to uh insert the data every time I run this project so what I want to do is I want to create some default data so one thing you can do is you can click right click on the resources and say new create a file and name it as data. SQL so basically your H2 will load this and what should be the query it is this uh product product I don't know why we got this two times we'll just have it on the okay there's some spacing issues there okay but you got the point right we got this data here in this data SQL and now what we'll do is we'll restart the application and see the magic magic or error you will get something and we got the error okay now this is not this was not the issue till spring 2.7 the problem spring board 2.7 it started after that so basically there's a problem where uh it searches for the table but the table create L table is created later and it's try to insert data with the table is not there so I want to delay this process of initializing so what I can do is to delay that we have to set one more property which is spring. jpa do defer data source initialize ation and we'll keep it true so basically data inial should be later first it will create a table okay so we'll set this property true so that now it will not be confused it knows the sequence properly so now if I run this and there should not be an issue okay it's working it created the table and it will also load it so I'll go back to my thing and run okay connect once again because the database got recreated uh run and you can see we got data let's also check in this refresh and we got the data but we don't want this data here we want this data in the react and it still says something went wrong how do we solve this how do we solve this problem so we'll try we'll try to connect this and also we'll try to understand how do we get the data format which we want okay so things are working out using the browser and the postman as well so just to show you the output of Postman so when you click on this send you got this data in the just Des format right uh in fact in Postman also you can get in a text format if you want but since it is Json we can get Json uh you can even request for XML but then for that you have to request for XML by default you request for Json okay but then let's talk about the front end now if you if you remember we have seen the front end code and then I have stopped it so we'll run this once again basically when you run this it will open the page let's do that so I will say npm run Dev and it started on this port number so mind you uh just to give you a hint so basically we are using this port number which is 5173 for the front end so when I click on this uh it will take you to the browser and you can see this is what it says the reason it is saying that something went wrong is something is wrong right how do we know some what is wrong so what you can do is you can go to the inspect element and let's head back to the console and let's see what erors you get when you run this so refresh and you got an error so if you try to understand so it says uh this is from origin this has been blocked by the cross cause policy uh no ACC no no access control allow origin header is present okay so basically uh it gives you a cause error which is cross origin resource sharing error so what is this see the server is running on Port number 880 first of all let's check if the server is running so I will just go back here run server is running right but the server is running on 8080 so if you head back to your diagram here so the server which is this is running on ad80 port number and the front end react is running on five uh okay what is running on it's 5173 and they are basically running on two different port numbers now what happens is for the security reasons U it is not allowed to send a request with with some other address maybe there will be some malicious uh website which is trying to access the resource so that's why they say okay hold on uh you can only access from the same resource and that's why we are saying it is cause which is cross origion resource sharing eror so from from the same Orion you can access it cross you cannot and that's what is happening here so we're getting this error how do we solve this now of course there are multiple ways of doing that from the server side from the client side let's only focus on the server side now so if you want to solve this problem column before spring boot we were supposed to do a lot of configuration but now in Spring boot what you simply do is you go back to your controller on top of your controller just say at the rate CA origin okay that's it nothing changes by doing this just restart and I don't see any error in the console that's a good thing okay now let me go back to the uh browser and hit this URL without the front end it works for the same resource or when you're accessing the from the browser directly but when you try to access through the browser or through the front end let's refresh and you got the data so that was the issue and it was giving you eror so whatever data was there in the database now you can see on the front end so what is this uh okay so I have to go back to 100 so this is how it looks like I will go with the dark mode this looks good compared to the white mode doesn't matter okay so we got different products here and as you add more products you will see that here of course when I click on ADD product this feature is still not here so in the upcoming videos where we talk about add product we'll change the front end as well home is this if you click on the lco it will take you to the lco homepage okay and if you click on categories it's not it's not enabled yet uh cart is also not working if you can see search will also not work add to card button from here will also not work so we have to enable this also when you click on each product I want to see their description so let's say if I click on Swift so what exactly the car Fe features are so I want to know those things so maybe the features like easily damageable or not safe car on the other hand Nexon safe car again I'm not promoting a car or saying anything it's just that opinions anyway so let's get back to the front end and let's see what is happening in the front end how you are sending the request so this the page on the homepage when you refresh the page it will hit this URL which is uh which is mentioned before and whatever data you get it will be set in the products and using that products we are running a map and for each product we are basically running uh it's like a loop kind of stuff and you are fetching each element and you're printing or you're showing on the screen okay uh it's looking beautiful because of the CSS otherwise it's a simple stuff now how do you make other things basically if you see there is also add product but uh it says coming soon there should be some more code here and for search also we'll we'll have that so nav bar I think it's not implemented yet so so yeah so in this video we have talked about the cause origin which is mentioned here and we have solved this problem so now the front end and the back end is connected and now we can focus on the next parts so now it's time to go one step forward so basically in the last video we have seen how do you get all the products on the screen right but now what if you want to know about each product what I'm talking about is let's go back to the front end so I have changed the front end for this video so you will see that in the description and let me open that uh new code in vs code yeah okay so this is the front end to as you can go with the names so the first one was one this is two so this is a different UI nothing much changes only one particular change so if I click on SRC you can see there's a folder called components inside this component there's folder called a product so basically uh let's go back to first of all the home we don't have navigations here right navigations is is here so so I can basically get into get on the products and I can call this product which is here now what is happening in the products is so basically your client will send a request to the server for one product not all of course you will get all first because of this page of homepage but then if you click on one of the product will send a request to the server and it will send the ID of that particular server of of the particular product so this is where the we are sending the ID so of course on the back end we have to handle that okay uh so that's something we'll do in this video but before we do that I want to make for some more changes or maybe we'll do it do it after so once we complete this we'll make those changes what I was talking about okay so how do you make it work how do you get this particular ID or how do you get a product with the ID uh so let's run this and to see what we are expecting so have to run this I will say npm install and there's no problem I will say npm runev it's running on this port number let's head back to this port number here okay it says something went wrong is because we don't have our database okay uh or we don't have a server running so let's go back to the code and let's run the server okay no problem for the server you can say it says stormcat started and now if I head back here if I say refresh uh you can see the products on this page but then with the new UI I can actually click on a particular product so if I click on this uh it says loading but nothing will happen Okay because the server is not configured to uh do this you can see the request here it says product /1 uh I can just go back here and inspect element console and let's refresh this to see what happens okay we got an error basically we are not able to send the request because we don't have the back end ready so let's go back to the back end and let's add it so in the controller we'll go back to this controller and add one more method in fact we don't need hello well at this point so I can just remove this but now now and let's add one more method now this is going to return only one product not the least of products so I can simply say product and the method name will be let's say get product okay now one thing you you remember we have talked about this before you are sending the number you're sending the ID right in the URL so if you see here we are sending product SL1 okay so in that case I will just map it with get mapping and we'll say slash product slash I can't say one is because the number might change so in that case I will accept that in the ID format and we have to also accept that here so I will say path variable and then int ID so this is where it will be mapped and once you got the ID I can get a particular product but who will give me this particular product so of course I can say return service you are responsible to get me the product so I will say get product uh baby by ID so get product by ID and we'll pass this ID here but unfortunately we don't have this method in this service so what I will do is I will ask my IDE to create this get product by ID method and of course even the service will not do anything service will ask the repo to give the data so it will say hey repo it is your jobs return repo dot uh now we can't say find all because find all will give you all the products we want the one uh so in that case we have to search by yeah so you can see we have find by ID method so we'll use this and let's pass the ID okay now this will not work is because of this uh find by ID returns you the optional product not the product because there might be a chance that in the database we don't have any product with this particular ID so it will return null and then you will get null Point exception so we don't want it so to safe side it says I will return optional it is your job to handle it now I can handle it I can check if the product is present don't don't only return this otherwise we can R something else or I can simply say Get it will give me the product okay or better way is say or else if the product is not there in in that case it will you can return a blank product uh by sending value let's say zero okay there are lot of values you have to set okay so what I will do is I will uh simply keep it empty so that you will get all the null values or default values not a good option okay uh properly you should implement this uh with the status code and something but we'll set the status code later at this point just keep it simple it return the empty product or you can even say get even that works but again not a good idea okay so at least if you have a product it will give you that's what we are doing now so now after making these changes I hope this will work so I will just refresh and it says okay still okay table created all the steps have been done started on Tomcat let's head back to our browser and let's close this let's go back to homepage we got all the products and this this time let's click on some of car and we got it you can see this says Honda City cool uh so this is how basically you get it few changes I don't know why it it's coming out of stock we'll check this later but if you see the date here this is weird Okay we don't want to show product listed on this you can do two changes here either you can make this changes in the front end you can simply get it and using react you can change this value you can read this value and convert into DD YY I mean ddmm YY format or we can directly store this type of data in the database in a different format let's see how we can do that in that case what I will do is I will go to product and on top of release date this is where you have to make a change U so you can basically change the way you store data in database or the DAT in database okay and the way you can do that is with the help of the Jackson Library we have used so Jackson is a library which we use to convert your object into Json Json to object but it can be used to change other formats as well which is date format here and the way you can do that is by using the Json format so this is the an we have to use Json format and specify what you want to change uh so basically I want to show this in a string format so I'll say control space it can be done with the help of shape and we have to use a format I want to use a string format so control space and we got string then the next thing you have to mention is the pattern in which format you want to store it so I will say pattern and the format which I have is DD mm y y y so it will basically make my job easy so by doing this we are changing the format how it will look okay uh so restart the application I think I've done that so let's go back to the browser and click on home so we got the products and if I click on one product uh now look at the format so this is much more datable for the IND list I'm not sure what format we use all over the world different countries are different formats but this I'm very familiar with so whatever format you prefer you can change that uh so you can say it says the day uh it says 30 uh this is weird this is not 30 it should be 14th okay I think I have to make m capital you know date formats are always headache okay so started refresh and hit okay now it looks good so Capital we have to make the m capital okay now it looks good so this is how basically you can uh do that format uh date format stuff uh what else we can change uh this is working we have made the product controller worked right for the particular ID now there's one more thing I want to talk about see if you go back to postman so let's say if I'm not using the client the react client I I'm using Postman as a client and when I when I hit this you got this response right but maybe it's a good idea to send your own status code we're not sending it okay and sometime in the front end also if they want to handle the errors and stuff if you send a proper status you have to send the status that's important okay okay how will you send status because we are not doing that this these are the default HTP status which we are getting so we are not doing that here so instead of returning this we should return the data as well as the status and the way you can do that is by using something else and that something else is called the response entity so instead of returning the list of products return the response entity object of type list list product so I will say response entity of type list of products so by doing this we are not just sending data but Al the response entity so we can have status there but then you can't return a product where it is asking you for the response entity so what you have to do is you have to create object of response entty and in the bracket you pass this object and the benefits are with the object you can also pass the status code so maybe I want to pass HTTP status dot so we have inbuild object you can see we have okay uh you can you can send something else as well maybe anything you can also say found if you want to do that I think that's the only thing matching here apart from okay so I will say okay uh we can also use created when you create a product now we are not doing that we are simply sending response so uh okay works if you are not comfortable with this syntax what you can also do is you can create a list of products separately uh get the products and then pass the object of list of products here so I mean depend upon you how you want to do it so this is one way this is one thing uh we can also do it for this but let me just run this how it would look like for all the products just restart the application you know after making the changes have to check if that work that is working so let's get back here refresh it is working it is working but then the front end if you're working on front end as well if you have more control depending upon the status code you can change the way you show data uh and you can also check that in Postman but of course you will only get okay because we have not done that uh we have sending okay but let's see if I send something else I'm not sure I've never tried this found I don't know the status code for this we can check it you can click on found and found returns 302 so let's check what St code you get on the postman so head back here click on send okay server still not started started now okay so you got data but you can see the status code is 302 found and this is not a good idea so good status is always 200 series so we'll say okay let's change the product as well so here also we'll use response entity so from now we are going to focus on response entity not just the product or the data so here also we'll say new response entity we'll put that here looks good and we send the status now since we are doing this we have we can do one more thing now since we are sending the status don't you think we should also check if the product is there uh so I can check if first of all let's get the product product what product product equal to service. get product by ID will send this and that's what we are doing here right on this line so we don't have to say this now we can send the the product itself but we can check if the product is not null then only you send this response right but what if the product is not there in that case you can return something else you can return new response entity we have one more Constructor which takes only the status so I will say sttp status dot uh not acceptable you're not found so we can say not found so this is one way uh to handle this let's uh relaunch it so let's head back to the postman and now send a request for let's say one we have it so it will get data but what if you said 12 we don't have any product with 12 I hope and you got error and this is where you are getting because of this so I have to make one more changes in the because I was assuming that you will get data and we don't have it uh so I will say OD else return null let's work with null because anyway I'm checking that on the client side right I mean from the controller so send this works to 12 and you can see it says not found so this is what I want to I wanted to return see there are multiple ways to handle the situations um I'm going for this approach looks easy but depend upon different scenario we can use different approach again sending n is not a good idea we have to find some other way of doing that cool so this is how basically you get one product and we have done that here as well so it is working now it's time to move to the next step now this step is bit crucial not exactly crucial but then uh difficult to understand uh because when I was doing this before the video uh there a lot of moving parts and there a lot of things we have to take care of because we are not just doing adding of the new product because that's what we have discussed right uh so we have talked about getting all the products and we are getting those products on the page so just to show you the browser this is what we had so if I see here we got the products and if you click on this uh you will also get the items that is working right uh but now what I want to do is I want to basically have images as well so with each product I want to get their image but that will make it more interesting right so because when you go to any website or any e-commerce website with the product you also get the image and of course when you click on this thing on the left hand side also I want an image next thing I want is the add product of course this is not there so we have to change the UI as well but I want is this option of adding a product and when you add the product I also want to add the image now the tricky part here is if you see uh not this if you see our code this is the UI if you see our code in the product we don't have any variable which handles the image we have ID name description uh brand price category uh release date availability and quantity nowhere we are talking about image okay so that's something we have to add so now this time we'll go for the new uh UI okay so we have updated the UI U for the for this particular section and we are going to use the new UI and the new for the link for the new UI you will find in the description okay but in the new UI you don't need to handle this because it has been handled in the UI itself so we don't have to convert that by uh by doing it in Java so the in UI also we have done this so we can remove this so you got the right we can do that from the back end as well now I'm removing it so we'll handle that in the UI now what are the variables we need and what is a new UI so let's let see the new UI first uh this is the front end to for the previous section uh I will open the new UI so I will say open so this is where I have my new UI which is the front end 3 I will open this I should have stopped the earlier one it was stopped okay I've not stopped it maybe the same put number we cannot use let's see let's see so I will close this and now I will open the terminal in the terminal basically I'm going to uh do the same thing NP M install same steps which you have done before uh since new project I will just do that once again run Dev and okaying on the same port number so it was stopped automatically I'm not sure so if I click on this it will open the new why it is opening on the other screen I will open that here okay this is a new one uh this is the old one or which is a new one okay so it got refreshed automatically anyway this is a new UI and what is missing here is you can see that we got the products but then this thing here it it was an image and we are not able to show the images because we don't have the image from the back end next when I click on this on the left hand side as well this is where I want the image so I will I want to do that as well and how do we do it and also we got the add product page now this is where you will enter your product name brand description price uh which category it belongs to the quantity the release date of the product and the image okay now this this is basically tricky part image part other other things are easy now if you from a long time from the start you know how do we create a new product or how do we create a new object and send it and save that in database we know that but the tricky part here is to handle the image so we'll do that we'll do that step by step so let's get back to our IDE uh in fact we should talk some we should see the code in UI as well uh I'm looking at the Cod UI with you so just bear with me where are the changes would be nothing in app uh category use State okay we got product with ID this was there before okay we also got cart uh option so if I click on cart we can also see the cart but we'll do the card part later not in this video and we'll do add add product this is what I want to do and how the add product page is built is is with the help of this so in this we are changing the product and image separately so we got two different uh methods to do that we got set product and set image so basically we are fetching them individually so we are fetching the product here we sending the product this is the post and for image image will be loaded right so that will call that function is calling handle image change handle image change is calling a set image okay makes sense so this this is how basically you are sending the request for the add product and on the homepage also we have a change because not just the product data but we also want to show the image and for the image we are sending the request separately if you can see we are sending the request for each product so we are using a map for ration and for each product with a different ID we are sending a request for the image okay so since in the UI we are using this format let's say use the same format in the back end so the request would be product SL the product ID If the product ID is one it will be product SL one/ image product SL2 SL image that's how it will go so it will fetch the image for a particular product uh since it's not in database it is giving you that blank icon but that's what it is there in the UI I don't think there's much change apart from that there's also card button will us will be doing card later in product individual product also we are going to supply the image so same URL you can see product/ one/ image or product SL2 SL image so same URL we're going to use so in the back end basically we have to build two URLs one for adding a product and one for getting that image okay so let's do step by step let's go back to our code and the first thing you're going to do is change this product so for the image I need three things because when you send data on a client side you have to also specify the content type of the image uh not all formats are supported maybe someone is sending the malicious executor file or some Javascript file so make sure that you go for images like jpg so that's something you can check on in the front end side so you have to specify what type of content you're are working with so this image actually can be a file so file name file type and uh file data so we can also say image data that's fine so I will say private string image name uh then private string image type and uh the data so I will say private now what type of how do you store your data so basically there are multiple ways of handling with images you can store the image on some uh cloud storage okay and you can get the link of it so you can store the URL of that cloud storage in your database that's one way uh we went for the simple way because otherwise we have to sign up the a account store the image there so multiple steps right just to keep it simple I'm storing the image in database again not a great idea but it works so I'll be using a bite AR because we are going to store that in a bite format and I will say image data now whenever you work with this type of data like bite array which you want to store in database we have to store that as a large object so we have to use annotation for from persistence which is lob which is large object okay so this is what we have done and now if you run this you will get us different database different table and since I'm working with images I don't want some pre-loaded data so I will okay let's have the same preo data and let's see what happens so let's reload this to see what is a database change we are getting so this is loading okay Tomcat started uh I will be going to my console S2 console okay refresh default data I will say product and star so you can see we got this data but we got three more columns we got image data image name and image type and all are null you know why is because we have not specified this thing in the data SQL but I don't want it actually because image is not there uh I have to manually update each image we can do that in the update feature but then we are not working on update now so I will just go with the blank data and I will type each object Tous task but we have to do it right so what I will do is I will just delete this or maybe I'll just change the name of it I hope by changing name it will not be loaded so data SQL one uh next I want to so we have we have made the changes in the product and we have seen that it is reflecting in database as well it's time to accept the new uh product so I will create an object I will create a method here for the new product so I will say public we return the response entity itself with some data and I I may return any type of data so I will say put in question mark because I'm not sure we might return the data or we might WR an exception so we'll have it both uh I me not exception but only the status if something went wrong uh next I will say add product now the question is what kind of mapping we we going to do so of course we have to do the post mapping so post mapping and the URL is simply slash product nothing else just a product because we are sending sending data in the body right so this is where you're going to send data now if it is a normal product with without the image it's very simple you can use request body and we can simply say product product our job is done right it will accept the product object but the tricky part here is we are sending the image as well which are a different format so instead of using a product a request body we'll be using the request part now request part and request body is bit different request body accept the whole object or the whole Json as object request part will accept in a part in a part as the name suggest right in two different parts or multiple parts so request part for product you accept in product but for the image we'll do that next line so I will say request part and we'll accept the image now image itself is a file and we'll accept that as a multiart file yeah multiart file and we'll store that in the name called image file you can also say image that works but image file is here now once you got your data we have to send this data to the next layer who's the next layer we service layer right how we going to send this so I can simply say hey Service uh add the product whatever I'm accepting and I'm going to pass you two things I'm going to pass you the product and I'm going to pass you the image file so you just take care of Shoring that in database but I also want to check if it is actually getting stored so what I will do is I want it to return me a product I will say product okay and what if something goes wrong CU so we also have a product name here let's say product one and okay how we are going to get the error so let's say if something goes wrong with the ad product it will throw the exception right so what I can do is I can set that in a try catch so I will try to add the product if it is not working if I get the exception e so in this case I will simply return the uh some other response okay but if some if everything goes wrong I will return new response entity in which I'm going to save the product which is the new which is the save project in in database and also the status code the status I want to save is HTTP status do created but what if something goes wrong in that case I'm going to return the response object new response entity but we'll send only the status not the object itself or maybe I can send the message so e do get message so it will send the other message again these things will be handled from the client side so whatever message you're sending client can decide they want to show it or they want to handle their own way but I'm sending the message with message I also want to send the status I will say status dot uh for error it should be internal server error something went wrong but also we are sending the message so at least the client will know what is going wrong okay so we got add product but it's not there in the service so let's create this I will just click here and say hey create this method called add product and now the next step goes here how do we do this how do we add a product it's actually quite simple right we can can add a product by saying hey repo save the product it's so simple right get the uh product from the uh from the controller controller send it to service and we getting in the service from service send it to repo job is done right but actually no because we don't just have to send the product we have have to also send the image right now this is tricky to send the image basically we have to get the image and we have to convert that image into bytes remember this database we are sh in a bytes format okay so we have to get the format and not just that we have to also get the name of the image and the type of the image so in the product we'll say uh set the image name and how will you get the image name so I will say image file dot get name in fact not name it will not return we have some other method yeah it's original file name product do Set uh image type so I say image file. get content type and and then we have to set the image as well so image data and this will be your image file dot we have to convert it into bytes and we have a method called get bytes okay it might throw an exception yeah so we have to add the signature it might throw an exception so I'm throwing I exception and once you have all the image data in the product I can simply say return uh so whatever you save will be returning it here and it it should be stored in database I hope will work so what we have done is we got the product we got the image uh we got image details and said that in the product data or product object and then we are saving it so this should be stored in the database that's the first step let's try let's see what happens I will relaunch it and if something goes wrong we'll solve it right we know how to solve those now so Tomcat got restarted I will go back to my browser I have to move multiple pages and here I will click on home okay it says nothing is visible so I will click on ADD product let's add some product I will say Sam mobile the brand is Samsung some description price $99 uh it's a type of Mobile stock is let's say four date release date is let's say 1st June image I got in my downloads so that's the image here available yes click on submit okay it says product added successfully but when I click on okay and if I click on home image is still not there okay so we'll solve this but I want to see if the image actually has been sent on the in the database for that I will go to H2 and if I click on product okay I think I have to reload yeah connect product run okay we got the product and if you can see uh we got okay something went wrong with the description we'll check that later uh but we got we got the image we got the image name we got the image type we got the name of the phone price quantity okay so these two things we have to sync up I think there's something wrong with the communication between the client and the server but that something will resolve it it it is might be some name issue but the problem but the main thing is we are able to send the image but why the image uh product details are not coming up what I will do is I just go back here and just I just want to print whatever we are receiving in product just to see uh what is missing from the client side add product Al Sam M brand Samsung some description $88 a phone stock six release date one image available submit product added but now I want to see what is coming on the console on the console it's sending ID zero makes sense and one okay so description is not getting sent from the client itself so some there's some issue with the UI we'll get it resolved and we'll replace the link if I conncted that but the most important thing is we are sending the image separately and that's why you got null here but we'll we'll solve that it's nothing not a big issue I think these are the names we have changed right that's why there was a issue anyway uh okay I also we are not even loading the default data so that is working but what about the image why it is not loading the image it's because if you see the UI the image is getting fetched as a separate URL and this is something we have to build okay so uh we need to work on this not just on the product page but also on the homepage it is requesting for that URL and now we have to search for that URL where it is doing that yeah it's here so we just have to build this thing and it will work we just have seen how do we add a new product but with the help of image as well uh with the help of multi multiart file and we are getting the details about the image and storing that in the product and then we saving the product in database so now let's work with that image part so in the UI where is the UI where is the UI in the UI when I click on home you can see the image is not coming here and even if I go inside it's not working and apart from this there were multiple issues the first one is it's still showing out of stock so we'll solve that and I think I know the problem if in the description also we were getting that empty right so if I click on here there's no description we have entered the description also it is not showing the stock is because in the UI and in the in the back end there's a name issue so name is same brand is same description what we are using is only uh the dis disk not description so if I change this I think it will affect some somewhere else I'm not sure uh but I think I have to change that in the front end yeah so I have to change it everywhere so I know the problem you will solve this I know uh so in your code uh in the back end change this uh names or maybe I will just change that fast okay what I will do is I will just go back to the product now since we are making this mistake from start so let's resolve it description what is what available it's product available stock quantity what else was there stock quantity product available release date and there was one more issue description price category yeah I think these are the only issues okay but if I change this I think I have to change somewhere else as well in the service anywhere we are using it no good in repo also no change in the controller also no change okay looks good so those are the thing things we have to change and now let's work with the image so in the controller uh we have to work for that URL right so which URL I'm talking about is in the home this URL so product slash I just copy this this is what you need to work on so let's go back to our code and create a method for this or create a mapping for this so it's public and we are going to return the response response entity in the entity the type will be bite array because we're going to send the image right bite are and will say get image by product ID because that's where we are going to pass because the mapping is get mapping in the URL this is the URL right so instead of saying product I will simply say ID or maybe product ID will look good why there's a mistake here okay so let's complete this method now and there was is show with the method name and here we have to accept the name so I will say product V product path variable uh integer product ID and let's work with this now how will you get the image so of course the image will be coming in the product itself right so we'll fetch the product and we'll save that in our product so I will say product product equal to and who will give me the product it is service so I will say service hey service get a product by ID and the ID we already have now so this will give me a product now from this product I want to get the image in which format in the byes format so I will say bite AR image file equal to how will you get this so I will say hey product get me the image data dot get me the bytes okay get image data itself is a bite right yeah it's a itself is a bite so we got the bytes we don't need the name of it but we'll need type because on the client side we have to send the content type what of a Content we are working with because there might be some issues so we'll send the content up as well so let's return the data now so since we have the image so I will say return this time we want to send the content Ty as well so I will use a different format response entity because it has multiple ways of doing this uh you can send the okay which is the status and we can say dot I want to send the content type as well the content type is okay content type is Media type so we have to say media type Dot uh we have to use value type value of and you have to mention the type of the content and how will you get the content type so I say product dot get image type uh apart from this so we have we are sending the status okay then we are sending the content type we have to also send the data right so data will be going in a body not boy body and the data itself is image file okay looks good so we got the product then we got the image in the bite format we are sending a response by sending okay we sending the content type which is the get get image type and then we are sending the image file uh you just try out without the content type I tried before this there was some issue and uh this has resolved it you try it without it and let me know in the comments if it is working for you so now with this let's refresh I hope this will work hope but again when you restart we have to create the object once again so let's let's try so Tomcat started uh let's go back here click on home you can see no product available I will say add product I will say Galaxy M that's a phone name brand is Samsung best phone $999 okay that's a huge price for this phone $500 it's a mobile phone stock is five uh release date is let's say 1st June image pegged product is available click on submit it says product Ed successfully that's great Okay click on home oh we got the image okay so that's a good thing click on this and okay description is still not there okay description is there yeah it is best phone uh stock is now five so we have soled that problem it says add to card so yeah the names for the issue and we got the image it's huge but then that is something CSS issue not our issue right I can click on add to card it says added to the card but we have to manage this in the back end oh no that's a UI UI feature anyway uh now I will add more products I'll say sports Show brand is Nike Walkin code price 99 category fashion stock is let's say only one uh date of release is today image I hope I have picked this show image available submit product added successfully Great Click on home yeah we got two products so like this like this we can add more products and we you can see that here and if I click on this this is what we got okay things are working out looking good so this is how basically you ingate the front end and backend uh so for a particular URL you have to build the API so that the front end will get the data and that's what we have done cool stuff right uh we can handle this more because we actually not checking while sending the data that if the product is already there what if you're sending an ID and the product is not there what you will send so we can handle that here using try using eils stuff and yeah so this is working so once we have done with the create and read it's time to do the update and delete the thing is uh the UI which you are working with I'm not sure it has the update and delete feature I can't see that here so we'll be going for a new UI so we'll replace this first of all I will just stop this whatever was running here and let's open the new UI so I will go to file open and this is where I have all the UI now this is the latest one so this Ecom uh front end for I will upload that in the I mean you will find the link in description I will open this and open so this is our new UI and we need to make it work so again the same steps which we have done before I will go to terminal I will click on new terminal and here I will say okay don't restart now npm install that's the first step the next step is we have to say npm uh run Dev okay so this is running I will click on this thing it is opening on some other page just take it here okay so here we got two products now this is because in the database we already have uh two things have not restarted my uh database yet or the back end yet it's only the front end which is uh changed and now when I click on any of the product you can see we have this button now which is update and delete I'm not sure in the old UI it was there please let me know in the comments so I will just check that after the video but we got this button now update and delete and when I click on update it should open the update form so we'll have all these details and I can change the category of this maybe I will just put that into Electronics now and the number of quantities I want to make it seven uh let's skip the same image and we click on submit so it says failed to update the product try again so what went wrong of course we don't have a URL or we don't have a API which will handle the update but which URL it is heading so let let me just uh check my UI so if I go here in the components I don't see update yeah we have the update so we have update product and it is it is hitting the URL for put okay where's our put request yeah so this is a put request and we are hitting a URL which is local colon 8/ API product/ ID but they put request okay that's important put request and what type of data we are going to send so from this we are going to send three things one of one of one thing is of course the ID next is the product and the multiart uh image okay so we are going to send three things the ID the multiart image and the product so for this we have to create a API we also have a delete option I will show you the delete one uh so delete product is getting called so I'm into product. jsx and yeah so this is the delete request so for delete also we have the same thing same URL but the method is different okay and in delete we don't have to send the product data or uh the image we just have to send the ID okay so these are the two URLs just remember these are the two URS we have to work with and for this we have to create the API so let's head back to our project and let's create these two controllers uh so the first controller I'm going to create is this for the update so let's say public and we are going to return the status with some text so I will say response entity and it is string and I will say update product now as mentioned we're going to accept three things but then what will be URL uh we have to use put mapping for the update and the URL is/ product/ the ID and that ID we have to accept here with the help of path variable so I will say path variable in ID that's the first thing apart from this we can just take it from the uh post mapping you can see we are accepting these two things so I can simply use it we want to copy pting we're just reusing the code right that sounds better and open close so this is this is the update meth which we have okay so when I say update who is going to update this of course I will ask my service to update the product so I will say update uh product and I want my update product to also return me something so it should return the product which is updated and maybe based on that I will return something maybe I will say product one and here we have to send those three things we have to send ID we have to send the product and we have to send the image file three things okay and if you receive the product or if if there's nothing wrong so product one if it is not equal to null in that case I will return the new response entity by sending two things I will send the text first which is updated and then I'm going to send the status which is okay or created okay okay looks good so I will say okay and else I will return new response entity we'll send the message failed to update and we'll pass the status dot maybe bad request or bad yeah B request works okay so uh this is the thing but then this is not working right so the actual logic goes into the update produ now this is bit complex so what I will do is I will just go back here and say create method update product see it is complex because we are also working with the image if it's simply product there's no problem but since we also have a image here then we have to make some changes now it's very simple actually you can simply use your repo by saying hey repo uh I want to save because even if you want to update you will use a save method you can simply pass product okay U it's our it's I mean it depends upon us if you want to use the ID or if you don't want to use the ID because I can simply skip ID if I want I can simply update this but there's one more issue with this what if the product is not there of course no one can send an update request prodct is not there right because we working with the UI but anyway let's imagine if someone is sending a request directly with the the update request and the product is not there it will create the product so maybe you can have a check if the IDE is present so that for that reason we can use the ID otherwise the code which I'm going to write I'm not going to use the ID okay so what I was saying is we can return um we can return report. sa because this will return the product and that's what I'm returning here but then it will not have the image we don't have the image data right in the product so before doing this what I will do is I will uh add I will say product do set image data and it'll pick that from the image image file. get byes now this get bite might through an exception okay so I will add to Signature I will say through iio exception but in the controller then we have to use try catch yeah this works but then we are throwing the exception we should return return this yeah bad request okay yeah the code looks big but then if you see we have a line for try catch uh if else the simple code is just this update product okay but if I go back to service this is first thing you have to set uh next I also want to set the name of the image I will say set image name and that thing I will get it from the image file dot uh get original file name and the type product do set image and if you have a better option let me know I'm just trying what I have uh explored but if you have some other way of doing this let me know in the comments so what I'm doing is I'm updating the product and storing the product and this should work now and I hope this will work anything need to check it looks good maybe we'll also write the code for the delete in one go I don't have to type these things again uh I will say delete mapping for which request delete mapping for SL product slash ID and here we'll write the method name as public even this will return the status so I will say response status with message so I will say string and delete product this will accept the so I will say path variable and accept the ID we can check you know before deleting it we can check if the ID is there uh I will go for a simple way so I will say hey service say delete delete product and pass the ID you know actually we we should check otherwise we'll not be able to return the status right uh so what I will do is I will just get the product first I will say product product is equal to service. get product by ID I'll just pass this ID and we'll check first if the product is not equal to null in that case it should uh Delete and return something so return the new response entity by passing a message deleted you know we should print product successfully deleted or deleted works and then we'll send the status so HTTP status dot do we have anything for deleted let's see yeah we'll go for okay okay but what if the product is not there so in the else case I will return new response entty and we'll pass the message uh product not found and return the status HTTP status do not F okay but then we don't have the delete method so I will just go back here and say create the delete method and in this we'll do very simple stuff I will say uh repo dot delete and pass the ID but I think for ID we have to use a different method it should be delete by ID and pass the ID that's it and now we have basically worked with two methods one is the update which is the put and delete so put will work with the update that's what we have done here and this will send the request to the service so things should work out I'm assuming it will work so let's restart the application okay looks like there's no issue Tomcat is running and now I will open my uh browser okay and in this let's click on home now the moment you say home you lost all the products because we are we have restarted this app application so I add product Samsung Galaxy which should be M brand Samsung some description price let's say $99 category phone or let's give a wrong category let's say let's put it in electronics and stock is let's say 9 date of release 1st June I have kept everything in the images I will click on this this I hope that's a phone and now product available submit so we got the first product and okay that's here let's add one more gaming laptop not laptop laptop brand ASUS some description price 999 uh category let's also put that in electronics we'll update this as well uh stock seven uh release date is one maybe we will'll have a release date 12th of June images of laptop product available submit okay so we got two products now the first thing we are going to do is update something so let's go to the first product and what I want to update maybe I want to update this description and also the stock so I will click on update and we'll change this description best budget Phone Stock I will reduce it to let's say six I don't want to change the image category I will yeah we have to also change the category to mobile it was laptop it was El 20 I think and click on submit and we got it says updated you can see the message here and if I click on home now let's let's go back to this product description got updated the stock got updated and we have also changed something or maybe two things so that's how basically you can update what about delete so what I will do is I will delete this laptop here and click on delete so it says delet successfully uh I can still see here maybe it was not refresh refresh it's gone okay so again the UI things UI things need to be changed but things are working out so we have worked with the update and we have worked with the delete uh so just to Del TR what we have done is we have added the two methods one for the update product and one for the delete product in the update we are accepting three things of course we are not using ID but we can if you want to check if the uh product is there or not before updating it otherwise the save method will create a new product okay so that's one thing you can check again that's up to you if you if you have done it let me know in the comments others can also see your work and we have done with delete in the service basically for updating uh it's very easy actually updating we can simply use Save method but since we have also have a image that that's why we have this three extra lines otherwise simply say works and delete simple line because we're not working with images we just say hey delete the product and our job will be done so that completes the basic operation what we wanted to do with this I hope you have enjoyed and if you have enjoyed please do share with your friends so now it's time to implement the next feature which is a search feature now why search is important is because we are using data jpa right which is spring data jpa and if you look at the repo layer it's quite empty so we are not defining any method or we're not even declaring the methods we we got all the methods from the jpu repository so when things are simple like C operations it can be done with the help of JP repository but let's say if you want to find something which is not a primary key because whatever we are doing now if you're searching for a if you're searching for a product by the ID it is very easy right you can just use a product by ID and it works but what about if you want to search with some other things example if you want to search with the name or I the product name or the description or the amount the quantity I mean that's weird right why someone will search for buy quantity byy quantity doesn't matter you got the point right so if you have other fields to check with how do you make it work because Bight will not support example if I go back to my product service and let's say in any of the method here uh let's say in the update itself just just to check what can do do so if I say report dot there are multiple methods right uh one of the method is find by ID so if you have the ID you can do it but we don't have a method using which you can actually find by name or any other field of the database now what are the fields we have if you go to product uh we got description brand yeah so let's say if you want to search with a brand in that case what you can do is let's say when you search with a brand and of course you can have multiple products with the same brand in that case you can Define you can declare not define you can declare your own method which will return you the list of product and okay list of products and then you can def you can mention the method you can mention find bu and you can mention the variable or the type which is brand and then you can you can pass the brand here okay so this is how basically you can achieve this now your GPA does support this right but then this is only for one field or maybe you can have two Fields you can also have your and and you can mention the next uh variable but let's say you have multiple things to check with example if you want to search but they're not just based on one field or maybe multiple field or maybe you have to you want to mention some customized query in that case you can literally write the query not the SQL query you can but then I don't want to write the SQL query and that's why we we got something called a jpql which is jpql which stands for a jpa query language so it is similar to SQL the only difference is in SQL we use tables here we have to use class name in SQL we use column names here we have to use the field names okay so those are those are the changes we have okay uh so the method which I want to do is which I want to write here is a method which will return the least of product and maybe I will have the method name as search products and then here you can pass the keyword on on which you want to search right so we can have this method but this will not work so by default JP will not understand what search products uh means so and that's why you have to mention the query and the way you can do that is is by writing atate query so you can write the query here okay now what is a query we'll see that in sometime but we have to mention the query in this in this brackets okay and of course in the query we're going to use keyword uh so example if you want to search something in uh in SQL so you say select star from the table it's a product where the product or the brand is equal to and then you you can use a like keyword and you can put that in single code right so what's a keyword you want to search with so that's something we we are going to use here but then if you want to achieve this we have to also change our front end right so let's start with front end and then we'll come back here so let me go back to my front end now this is our front end right now in this front end basically we don't have the search feature so for that I'm going to open the new front end so and you'll find the link in description so I will say open folder and this is the thing which is Ecom front end 4 so this is the fourth front end which we are using of course everything is iteration so nothing new uh and if I open that uh okay I can simply say npm install and by the time it is happening let's look at the place where it is searching again with you I'm doing this for the first time so I'll go to naar I'm assuming that it is NAA and here it is uh so you can see there there will be search box and then every time you hit the keyboard or when whenever you hit a key it will call this function called handle change uh let's see where are we calling it yeah so if you can see uh we have handle change here so with every input every time you press the button it will hit this query again there are different way of implementing it maybe you can have a button so once you type the entire text you can hit the button uh which is much more efficient in terms of uh server loading because you're not going to search this server for every key but maybe we want to make that uh more user friendly just by typing few letters if you can get the suggestions uh so it is hitting that and it's requesting for this URL so this is what we have to work with so search and then the value okay so we are using using question mark here in the front end we can also use slash but let's question mark makes sense here so we are not going to use path variable because this for this we have to use something else again we'll see that so this is the URL we have to work with so install done let me just say run T and enter so it's running on this port number now maybe earli port number is busy it says something went wrong uh because the server is not ready and this is what I was talking about so if I search something it should give some suggestions there's no button here to say go so to give the suggestion so now what I'm going to do is I'm going to go back to my server or the back end and in this basically I will go back to my product service we have we have return this let's remove the extra line and go back to controller so basically we need a mapping for that particular search so I can create a public it will return the response entity which will return the list of products and let's say the method name is search products now this will accept a string right so string key keyword which is coming from the client side and for this we are going to do mapping as well so I will say request or get mapping for the URL so now we know the URL it is SL product slash okay we don't know the URL SL product/ search is the URL okay but then how we are going to accept uh this particular thing here which is name is value now in that case of course it should be keyword so searching for name okay looks like I'm using a wrong print end okay so we let me open the fifth one I think fifth is the correct one I was expecting that it will be keyword but I got name uh let me just check the fifth one and yeah so it is keyword so this is the correct one not the earlier one uh so what I will do is I will just do the same thing for this which is npm install and npm runev so when this is using the same port number okay this is what I was looking for so we have changed the uh front page if the back end is not ready uh okay so my bad so I was using the wrong UI so I will share this link in the description the fifth one okay anyway so let's get back to the code and here I'm going to use the same thing because we are searching for keyword I mean we are passing the keyword in the URL so it will accept that in the same name okay once you have that the next thing you have to do is we have to basically call the service method right to search it now the service method is going to return you the list of products and I will say products is equal to and I will use a product service so I will say service dot uh search products unfortunately we don't have that method there but uh we'll we'll create one so I will say keyword so I want this method in the service but let's say if you got the products how will how will return the values I will say return and now we have to return the response entity not the products and in the response entity we have to return the products and then we have to also give the status which is dot okay and done but then we don't have this method so let's create one in this service and in the service we have creating this method now again service will not do anything service will simply say return and it will ask the repo hey repo search the products and since we have created the method in this repo first we have that there just pass this keyword so service job is nothing much here just pass it to the repo now in the repo we are going to do the actual work okay I'm just confirming if everything is good in here so looks good nothing wrong let's go back to repo and let's write our query okay now basically we have to use the jpql right not the SQL query so it looks similar to SQL so I say select and we don't say start we say p now what is p here uh so I will say from product and mind you product is not a table here it's a class name and then this is the Allies for it so this p is allies for product and that's what we are using here and then we say where and now after this where we have to check for multiple fields we got for name brand so we'll do that here so I will say enter uh now the first thing we can do is we can check for the name field field so I can say p. name because in the P we got the name right so we'll say p. name and let's match it with the keyword now how do we match it basically we can use like for matching the text and then we can use the uh percentage symbol because it might be having something front and back of it that's how we do it in SQL and in between we can pass the keyword now since keyword is basically this is what you have to it has to pick up right so we can use the uh colon So when you say colon it will search for this field in the parameters which you passing so that it it sols but this may not work maybe because the text which you're searching for is small and in the database you got Capital so what you can do is you can convert everything here so you can say lower so these are some Inu functions we have and here also I want to lower it the keyword okay so we are lowering so these are the input functions we in jpql so we can use lower it will convert this into a small letters and even this will do it but the problem is we are concatenating this uh percentage percentage with the uh keyword right so instead of doing this we have to use the method so the method name is concat I want to concat this particular percentage with the keyword and this keyword with the percentage and since we are opening the brackets here that is closed here we are opening it here we'll close it here now this is only for the name what about uh you want to have some more things so you have to say all and enter more things here now since I already have all the things written I will just use it here instead of typing so I want to add more lines I'll just paste it here looks like we got an extra bracket yeah so this looks good I'm just confirming if everything is correct it's It's tricky because even if you want to check what is happening basically we have to First add those products and check it uh and I have to do things multiple times so I will just check it once okay so from B where okay looks goodt looks goodt so this should work so basically what we are doing is we are converting every charact every text into lower and then comparing it with the keyword so now once we have done this uh let's restart the application or in fact we have not started even started this so let's restart and see if that search is working in fact what I want to also do is here uh every time you call this search I'll just print searching with and let's also specify the uh keyword on which it is searching and doing that let's restart okay okay so it's running on port number 880 so no problem let me just go back to my uh browser and let's refresh so you can see it's no product available so that means Ser is connected and let's click on the search and search something let's say I want to search for Samsung phone if I say s okay this is not working let me let me just add a product here so I'm saying Sam phone Sam mobile Samsung I 33 it's a phone stock is [Music] three date is this choose a file this is a phone open and now when I click on submit product added okay let's see if that works yeah it is working but not white is not printing anything on the console let me restart once again okay let's hit that now again when you restart you will lose data and if I say s Now what song why is not printing anything let me me try for the homepage this is weird Okay it is saying hi but why is not doing for the search okay okay I think I got the problem the problem is we got here products not product okay I thought there's something wrong with the ID itself okay let me remove this high from the top suddenly I started questioning my Java knowledge now get back here and if I say s so you can say no product name with such name but it is searching with s now if I say something else there if I say sa it will hit it with sa so every time you type something it will search but now to actually test it we have to add some product so I will go back here and let's say I'm saying Sam mobile or brand Samsung and some description budget phone and here price 666 it's a phone stock is two date is current date or maybe 23 three let's pick up the phone image and add so that's the first product you have which you have added and now we got home here let's add one more to test it Al giving laptop brand is assus and pass machine it's a laptop date selected image selected submit okay so you can see we got two products now how we are going to search let's say let's say I want to search for Sam and you can see when I'm searching for Capital a it is doing that right and if I click on Sam mobile we are getting this next if I say a phone so it still so shows Sam mobile because in the in the uh description we are saying it is phone right next we can also search by uh something else maybe gaming with the name itself so we got gaming laptop we can search a brand which is Asus so we got gaming laptop so that's how your search is work working again the UI is good and that's why you can see such beautiful text here but then you have to also make your backend work to get it work uh so at least you now you know how do we write our own queries with the help of jpql so what you do is you use select from similar to SQL the difference is instead of using a table name you have to use a class name and then uh these are not compulsory thing but then since we wanted to make sure that the search should happen even if you don't follow the capital and small and that's why you got here and that's why we using lower and all this stuff so yeah that's it from searching uh I hope you enjoyed so now let's in the upcoming videos let's see something else and we'll have fun thank you so much for watching bye-bye