SPRING BOOT Tutorial For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to this course on getting started with spring boot in 25 easy steps I am rangarao carnem I am a hands-on architect with more than 15 years of experience developing designing and architecting java web applications I've been using Spring Framework for more than a decade at in 28 minutes we ask ourselves one question every day how do we create more effective trainings this step-by-step series of courses is a result of that spring boot is not the most popular option to develop restful web services in this course we will use an example of a small restful web service to illustrate the power and magic of spring boot while spring boot makes it really easy to develop swing based applications we believe it is equally important to understand what happens in the background with things like Auto configuration and starter projects all the code for doing this course is on github we will start with creating a basic restful web service and all the course of 25 states learn about different things related to spring boot spring initializer auto-configuration we'll look at how to monitor your applications with things like spring boot actuator we will look at some of the important starter projects with springboard spring boot start a web spring boot starter data JP a spring boot data rest and also will secure our service with basic authentication using spring security we expect you to know Java and basic familiarity with Spring Framework would be of great help we will help you set up and learn Marwin embedded tomcat along with the ide Atlit you can find more about the course including the step-by-step details on our github repository thanks for your interest in this course I hope you are as excited as I am go ahead and enroll for the course or you can take a test ride using the free preview feature see you in the course let us welcome you to the course again in this video we will give you an overview of the tools we are going to use in this course we will show you how our git repositories are organized and also we'll show you how to install the tools you would need for this course last but not the least will guide you on how to make the best use or discourse the git repository for this course is github.com slash in 28 minutes slash spring booth for beginners so it's github.com slash in 28 minute slash spring boot for beginners this repository contains the code as it is at the end of the course as well as at the end of every step you can go to any of these steps starting from step one to step 28 and open up the file each of these steps would have a description of what you'd learn during that step as well as a few useful snippets and also we would have complete code as it is at the end of this specific step if you look at the screen right now we have a Palmer XML the complete pound or XML is here and the application that Java so the entire code at the end of the step is present in the github repository for every step at specific points during the course we have zip files with the N tag code at the end of the step zipped up and put in for your users so you have let's say step ten dot rip contains the co entire code as it is at the end of step 10 so you have step ten dot zip step thirteen dot zip and you have 15.3 17.8 and so on this git repository would be a constant companion for you during this course now that you understood how a git repositories are nice let's shift our attention towards installing the tools that you would need if you open the github repository and go further down this is section called installing tools the two things that we would need as part of this course are Java 8 and Eclipse IDE the Eclipse IDE is these days come pre-built with melon so you don't really need to install Mavin's separately so all that you would need is Java 8 and Eclipse this PDF out here if you click the link and open it up would help you to install Java Eclipse and mavin it contains step-by-step details of how to go about installing these it's a cakewalk to install Java and eclipse and man sweat you might need some time to download everything and put them together but installing them should be really easy the two things that you need to make sure is that you are using maven version at least 3.0 so we expect my own version greater than three point X and any of the latest versions of Eclipse are fine we would be using Java 8 so that JDK 8 during this specific course make sure that you're using the JDK and not D JRE so Java development kit is what you would want Java JDK is what you are searching for if you're new to web application development I would recommend you to spend some time with the installation guide and also look at the section where we have a few important moment commands listed for your use this is also a small video here to help you install the tools take some time install the tools get them ready and I'll see you in the next step at in 28 minutes one of the things which we focus on is to keep our courses hands-on as much as possible before we discuss any theory about springboard and taste good start with a small example of springboard what we'll do is do the first couple of steps we would set up and launch a springboard application we'll use maven and Eclipse and drilling step two we'll also create our first list controller once we do that we would have an explication which we can use to discuss all about spring boot magic we will look at more about spring boot starter projects we would understand what spring boot start a parent is and how screen boot compares with spring so would start up with a couple of hands-on steps and then we would move into understanding all that you need to know theoretically about spring boot does it sound like a plan let's get started with step two one now in this step we will set up and launch a spring boot application with mavin and eclipse all details about step one are in this files step 1 dot MD key Capcom slash into in th minute springboard for beginners this is it page and if you go further down you have a step 1 dot MD if you open that up you see all the details about what we would be doing cheating this step basically what we would be doing is setting up a springboard project so what would be doing is you'd be setting up a mavin project with Eclipse that's very easy if you want if you want to use ID like inclusion the link for how to do that is here after that we would include all the dependencies that are needed for string boot if you don't understand the terms dependency that's not a problem we'll look at it as well and then we would launch your first spinning boot application let's get started with setting up a maven project with IKEA when you launch up Eclipse for the first time it would ask you for a workspace workspace is basically a folder where all the files belonging to your project are stored select some folder on your hard disk as a workspace folder and click OK and then you would end up at a page like this this is basically the starter page for Eclipse Java IDE one of the most important things is you need to make sure that you have a Java EE ID you can close this and let's get started with creating our first maven project so the way you would create a maughan project is file and say new you want to create a mavin projects of file new mavin project and you want to create a very simple project so because this is one of your first mount projects out choose creating a simple project without really selecting a mountain archetype you can use the default workspace location so that it stores the project details in this same folder as the workspace you can click Next to start off we will need to give a name to our project so the group ID and artifact ID are basically the way you'd give a name to your project just like you'd give a package name and a class name to every class that you create similar to that any mavin artifact that you would create these to have a group ID and an artifact ID I'll just come in 28 net spring boot at the group ID and because this is your first Springwood project I'll just call it for springboard project as the artifact ID I leave the ocean a snapshot and the packaging as charged and all the other stuff I can leave it as it is and I can go ahead and click the finish button so this basically create a Mallon project and you see eclipse would open that project up when the project is first created you'd see all these folders in the project Explorer project Explorer is on the left and this shows you all the files in your project so what we have is a folder called source main java source main resources so stress Java and sorcery resources all of these are empty for now and you have a file called pound or XML polar XML is basically the configuration for mavin so palm stands for project object model and this is where we define all the dependencies of art project so smell Java is the folder where you just have your source code and so stressed Java would be where you have your unit tests or your integration test when you open up the pounded XML this is what you would see so this is basically a simple XML the most important things for us are group ID that's exactly what we entered coming 28 minutes spring boot artifact ID and version these are exactly as we entered them that's cool you might ask a question why do I need Marvin as we know Java web applications typically we would use a number of charge files these are typically called dependencies for example because it's a spring project I might need spring jar probably I'll be using logging so I need logging log4j HR or some of the SLS for Jade dogger or some logging job before Marin the way we used to do that was to download the jar manually and put it in our project workspace mavin offers a better way of doing that called dependency management for example let's say I want to down I mean I want to include Spring core jar in this particular project all that I need to do is find out what is the dependency name for spring core so for example if you search for Mao and dependency Spring Framework one of the results which comes up is spring course so I just click that I want to use the four point three point five degrees and over here is the dependency name for spring core so I copy that and I would include it in here so I'm basically what I'm doing is I'm including the dependency before we include the dependency we need to include create the dependency section so let's go ahead and do that dependencies and there we go so that's good the things you can see is that the group ID and the artifact idea identify this specific thing and we are trying to get a specific release so you can have multiple versions of this specific jar so spring code you would have a lot of versions we are trying to get one of the latest for dot X versions for a 3.5 degrees I am not really saved it in yet so if you look at mam and dependencies there is nothing in here right now so demand dependencies is right now empty so now I'm doing a ctrl s save and you can see my own dependency on spring code comes in also you'd see that comments logging is brought in because spring core needs commonly logging spring core users comments logging so comments rubbing is a transitive dependency for us so because we use spring : spring corn it's comments logging it gets it in that's the reason why we use maven in this specific project as well as I mean kind of Mowen is kind of the most used build tool we looked at one of the advantages of mammon which is dependency management there are a lot of stuff that Mao and helps us with we'll look at all that stuff a little later for now let's focus on creating our first spring boot application we would want to launch and run it in Tomcat there are five steps actually there five total steps launching application with spring boot what are the first ones is to add a spring boot start a parent so over here if you look at it we are specifying a release so we are saying 4.3 dot v dot release one of the great things about tata parent is it manages the dependency versions for everything so once we include the spring would start a parent in we don't need to specify the version anymore that's one of the advantages of spring would start a parent so we start with including that and then we would include the spring would start a web one of the things with typical projects before spring boot is that if I need to develop a web application let's say there are a lot of things a lot of jobs I would need litter I'm using spring MVC without using spring boot then I would need to include a lot of dependencies manually to get all the functionality working spring boot all that you need is to include spring boot data web so this book spring boot starter web would bring in all the stuff that are needed to develop a web application we look at how to do it the other thing is we want to use Java version 8 so we will configure that and also we would create a spring boot plug in the spring boot plugin would help us to launch the springboard application the last one is to create the Java file which is called the application launcher those are the 5 steps which are involved let's start with the first mini step which is to configure this spring boot startup parent to make it easy we have all the files present in here so if you go to step 2 1 dot MD I'm a spring good for business and step 1 dot MD it contains the pom dot XML the configuration of parent is right here so I just take it from here or you can manually type it in so if you look at the artifact ID for the a parent it spring boot startup parent and the group ID like most of the spring boot stuff has the same croup ID it all spring framework dot boot and we are going to use the 1 dot 4 dot or dot release 4 enough so you can just copy this parent in so let's go ahead and use that sure now what I'm doing is let's remove the dependency on spring core I mean this is not really needed we'll get it in some other way I added in the parent so this spring boot start a parent is added in the spring would start a parent provides a lot of stuff it provides the default plugins it kind of provides the versions versioning for like we said we want to use the 1.4 dot or release so this makes sure that it provides one dot product release as the default for all these starters we'll look at that in a lot of detail a little later if you look at the spring would start a parent I saved it and now the heck I can go ahead and say right-click mavin update project you'd see that now the Java now we are using GRE system libraries Java SC 1.6 the second thing we wanted to add in is spring boot starter web so we wanted to add this spring boot strata web this thing would start a web is kind of the parent starter for developing any java web applications so if you want to develop web applications with spring boot which start I should you use you should use this data where it kinds of things in the web framework the cost of the logging stuff and binding stuff and all the other stuff that you'd need to develop a typical web application with Java or with spring boot so the dependency for that is here I'm having the dependency so now I bring it in so I'll put it in dependencies I've copied the dependency for spring would start up web in here as you can see in my and dependencies right now it's empty I have not saved it yet but as soon as I save it you would see a lot of magic happening you can see that the build happens and then it might take a while for you because this might be the first time you are doing stuff so it might take a while for everything to get downloaded but you would see a lot of jar getting downloaded so all that I did was add in the spring boots at a web but you would see that there are a lot of jobs which are coming in here so you have all the spring related jars so you have all the spring related jars the spring web web is the most important jar and you also would notice that I have not added in a version here so I'm participating see I have not added in a version because the parent provides the version the version I would want to use of the spring boot starter is one point 4.0 dot release so that's basically you can see all the things which have been here for spring boots starters all the spring boot versions are one point four point zero dot release once I configure the parent and completely release for the parent all the dependencies all the spring boot dependencies use the same version as the parent I don't need to specify a version individually for each of these starter webs so that's one of the advantages of having a parent in so now spring would start a web brought me all the spring boot stuff it brought me bring it also gives me data binding we look data binding is a little later and you can see that there is an embedded tomcat as well we will run the application very soon you will see that it's running with Tomcat and it uses the embedded Tomcat which is provided in here as well as you can see that there is logging stuff which is coming in here so that a lot of stuff coming in for free I mean app and also you'd see that there are a few auto configure jars we learn what is auto configure later just by adding a simple dependency we got a lot of stuff for free the next thing we want to do is to configure Java version hint so how do I do Java version eight again so it's basically I would want to override the property so properties is one of the features in Marin where you can configure a lot of properties and in parent form so in the startup parent if I open that control and click it will open the parent form look at the Java version which is inside the parent or Tom it's one point six so all that I need to do is define another property in the current pond so basically it's like inheritance I'm overriding the java dot version which is in parent form in the current pond so I would actually go ahead and put a property so I would say properties Java version I want to use one point eight so I don't want it like I want to use 1.8 version of Java okay one important thing is if you are having any problems with the structure of pomerode xml and if you are facing errors and one of the things you can do is copy the entire pound or xml from here so go to step one dot nd copy this as it is and then you should be fine if step by step is working for you that's cool too we configure the Java version to 1.8 and you should see the magic happen very soon we have JRE system of 1.6 configured in here and I will do a right click mavin update project and ideally it should have updated automatically yeah now you can see Chou is e 1.8 so we now have Java 1.8 we have a lot of dependencies coming in we are using startup and the next thing which we want to do is choose a plugin so we want to run the application right so we want like we don't just want to create a project we want to also be able to run and this plugin would be the thing which would enable us to run it so the way you can do that in Milan is by configuring a build plugin so all that I need to do is build plugins so I just at the end just below the dependencies I copy this in this would help us to build a jar build a bar which we can run we look at a lot of users related to the spring boot mount plugin a little later but this would really help us to run the entire application we have our pond or XML ready so basically what we have done in the pond what XML is for mini steps right I mean there are really steps in the big step terminology these have kind of mini steps we added in a starter parent dependency versions of D we added in this spring would start a web drink which brings in all the dependencies that are needed to execute web projects we configure Java version 8 and we configure the springboard plugin the only thing which we need to create right now is a spring boot application launcher so the way we create a spring good application launcher for enough let's copy it quickly in so we have the application got Java here so I just copied this in this is basically a very simple Java file so I'm creating a class called application with a main method and all that it does is launch a spring application context so the spring application is one of the utility classes in spring boot as you can see it in here so all that we do is spring application or run application dot tell us one of the important things to note is in this particular step we are copying a lot of stuff because one of the first things I think which is important for a beginner it's a see a running application once you have a running application up and ready then you can play around with it type whatever you would want but the most important stuff for me is to have an application running once you have the application running you can play around with it like do undo D type something in play around with it and you'd be able to learn things much faster so let's go ahead and copy this stuff in control C and this also gives me a chance to show some of the magic of Eclipse so edit a control C here I copied the code and I'm doing a control-v you can see that Eclipse automatically creates the entire file so all that I did was copy here go to source main Java which is kind of the place where you would put your source code in and do a right-click paste or you can do a control V control C control V we have a spring boot application launcher ready what we have here is a simple java class with a main method the magic happens because of this annotation called add string boot application a lot of functionality that the spring good application annotation blinks in we discuss about that a little later for now you can kind of think as if this is the annotation which launches up or spring boat application so it launches up the application context we pass in the name of the class as a parameter to the spring application got run method spring application is one of the utility classes in the spring boot framework you pass in the application class to it I'm the spring good application I mean whichever class is annotated with spring good application to it and pass in the arguments which are passed in from the may passing to the main method from the command line and then you'll be able to create application application context and run it that's a a high-level overview of what's happening in here we'll discuss a lot in detail a little later about this specific class for now let's focus on running this so let's do a right click run as java application so it's right click run as java application this is basically running a java file so it's almost like very similar to running a simple java file once you launch up the java application and you would see all the stuff which we are saying in the log right now so we have this spring banner spring boot this specific view that we are using you'll see a lot of stuff in the log and also at the end you'd see that tomcat has started on the specific port in here tom get us started on 8080 the application is up and running or there are a few things map which we'll discuss about a little later for now you'd see that we have been able to start up tomcat the magic of the whole thing is the fact that in a pond or XML actually there is no mention of Tomcat neither is DeMint is there a mention of Tomcat in our application of Java so these are the only two files we created and these two files held that to launch a web application in a very very easy way in the step we actually focused on getting an application up and running our focus was to get something running so that we can learn from it until the next step welcome back we are now in step two in the previous step we focused on getting our first spring good application up and running we followed five steps one is to use a spring boot that apparent Tata web we configure Java version eight we use the spring good plugin and we created a spring good application launcher by using the annotation at spring boost application we were able to launch up a tomcat embedded tomcat server and we were able to see everything printed in the log however we did not create any controllers in the last step we have a server running but there was nothing running in it so in the step let's focus on creating something running in there so let's add a rest controller and let's see how the spring good magic happens so let's now go ahead and create a new class so I will go ahead and say right click new class I would want to create a welcome controller let's call it welcome controller what I want to do is I would want to be able to type in localhost colon deported which the application is running is 8080 so I would want to be able to type in localhost colon welcome and I want to see some welcome message in here right now if you see if I use this specific URL then I get an error page this is one of the magics of default configuration of spring boot I mean that call Auto configuration so there is an auto error page already configured you can see that the error page is saying this application has no explicit mapping first slash there is a error happening that is not found status is 404 so that's basically the error and there's a beautiful error page coming up saying that there is a specific error so now I would want to create this URL so I would want to create a controller which would handle this slash welcome URL so I would want to be able to try a controller accepting the slash we welcome so in spring or spring MVC I mean in spring boots spring MVC spring web which everywhere you want to call that the way we would create a controller is by using the annotation at rest controller so at rest controller is the way you can actually create a rest controller and in the rest controller what we would do is quickly create a method so I would want to return a string and display that sling on the screen so I would say welcome and let's just say return a this is great I see something on the screen okay let's say this is the welcome message okay cool so the rest controller welcome controller and there's a welcome that is method created a method and we return something back now I would want to map this slash welcome to this I've created a rest controller but I need to map the slash welcome URL to that how do I do that the way I do that is by using a at request mapping and you can put this in double code so you can put the URL you'd water map in double code you can save it and relaunch the application right click run as it's not the Welcome controller we should actually go back to the application or yes I can do this as well so I can actually launch it by using right click run as java application down here as well at school too so now you would see now have restarted the application let's see if this is printed in the log slash welcome let's search for it to see something is math yep so mapped slash welcome on to welcome controller slash welcome so what is happening now is we have created a controllers we create a controller and with the annotation address controller and created a method in it mapping the URL slash welcome and we are mapping it to the method welcome and this method is returning a string back what happens now is that this specific string this will turn back so this is return back and it will be shown on the screen let's see what happens so now i refresh the page so now this is great I see something on the screen so now let's say I want this welcome message to come from some welcome service so let's create a class called welcome service which is actually responsible for getting the welcome method so and let's say I curate a method here which is public shrink retrieve welcome message or determine welcome message doesn't really matter what the name is but the concept is basically that this is a business service method which is actually getting the welcome message and I would want actually this particular thing to get the welcome message so I'll say return this so let's say there is some business logic in this particular method which says which looks at the time of the day and says whether it's good morning or something or that cancer let's say it's good morning and it returns something back so let's assume that this service has a lot of business logic in it and this is the service which we want to make use of let's add is an external service maybe and things like that so this is a service which we want to make use of and here now if I would want to make use of the Welcome service so the way we can use a welcome service is by typically what we would do is we would say private welcome service service and I can I would need to instantiate it obviously is equal to new oops I want to create an instance of the Welcome service not the Welcome controller so is equal to new service and here now I can say return service dot retrieve welcome message now what what would happen here is that this specific service which is present in here would be called and that message would be returned back let's assume for the sake of simplicity I just had code editing in here let's say this is a complex method so this is a complex method which is having a lot of business logic in it and returning the good morning or good evening or whatever based on the day I would want to be able to run this and see what's happening let's again kill the application start it again ok hopefully the application has started up so they go when I say localhost 8080 welcome it's painting in good morning welcome service here is a dependency of the Welcome controller the Welcome controller depends on the Welcome service to get the welcome so this is basically what we call a dependency let's try and differentiate between dependency in terms of jars and classes right so mavin maintains dependencies in terms of jars so whatever you put in the porn or XML our jar dependencies so whatever jars the application needs to be able to work fine so that dependency management which mavin does it basically maintains the jars so it manages what jars I need it goes down loads them and makes them available for me here the dependency that we are talking about is another class that I'm making use of so the Welcome controller class needs the Welcome service class or a welcome service could have been an interface as well so there can be a interface welcome service that I would need for me to be able to function well so that's what is called a dependent when we do a new welcome service and create the dependency directly so this welcome controller right now is creating an instance of the dependency by itself so it's actually saying ok create a welcome service myself I'll handle everything myself I'll create an instance of the dependency and work with it what would happen if I would want to unit test this with some other instance of the Welcome service now that it's creating it by itself it would be very difficult to replace it with anything else and that's where frameworks like spring come in so with spring what we would do is we don't want to do new welcome service so what we want to do is we would want to have spring manage the specific bean so we want spring to manage this so we want spring to create an instance spring to manage this bean and create an instance of this and once the Welcome service is created we would want it to be injected in here so injected over here so what we want is we don't want to do a new welcome service what we want to do is we would want to be able to say just welcome service and some way we would want some framework to create an instance of the Welcome service and inject it in here then what would happen is I can create different varieties of welcome service let's say welcome service was an interface I can create multiple implementations and based on whatever implementation is available the Welcome controller can work with it I would want to ship the responsibility of creating the Welcome service to a deep framework that framework is what spring is now how do I tell string that this is a component which it needs to manage so spring needs to understand that this is a component that it manages so how do I tell that it's by putting something called at service or you can use the call to parallel annotations as component or at service I can use s component or H service I say H component and say certain of spring knows to manage this bean so spring says ok I know how to manage this so whenever you start the application what would happen is spring would automatically create an instance of this bean and be ready with it what we would need to do is we would want welcome controller to use this as well so whatever instance is created we would want to inject it in here so that's called auto wiring so Auto wiring is basically the process where spring creates the beans and injects them wherever they are needed now how do I tell Spring Framework that this needs to be out of wild so that's basically so Auto Wyatt so I bring in the annotation at Auto wire so now I'm not doing is equal to new welcome service now spring would take care of creating this welcome service and auto wire it into this welcome service here and you'd see the magic happen so let's restart the application and see what happens no problem good morning is displayed just to make sure that the right message is displayed I'll say good morning updated just to be very sure so let's restart it again to make sure that is really coming in from the updated application so good morning updated ok cool so we get good morning updated that very cool during this step until now we created at rest controller one rest controller called welcome controller we returned we created a dependency for it so we created a component welcome service and what the why I did it welcome controller is telling that welcome service is a dependency for me how does it tell by saying at Otto wide so what are spring - spring searches for welcome service spring says ok I'm going to search for welcome service and try to find it and auto wire it in how do I tell that this is a comp I mean this is something which spring should manage that's by putting the add component add annotation on it what would happen when I start this application is spring would create welcome service because an ADD component spring would create an instance of it spring would also look at add controller crest controller and all these kind of annotations and create try to create instances for it as well so it tries to create an instance of welcome controller but it would go in and see oh welcome controller need something something needs to be Auto ID in that's a welcome service so it will see the Welcome service create an instance of it or to wire it in here and then it would launch at the Welcome controller and when I will go to the URL it would use the Welcome service automatically that's a lot of ground that we covered change the specific step what we'll do is we'll take a break down here and meet you in the theory section of this step where we would discuss more about auto wiring add component and component can if you are familiar with the spring framework then you can probably skip the theory step and directly move into step 3 until the next step where I would see you alright in the previous step we talked about spring being a dependency management framework some important questions related to Spring pop up in your mind right so how does Spring Framework no way to search for the bean so created at add component in here we so we created a welcome service and we created a atlest controller how to spring know to search in this specific package for this specific class does it search through all the classpath what are the annotation spring looks for so we looked at couple of annotations that spring useful right so at rest controller as component at Ottawa these are some of the annotations which spring looks for so are there any other annotations of spring looks for and what if spring does not find what it is looking for so what if there is no component with a specific thing so let's get started with the last question first what if spring does not find what it's looking for let's go to the Welcome controller and let's say I remove the annotation add component from the Welcome service so I remove the add component annotation from the Welcome service so now this welcome service does not have the annotation add component let's start the application ok cool so you're seeing now that spring failed the whole thing so let's copy this message what the error message so that we can see it more clearly so what if spring does not find the annotation so what it does is it throws an exception and it stops the server from starting up so it's saying no qualifying beam of type welcome service found for dependency welcome service so what it's saying is I'm looking for a dependency welcome service but there is not even one beam which is defined of that particular type if spring is looking for a specific dependency and it does not find a matching component service or something of that kind then it will to an exception and it would stop the server from starting up completely so that's what would happen if string does not find what it is looking for it would throw an exception and stop so if you at any point you'd see no such beam definition exception anywhere so it most probably is because you didn't put an add component or a net service that's one of the questions answered first question how does Spring Framework no way to search for beans to manage to answer that what we do is we would actually take the Welcome service out so what I would do is how take the Welcome service and I would move it out cut it and I'm pasting it here now I mean this is class so I need to make it public class welcome service because it's now it's in its own class so I can make it public I'll add back the add component that we removed earlier so let's add it back in and if you start the application right now then you would see that everything is working fine everything is working as expected that's what you'd see if you restart the application right now all that we did is we move welcome service out of the Welcome controller into its own class and made the class public so what I'll do is I'll also change the package name so come in 28 minutes I'll change the package name to come in 28 minutes and let's say service you can press command 1 or control 1 and eclipse would give you options I would want to move welcome to welcome service dot Java to that package now you can see enough that this service welcome service is moved to that package there's a compilation error on welcome controller because welcome service is not in the same package so I can organize the input control shift or you can say right click or has inputs or find a way to input the Welcome service in so now the application is compiling find all that we did is now first opposed we move welcome service from Vericom controller outside into its own class now we move it into a different package called dot service we are still having the add component annotation on it so spring can look for it and find it but will it be able to find it now because it's in a different package let's see I'm restarting the application ok same error again so it says I'm not able to find you what's happening here why is an exception being thrown or the same exception no such bean definition found exception being thrown I have at component defined so that's good how do I get this to be working let's try one thing I'll move the Welcome service back to the spring boot package ok let's now restart the and see what would happen aha this is starting up fine when I move it back welcome service I'll move to the other one so so is package and stop it and restart it aha failed basically what is happening is if the Welcome service is in the package calming xx next order please spring boot is being picked up if it's in the package come in 28 minutes or service it's not being picked up why that magic can be explained by component scan so in any spring boot application what would happen is when I have a spring boot application annotation in a specific thing what would happen is spring boot would automatically do a component scan in this specific package SOCOM in 28 minutes spring boot it would search this particular package for all these stuff so it what does spring would do automatically is to add a component scan on this particular package so what does the components can do components can would search this package enter package for any classes any component any service or anything of that kind so that's what is called a component scan but welcome service is outside this package it's not in the package come in 28 minutes spring boot or any of this child packages it's in a different package coming 28 minute service so this version service is not being picked up so if I want the Welcome service to be picked up then I would need to add a component okay let's now go to the spring boot application so I'm opening up the application or Java and now I would add in these components can so I want to components camp I would want to not just scan this specific package in 28 minutes spring boot but I would want to actually scan the entire now obtain the component scan to comm got in 28 minutes and let's run it right now application is startup server is working so now welcome service is also being picked up basically how it works is very very simple so components can defines where spring looks for components where does spring look for components is defined by the components can once spring does a components can click it tries to create instances of all the components if on any of the components there is an ad Auto add annotation then that's basically a dependency so it will look through the components to find a dependency which matches that so if it found then it would auto buyer it in so that's how the entire component scan and auto wiring have works with Spring Framework so if spring does not find something then it would throw an exception to go back to the questions that we were discussing about at the start of the video how the Spring Framework move ways to search that's through the component term the great thing about the spring bootie application annotation is by default it would always look in that specific package so by default it would so as soon as I it added an annotation at spring boot application it would add in at component scan on the current package if you all the components that you have are in the current package directly then you don't need to have components can defined explicitly but if they are not then you would probably need to define a component term of your own what are the annotations that spring looks out for so there were a couple of them we already discussed add component is one of them add service also is one among them there are other ones at controller at rest controller is another one which spring picks up and create a component so one more thing is more related to the data layer it's called at repository so these are all the stuff which spring looks for and picks up directly at res control s kind of an inheritance wet cress control is basically extension of the add controller so looking at it these are the four ones which Spring Framework looks for there you go that's a lot of discussion on spring we understood what a component water service what is auto wiring and we looked at components can in detail as well I hope this was useful I mean especially if you are new to spring then probably this use video would have been really useful for you until the next step all right in the previous steps we created a simple controller and got started with developing a simple application with spring boot which are how easy it was to get started with spring boot and also using the theory of the step 2 we looked at some of the important concepts in Spring Framework now let's shift to this step 3 where we would start understanding the magic behind spring boot pute start with one of the most important starter projects in spring boot spring boot starter web we would look at what is happening behind the scenes with spring boot strata web we saw that Tomcat is the default embedded container so we saw that Tomcat server is being started up how does it get started up and also we would look at what is happening beneath the hood of spring boot with spring boots data web a previous step you of playing around with the Welcome service I'll move it back to the stage common 28-minute spring boot I'll lead the service as well package and now that we deleted the service package I don't need the component scan anymore there you go I remove the components can from here as well as long as inputs as well that's good we now have a running application spring boot start a web is one of the coolest starter projects with spring boot and it's also one of the most frequently used um this is commonly used to develop either rest services or typical spring MVC based web applications now it is this is mostly used for rest web services because resources are becoming very very popular so let's look at the following aspects a spring boot data web what version of spring would start the web is used and then we'll do get what are the dependencies that spring good starter web brings in and also there is a lot of Auto configuration that spring boot strata web brings in so we look at what Auto configurations spring good starter web brings in let's get started now place where we would actually configure this spring wood strata web is yeah it's a bound or XML so we have this spring good starter web configured as a dependency that's the only dependency that we have as of now so that's the only dependency that we have in this project when I actually do a control and click this when I do a ctrl and click our command and click this now in command and hover over that it brings me to options open manage location for that and open palm directions first let's take the first option open manage location this opens up the parent form that's basically displaying boot dependencies form where it's managed diversion of the spring would shatter web which is being used is one point 4.0 release actually this comes from the fact that we are actually using one point four zero threes of the parent so basically the dependency version management of this is done in spring boot dependencies which is actually directly used in spring boot data build so we get the dependency I mean I know summary we get the version from spring boot startup period and what are these different dependencies that spring good starter web brings in to look at that we need to go to the format xml for spring boots data web so open the format XML so spring wood strata web and it opens up the form now I go in so as you can see all the starter projects have the starter spring boot starters as the parent and here all the dependencies are declared so all starter projects are dependent on spring good starter and you can also see that it defines a default thing on Tomcat the default embedded server embedded container is Tomcat so by default if you don't say anything else spring boot starter web would include spring would start Tomcat and then we have the hibernate validator which is kind of the default implementation of the validation API when auto binding is when binding is done and you want to validate that's the thing which happens so other than that there's a Jackson data bind shrink web and display exam MVC that's a swing and VC framework do round up the whole thing so these are all the dependencies that string boot data web brings in as the description says this is a starter for building web including restful applications using spring MVC it uses tomcat as the default embedded container the next question we would want to answer is what Auto configurations that does spring boot data web drinks in before that what is Auto configuration basically what a configuration is very very cool stuff let's say I'm developing a spring MVC web application then by default I will definitely configure a dispatcher servlet earlier when we developed applications with spring we need to manually configure dispatcher servlet in the spring boot application until now we have not configured dispatcher servlet at all how does this patch a servlet come in that's to auto configuration earlier we created this service for localhost 8080 slash welcome good morning updated so that the service that we created let's say I type in a URL which it does not there there is a default error page which is coming in so there's a white label error page which is being provided by this framework directly I am not implemented any error page at all so all that I did was took the basic things that Springwood provided and created a service and started this error up when I go to a wrong URL it says this is the status it's not giving me a 404 and a bad I mean page not found kind of a stuff it's really giving me a good error page I mean this can be improved but this is giving me something to start with so this is another example of Auto configuration spring boot looks at the frameworks that you have on the classpath so I am including a lot of frameworks so basically when I bring in spring would start a web what it does is it brings us a lot of dependencies into this class path right so there is a lot of stuff that comes into the class path spring AOP spring beans jackson data bind hibernate validator the Tomcat and where it's server logging and things like that what the spring boot auto-configure dot jar does is it looks at all the stuff which is in the class path and auto configures a lot of things by default some of them are error handling dispatcher servlet web jars and things like that so specific to spring boot start a web what is the auto configuration that comes into play to find that out we need to open up this buuut auto-configure 1.43 store charge so this isn't mam and dependencies spring boot auto-configure dejar and then we would need to go to web so all the auto configuration stuff is specified in here so if you open up the package all spring framework boot auto configure web these are all these stuff that are Auto configure not all of these would be Auto configured these are things which spring boot or the configure would try to configure so it will try to create a track I mean it would try to create an ever controller a view resolver it would try to create a default message converters and things like that let's try and look at the log let's try and start at the server and see what are the things that are Auto configure some of the things which are Auto configured if you look at the log dispatcher servlet so dispatches abilities automatically assigned a route so it's Auto configured same thing with the character encodings filters and the form content filters and things like that so there are a few filters which are getting order configured the details are not important for enough and also you would see that there are error things that are getting automatically map shows map / error map and you see that also web jars are getting auto configured so these are some of the things which are automatically configured so even though we did not configure dispatcher servlet manually dispatcher servlet was Auto configure if you want further more details about the auto configuration which happens one of the things which we can do is to start this over with debug blogging so how do we start this over in debug logging one of the things bring boots start to look for when it starts up is a property file by the name a plethora party so let's go ahead and create that in source main resources so I'll say file new types a right-click and create a new file I want to call this application dot properties be careful with the name it should be exactly matching application I have a type of your application but properties and safe finish you can actually copy the name from here as well it's in the useful snippets of step zero three and this is exactly the application of properties and I would want to start this spring framework up in debug mode this is how you do that so it's logging level or got Spring Framework in debug so actually if I do this then all the frameworks all the packages would be in debug mode I just want this spring framework to be in debug mode so I just do logging level org framework to be debug and now let me start the application again wow there's a lot of log being printed that's a lot of log actually if you search through the log there would be something called an auto configuration report it would report all the positive matches so it's saying dispatcher servlet Auto configuration is a positive match so it's searching for a class so dispatcher servlet Auto configuration what does it do its conditional on a class if this class is found in the class path then the dispatcher servlet Auto configuration would be configured so similar to that you can see that there are a lot of Auto configurations which are being matched it's also configuring the embedded servlet container Auto configuration it's seeing it's basically searching for either conditional on a class so if some class is found on the class path conditional on a beam if there is a specific bean string being available conditional on a missing beam if that specific bean is not available you can see this and you can actually see there's an error controller that is being configured there's error view conventional error view resolver that is matched and a lot of things right then and there so there is a jackson auto configuration for mapping for object mappers and things like that so you have a complete auto configuration report which is being printed out here including the negative matches so ActiveMQ auto configuration act if for this auto configuration to be working I need to have active entry on the class path which is it's not there so it will not match so it will not be executed basically if you look for the auto configuration report it would show you all the auto configurations which are done by spring boot and all the things which are not done by spring boot and this would come up only if I start this were up in the debug mode so logging level debug and then you have on the entire order configuration stuff printed out this was a very interesting step I would think I mean there might be a few things which you did not understand that fine because a lot of Auto configuration stuff is advanced all that you need to understand is the fact that spring boot brings in a lot of things automatically the way spring boot brings in these things is by spring boot Auto configuration and in this step we looked at spring boot data web we saw all the different dependencies that spring boot starter web brings in and also we looked at some of the auto configuration which it brings in it's very useful to understand the fact that you are not just using spring good starter web but you also understand chapping in the background that's what we try we are trying to do in this step if you're a beginning programmer and some of these things do not really make sense that's fine or the course of this complete course I'm sure you'd be able to completely understand these things have some patience keep hanging in and until the next step all right after long steps 1 2 & 3 welcome to this step fold which would be a really short one intercept we would understand start a plane parent and we would look at how things are like how you can overwrite some of the things we shall define in this starter parent and also we look at a few other startup projects so let's get started with understanding the startup parent what does Springwood start a parent really defined so it basically defines or diversions of the dependencies that's what it defines and also it defines a few default plugins and also it defines things like Java version this is exactly where the starter parent was defined in our contradiction also if you open up the format xml we have a parent where we have the starter parent in here so if you control click or command click on that you can bring up the definition of that particular Pounder XML as you can see one of the first things that we were talking about is dependency versioning the way dependency versioning is defined is actually string would start a parent inherits from spring boot dependencies and spring boot dependencies is where all the dependencies are versioned spring boot dependencies defines all the versions of the different things so you can see activemq version and learn one of the great things about the springboard project is the fact that spring boot make sure that all these versions work together really well let's say I'm using a specific version of spring weather a specific version of XML API is compatible with it or not I'm always not sure if I use spring boot then I don't really need to worry about it because spring would make sure I mean spring boot would make sure that the right versions are in here so I don't really need to verify whether a specific dependency is matching with it or not so let's say when I upgrade from one version of spring to another version of spring all that I need to do is upgrade my spring boot version let I am moving from spring boot one point four to two point zero I just changed from one point four to two point zero and I would get the latest versions of all the jars which are compatible with each other I mean that's a lot of magic actually that that would really make things easy I mean I have gone through a lot of job hell there I had to choose between a lot of jars I mean it it's not an easy thing and now that spring boot does it for us it's going to be really easy this is how spring board dependencies actually does the dependency management for us so spring would start apparent we get all the dependency management through that it's a hell of a big palm file it's about two five eight full dependencies form okay let's get back to where we were let's get back to the start up parent you can see the other things which are defined in here are defaults for a lot of plugins so the defaults for the fail-safes plug in the jar plugin the Surefire plugin which translators who are plugin all defaults for the plugins important plugins are present in the spring would start appear in message as we discussed earlier one of the other things which is present in here is the beach version with java so by default it 1.6 we saw how to overwrite that right so we basically created a property Java version one point let's say I want to overwrite the version of something then overwrite something which is already defined in the parent pond so what we have to do so let's pick up hibernate validator so we are using five point two point four final version but I want to use a different version of hibernate validator so the way I can well overwrite that is by updating the property for that so let's say I want to overwrite that so I already copied the hibernate validator dot version property from that particular pump so if you click the parent form and go to dependencies and go to the Pamir XML there's a property which defines the version for hibernate validator version in the dependencies so what I need to do is just OH copy that property file over and change it now I want to use five point two point three dot finest you would see that I am using hibernate validator five point to the three rot final so this an example of how you can override stuff which is defined already in the parent form and we already looked at the example of overriding the Java version this a couple of examples of how you can override stuff but I don't really recommend you to override things like the dependency version because spring would already make sure that all of them are compatible they shouldn't really be no reason why you have to upgrade to next I'm an upgrade to a version one of the options you should consider always is client upgrade instead of using one point four point two zero release try and update to a higher version of spring boot framework it sells so let's say update to one point four point three one point four point four or one of those kind of things rather than trying to manage a specific question of the dependency by yourself we looked at a few important things that are managed by spring bootstrap ailment so dependency versioning plugins and Java version and we also looked at how to overwrite things defined in a startup period so we'll quickly look at other startup parents which are present so some of the other startup parents which are present our spring boot data Web Services the one which we are using a spring boot strata web until now the start of web is used to develop rest a web services and also to use it's used to develop web applications but the spring boot starter web app web services can be used to develop web services based on soup so if you want develop soap web services then this is this best starter that we would recommend the other one is spring boot start a test this is to be able to write unit tests and things like that so spring would start test brings in things like j-unit mockito and also this spring unit test framework so it would bring in all such dependencies and it would be a great tool to use to write your test spring good starter JDBC would bring in the basic JDBC and the spring JDBC as well and spring would start the security would bring into pig security along with the auto configuration of that and there are other cup few things like spring good data JPA which is on top of the JPA it provides a few interfaces which makes it easy to query data from the JPS stuff so we look at bring boot data test spring would start a security spring boots at a data JPA and spring boot started data rest very soon in the next steps so spring would started at arrest kinds of makes it very easy to expose rest services I mean you just need to create the basic entity beans and spring boot data rest makes it very very easy to expose your services on top of that some of the starter projects which might you might want to take a look at more information about the different starter projects is present in here so you can click in here and you'll be able to see a host of spring boot starter projects the idea here is to just to give you an overview of the different starter project which are present in here I'm sure it's there are so many Center projects that it almost impossible to cover them all I hope I gave you a good overview of spring would start apparent we looked at how to overwrite things that are defined in the starter parent and we got an a little bit of overview of the other starter projects that's all for this particular step and the Linux Heather welcome to the step 5 of our course on spring boot for beginners in this step we have two main objectives first one is to understand spring boot what's the spring what are the goals of Spring Framework what are the goals of spring boots how do they compare against each other there is a lot of confusion around this so we would want to clear that before we go any further next objective is to make sure that we understand what spring boot is not there are a lot of misconceptions about spring boot so some people say it's a code generation framework and things like that which it's really not that's the second objective to understand what it is really not and what are the misconceptions let's chat with the first objective spring boot versus planed what is spring spring is basically a dependency injection framework right so whole concept of spring is dependency injection and things like auto wiring component component scan this is the fundamentals of this spring framework the other thing with Spring Framework tries to do is reduce the plumbing code in enterprise applications let's say I want to write JDBC code JMS and things like that I have to click before spring I had to write a lot of code to be able to do that now with spring you don't really need to do that I mean that really your spring if we take a step back in history and go to the first half of the 2000 decade let's say 2000 to 2005 ej bees were ruling the application java application development world but the fact was that easy bees were not easy to develop I mean they had a lot of XML and plumbing code and a lot of stuff that you had to do before you Gabey running add it to that it's impossible to unit test them all it's very very difficulty unit tells them if I really wanted to test some business logic the only way I can test that is by deploying it into a container and even the alternatives to Egypt is let's say I have to write a simple JDBC code those who are also very like it they all involve a lot of code as well let's take an example so what you see on the screen is an like simple JDBC example you can see how much amount of code is there to just insert a simple to do so I have to create a prepared statement and before that I have to setup the connection and I'm not showing this connection setup code in here but you can see like there's a lot of coding world do you have to do exception handling and things like that these were like the realities of the application development around that time period and this were the things each Spring Framework wanted to solve so Spring Framework came in with the aim of making Java EE development simpler so what was the goals of this Spring Framework one of the important goals was to make applications testable that's basically easier to write unit tests and that where dependency injection comes in things like this like auto wiring like a business service using data service by not creating an instance of it but through auto wiring before spring we used to just say to do be data service is equal to new to this data service I am PL and it's so it's hard coded I mean it's basically tightly coupled and therefore I can like it very difficult to write a unit test but which spring with auto wiring making this a component I can use that mock mocking frameworks to write a test for it so if some business logic breaks my unit test would start failing that the first goal make applications testable that's easier to write unit tests and the second one was to reduce the clumping code of JDBC and JMS if you look at the spring JDBC it made it very easy to write things lipstick example of JDBC like it made JDBC easy by creating something called a JDBC template and creating an abstraction around it so now if I wanted to insert something it's as simple as that so I just need to do this instead of writing a lot of code like this so not just JDBC I mean spring came up with n number of modules which helped reduce the JDBC code JMS code among a lot of other stuff this also led to the architecture being really simple with eg B's you needed a container I mean you need a EJB container with spring it's lightweight so it just a simple web container should be sufficient other goal of Spring Framework was to be able to integrate well with other frameworks so things like like the popular frameworks around that time like hibernate struts explained had good integration with aalam as we started developing applications with Spring Framework or we saw that they were really testable because if we made use of dependency injection then applications became testable but what was happening was there was a lot of configuration a lot of XML configuration or a period of time Java annotations came in and it they kind of reduce the amount of configuration involved but still there is a lot of configuration that is there with typical Java spring applications configuring a lot of beans and let's say I want to integrate spring with hibernate ad to configure a lot of stuff are to configure data source I have to configure station factory and a few other things before I would be able to get started similarly let's say I want conduit spring MVC a I would need to consider dispatcher servlet I need to configure view resolver I need to configure error handling and a lot of stuff like that you would see the typical spring application around this time I had a lot of configuration let's just take an exam so this is a simple configuration file from my test application that we developed a little while ago and you can see it's a lot of configuration and I mean this is just a simple example typical configurations ran in clam and I was working with applications which had like thousands of lines of configuration these configurations over a period of time can become difficult to maintain that's one of the things which was evolving over this period of time that the configuration files would showing large and large and the other thing was also there was a focus shift from monolithic applications monolith applications are large applications with hundreds of thousands of lines of code so the focus of shifting from one of these applications towards microservices to a smaller vacations so instead of having one big application before though shifting towards having ten smaller micro-services and things like that we needed to be able to start a project very quickly the case where if you have one big project then you can spend a lot of time in setting it up configuring it choosing the framework setting the frameworks up and things like that but when you are developing a lot of small applications you don't want to spend a lot of time with setting up projects right so we wanted minimum or zero startup time we wanted to be able to quickly to things like framework set up we would want to be able to deploy applications to multiple environments easily that basically means I should be able to configure the databases or contribute in my connections with other services in the application we run months easily I want to be able to do things like logging transaction management and monitoring of applications very easy so that's kind of what was happening in the last let's say ten years two things which we want to highlight is the fact that a there was a lot of configuration that was involved B since this shift was towards more micro services we need to we needed to be able to start developing applications much quicker that way that's the kind of world where spring boot came in so the if you look at the springboard website it's a spring boot makes it easy to create standalone production grade spring based applications that you can just run so the focus is on creating applications that you can take and put them in production and start working with that that's the focus of spring boot so they take kind of opini ated view of the spring framework spring platform if you can call that to it they take a lot of assumptions and obviously they give you options to override those assumptions but they take a lot of assumptions so that you can get started very quickly so let's say I want to integrate with hibernate so as soon as you add hibernate to your pond or XML spring boot comes in and tries to auto configure a lot of stuff so it says okay you add Spring Framework in and you have hibernate in so probably you wanted to connect to a database so I will create a data source for you and I would create a session factory for you and all that kind of stuff so you don't really need to configure all that for them spring boot configures them for you most important goal is to provide a quick start for projects which Springs you want to develop an application with spring you want to use the dependency injection features in spring so I'll provide you a quick stat so just do this and you'd be able to develop applications with spring and also to be apini ated so to take a lot of defaults so make assumptions but also provide options to override them and also the other one other important thing is the non-functional features like if I want to develop an application and deploy it to an environment I need a web server instead of that spring brute provides you an embedded server so you need security you need metrics you need health checks you need to be able to externalize your configuration so spring both provide you options for all this stuff that is mentioned in here the focus of Spring Framework is on dependency injection and reducing clumping code the spring boot on the other hand is to be able to take this application and deploy it into production I need a lot of things around it so I would want to be able to things like embedded service thing like metric cell checks externalized configuration so these are the features that spring boot provides so that's a good comparison between spring and spring boots it objectives that we had for this step is to understand what spring boot is not fussing it's not really an application server or our webserver so that's let's be very clear about it spring boot provides integration with embedded server so it can use tomcat jetty under - all these kind of servers but it's not an application server or a web server it just provides good integration with the embedded service so that you can use them your application it uses them very easily spring boot does not implement any framework so it does not implement JPA it does not implement JMS or any other stuff so all that it does is provide a wrapper around all of them so that you can integrate with them very easily and spring boot does not generate any code it's not a code generation framework there is no code generation as part of springboard all that magic of Springwood happens through Auto configuration which happens at the startup of your application in this step our focus was to understand spring boot versus print remember what are the aims of Spring Framework versus what are the aims of spring boot the other thing which we discussed is what spring boot is really not hopefully this which we discussed in this step are useful in getting a PM map of bought Spring Framework ease and what spring booth is until the next step alright
Info
Channel: in28minutes Cloud, DevOps and Microservices
Views: 425,734
Rating: undefined out of 5
Keywords: java, Spring Boot Tutorial, Karanam, in28minutes tutorial, Rao, Ranga, Spring Boot Tutorial For Beginners, rithus, interview, in28minutes
Id: PSP1-2cN7vM
Channel Id: undefined
Length: 75min 40sec (4540 seconds)
Published: Mon Jan 16 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.