Spring Boot Interview Questions | Frequently Asked Spring Boot Interview Questions and Answers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys ramesh here welcome to my youtube channel in this video we'll go through a few important spring boot interview questions and answers so here we can see i have listed around 18 spring boot interview questions so these are the questions that may appear in spring boot interviews and each and every question i have explained with practical examples okay so this video is quite lengthy uh it is around two hours because each question or each concept i have explained with practical example that's why you can see the video length is you know more than two hours okay well whenever you will give answer to this question in interviews make sure that you will also provide some practical examples or real-time use cases so basically the inter viewer will expect a practical answer from the candidate okay so make sure that whenever you will answer to these springboard questions in interviews we'll probably provide some you know real-time use cases for example let's say you have used some springboard features in your springboot project so make sure that you will give a real-time that project related use cases or you know practical example so that introvert will basically expect those kind of explanation from you okay so in this video we'll go through these few important springbook questions in detail with practical examples like what is spring boot and what problems spring boot solves explain few important spring boot key features what is spring boot auto configuration and how spring boot internal works or explain run method in spring boot what are the different ways to create a spring project and explain these important annotations and what is spring boot status and name few commonly used spring boot starter dependencies what is spring boot starter brand dependency how many types of projects we can create using spring boot explain types of embedded servers in spring boot and how to change default server port and how to run spring boot application from the command line and describe the flow of rest api request through spring boot application and explain spring boot a muc application architecture how to create and deploy springboot warp file to external tonkat server what is spring boot dev tools used for and how to add security in your springboard project and explain springs security after configuration in springboot project and what is spring data rest used for and how you use profiles in your springbook project aps explained briefly all right without further ado let's go ahead and let's dive into these springboot interior questions before understanding about what is spring boot let's quickly take a look into what is spring framework well spring is very popular java w framework for building web applications and enterprise applications and spring framework made java doubly much easier for developing enterprise applications well spring is very popular for several reasons like spring's dependent injection approach you know encourages writing testable code and it provides a powerful database transaction management capabilities and spring simplifies integration with other java frameworks like hibernate or you know starts and other web frameworks and spring provides a webmaster framework for building web applications as well as restful web services alright so spring is very popular for these reasons guys and spring boot is basically built on top of spring framework to basically reduce a lot of configuration required to set up the spring based applications okay so spring boot is basically an extension of spring framework which eliminated the boilerplate configuration required for setupping spring based applications okay spring boot is not you know it's not a different framework guys spring playmark is just you know extension of spring framework and spring boot internally uses all the spring framework libraries all right so apart from that spring boot provides many key features for example springboard provides spring boot starter dependencies and spring board provides auto configuration externalized configurations being boot actuator and easy to use embedded servlet container support so these are the key features that spring boot offers okay and the main goal of spring boot is to quickly create a production ready spring-based applications without requiring developers to write the same configuration again and again so in case of spring framework we need to configure a lot of things right so consider we are developing spring emus application using spring framework without spring boot then we need to do a lot of configuration in spring game usa project okay so spring boot basically addresses the spring you know application configuration problem so spring-based applications have a lot of configurations guys so that is the main problem of spring-based applications okay so if spring application is huge then we need to provide a lot of configuration in spring-based applications okay and that is the problem uh the spring boot addresses okay for example when we use spring muc we need to configure these details for example we need to con configure component scan dispatchers already resolver web jars among other things and when we use hibernate or jpa in in our spring-based application then we need to configure a data source into a manager factory orientation factory transaction manager among other things and let's say if we use cachi in our spring base application then we need to provide cache configuration let's say if we use message queue then we need to provide a message queue configuration let's say if we use no security database then we need to provide a no security topics configuration and etc etc all right so we need to provide all these configuration in a spring-based applications okay so this is the main you know pain for developers while developing spring based applications right we need to configure a lot of things so developers should have to focus on only the business logic not on these external configurations so this is the main problem of spring based application developed using spring framework okay and springboard addresses these problems so what spring boot does is spring boot will automatically configure spring game music you know configurations like component scan dispatcher are related view solver web jazz etc whenever spring boot finds spring emuc dependency in a class path okay so spring boot what it does it will configure all these things for us whenever springboard finds premium uc jar file in a class path similarly whenever spring boot finds hibernate dependency in a class path the spring boot will automatically configure these you know details for us similarly for cache mq and nosql so whenever springboard finds no sql database related dependency in a class path it will automatically configure in sql database configuration all right guys springboard solves a lot of lot of you know problem that spring developer basically you know face so the main goal of springboard is that as a developer you should only focus on you know business logic of your application not on the uh you know not on other stuff springboard will you know provide a lot of features to automatically configure a lot of stuff okay so the main goal of spring boot is to quickly create production ready spring-based applications without requiring developers to write the same configuration boilerplate configuration again and again okay so that is the beauty of springboard you know we don't have to write the same boilerplate configuration again and again okay springboard will automatically configure all the configurations for us whenever it finds a dependencies in a class path so let's quickly take a look into these are the spring boot key features so that we will know more about spring board okay so spring boot is nothing but a just extension of spring framework and we can use bing boot to create a production ready being based application without requiring developers to write the same configuration code again and again all right great so spring boot status well spring boot offers many starter models to getting started quickly with many of the you know commonly used technology like spring muc jpa mongodb spring batch spring security elasticsearch etc so these starter dependencies are free configured with most of the commonly used library dependencies so you don't have to search for compatibility library versions and configure them manually all right so these ping boot starters will take care of you know maintaining the compatible versions and all the configurations for example spring boot starter data gpa dependency so this dependency whenever we add in our springboot application then it will you know it will include all the dependencies required to use ping data gpa along with hibernate library dependencies all right so as a hybrid it is one of the most commonly used jp implementation so spring data gp internal uses hibernate as a jp provider all right so springboard provides this dependency that is spring boot starter dataship dp dependency we need to only add this starter dependency it will internally you know include all the required dependencies to work with spring data gpa okay so one more example is spring boot starter web dependency so whenever we add spring boot starter web dependency in our spring boot project it will by default pull all the commonly used libraries while developing spring average applications such as spring muc webmuc jackson json library validation api and tomcat uh you know server all right so spring boot will do a lot of things for us we know we need to only add the starter dependencies okay so this is about spring boot starters all right now let's take a look into spring boot auto configuration well what is springboard auto configuration spring boot auto configuration attempts to automatically configure your spring applications based on the dependencies that you have added in the class path okay so spring boot auto configuration it you know automatically configures your spring base application based on the dependencies or jar dependencies that you provide in a class path well before you know exploring spring boot auto configuration let's first take a look into why do we need spring boot auto configuration so if you have already worked on spring framework or spring based applications then you have seen that spring based applications have a lot of configuration okay so let me show you a spring based application without spring boot so look at here this is spring muc hybrid crude web application so this is the project i have developed using spring music net okay i haven't used spring boot in this project and you can find this project on my github profile so go to spring amc tutorial and in this tutorial repository you can find spring game is a bunch of crude web application project well in typical spring amusing hybrid project we need to configure a lot many things for example so this is a java based configuration and in application context configuration file you can see a lot of bins created manually for example session factory if we use hibernate in spring image application then we need to configure these three bins that is session factory bin data source bin and then transaction manager bin okay if we don't use spring boot then we need to configure these three bins if we use hibernate in spring abc okay so this is in all the difficult spinning mhc applications okay these three bins should be configured so apart from that we are using uh you know we are using a jsp or timeline for that we also need to configure wave resolver right so that i have defined in webmaster config class so look at here in this project i have your jsp so i have configured internal resource view resolver for gsp so this is a typical bin we need to configure when we use uh you know jsp or time leave as a view a layer so apart from that we need to configure dispatchers outlet so there are different approaches to configure dispatchers outlet like either we can configure dispatchers only in web.xml that is deployment descriptor or we can you know configure dispatches or lit using java based configuration okay so just understand like without spring boot we need to configure these bins for spring mvc project so this is the typical configuration okay you can develop any kind of spring image application and if you are using hibernate then you need to configure all these things when we use spring emuc we need to configure component scan dispatcher select view resolver web jars and other things and let's say if we integrate hibernate or jp in our spring image application then we need to configure data source bin entity manager factory or session factory bin and transaction manager and other things so these are the you know typical uh configuration in all kind of spring mvc application so we can automate these configurations right because these configurations are you know almost available in all the spring muc application i mean in our kind of spring ms application we need to configure these things why not we automate you know these configurations so instead of writing this configuration in all kind of spring image application again and again why not we automate these configurations well spring boot you know team has uh you know comes with this new thought process they have automatically you know configured all these all these configurations so whenever spring boot finds spring mvc dependence in a class path then spring boot will automatically configure component scan dispatcher solution resolver and if we add spring data jpi or hibernate in a you know in a class path then spring boot will automatically provides a data source bin entity manager factory orientation factory bin and transaction manager all right so spring boot auto configuration basically you know configures all these configuration for us whenever springboard finds spring mvc dependency and hibernate dependence in a class path so this is the best example for spring boot auto configuration so the next spring boot key feature is externalized configuration so this feature is very very useful whenever you want to deploy your spring boot application in different environments so basically this feature enables developer to work with the same application in different environments environments can be a dive environment qa environment uat environment or production environment okay so you can use properties file or alm file that is yml file to maintain the you know configuration and spring boot basically offers different configuration file for different environments okay you can maintain a profiles different different profiles for different different environments all right and you can use annotations like add value annotation and add configure configuration property annotation to read the values from the properties file or yml file into spring bean configuration all right so this is a very very useful feature whenever you deploy your same application in different environments okay so the next springboard key feature is spring boot actuator so this is one of my favorite feature okay so in our spring boot application we configure a lot of details all right and we need to monitor all these details right so spring boot activator provides a lot of rest endpoints to you know view the health check and the matrix a lot of things when you can you know view by using spring boot actuator so spring boot actuator provides a wide variety of production ready features without requiring developers to write a much code for example we can use spring actuator to view the application win configuration details we can use spring add spring actuator endpoints to view the application url mappings environment details and configuration parameter values and we can use spring actuator provided rest endpoints to view the registered health check matrix okay so spring boot actuator exposes a lot of rest and points to monitor a lot of configuration details environment details environment url mapping details health metric checks a lot of things all right so this is a very very cool feature that spring boot provides okay great the next spring boot key feature is easy to use embedded servlet container support well this is a very cool feature we can quickly run our springboard application in embedded servlet container okay so traditionally when we build a web application using spring framework then we need to deploy our spring web application in external tomcat server right so first we need to download the tomcat from the internet and then we need to set up the tomcat server and then we need to make a war file of our spring vmuc web application and then we need to explicitly deploy war file in a tomcat server so these are the steps involved uh you know whenever we use spring framework without springboard all right so spring boot made it very easier to deploy our springboot application in an embedded servlet container springboard by default provides tomcat server as a you know default embedded servlet container and whenever we use bing boot starter web dependency then it will automatically include tomcat as an embedded subnet container so whenever we run our springboot application as a standalone it will run in a default embedded tomcat server on port 8080 we no need to create external war file and deploy an external tomcat server so we can make a jar file of springboard application and we can run it as a standalone in amida solid container easily okay so these are the key features that springboard offers well what is springboard auto configuration boot auto configuration attempts to automatically configure your spring applications based on the dependencies that you have added in the class path okay so spring boot auto configuration it you know automatically configures your spring base application based on the dependencies or jar dependencies that you provide in a class path well before you know exploring spring boot auto configuration let's first take a look into why do we need spring boot auto configuration so if you have already worked on spring framework or spring based applications then you have seen that spring based applications have a lot of configuration okay so let me show you a spring based application without spring boot so look at here this is spring muc hybrid crude web application so this is the project i have developed using spring mvc hybernet okay i haven't used spring boot in this project and you can find this project on my github profile so go to spring mc tutorial and in this tutorial repository you can find spring game is a bunch of crude web application project well in typicals bringing a music hybrid project we need to configure lot many things for example so this is a java based configuration and in application context configuration file you can see a lot of bins created manually for example session factory if we use hibernate in spring image application then we need to configure these three bins that is session factory bin data source bin and then transaction manager bin okay if we don't use spring boot then we need to configure these three bins if we use hibernate in spring abc okay so this is in all the difficult spinning emission applications okay these three bins should be configured so apart from that we are using uh you know we are using a jsp or timeline for that we also need to configure view result right so that i have defined in webmusic config class so look at here in this project i have your jsp so i have configured internal resource view resolver for gsp so this is a typical bin we need to configure when we use you know jsp or time leave as a view a layer so apart from that we need to configure dispatcher servlet so there are different approaches to configure dispatchers for that like either we can configure dispatchers earlier in web.xml that is deployment descriptor or we can you know configure dispatches or lit using java based configuration okay so just understand like without spring boot we need to configure these bins for spring mvc project so this is the typical configuration okay you you can develop any kind of spring image application and if you are using hibernate then you need to configure all these things when we use spring emuc we need to configure component scan dispatcher satellite view resolver web jars and other things and let's say if we integrate hibernate or jpeg in our spring image application then we need to configure data source bin entity manager factory or session factory bin and transaction manager and other things so these are the you know typical uh configuration in all kind of spring emc application so we can automate these configuration right because these configurations are you know almost available in all the spring muc application i mean in our kind of spring msc application we need to configure these things why not we automate you know these configurations so instead of writing this configuration in all kind of spring image application again and again why not we automate these configurations well spring boot you know team has you know comes with this new thought process they have automatically you know configured all these uh all these configurations so whenever spring boot finds spring mvc dependence in a class path then spring boot will automatically configure component scan dispatcher solution resolver and if we add spring data jpi or hibernate in a you know in a class path then spring boot will automatically provides a data source bin entity manager factory orientation factory bin and transaction manager all right so spring boot auto configuration basically you know configures all these configuration for us whenever springboard finds spring mvc dependency and hibernate dependence in a class path so this is the best example for spring boot auto configuration okay so spring boot brings in a new thought process like can we bring a more intelligence into this when spring game usa jar is added into an application can you auto configure sim bins automatically okay so how about you know auto configuring a data source bin if spring if hibernate jar is on a class path how about auto configuring edit you know dispatcher servlet if spring machine is available in a class path so this is you know the thought process of spring boot team and they came with this you know auto configuration uh feature like whenever spring boot finds spring mac you know jar dependence in the class bar then automatically configure dispatcher right view solver and other stuff all right this is a very very you know important uh concept in spring boot guys try to understand spring boot auto configuration so just remember spring boot you know auto configuration attempts to automatically configure your spring applications based on the dependencies that you added in a class path and this is the best example like whenever you add a spring much in a class path then spring boot will automatically configure component scan dispatchers or lightweight resolver and if you add a hibernate or spring data gpa then hybrid you know then spring boot will automatically configure data source bin entity manager factory bin transaction manager etc let's understand the step by step how run method internally booster up our spring boot application this diagram shows springboard application execution process well we have a main method right so inside a start class that is springboard main entry point class we have a main method so this main method is same as like the normal java program we use right and we define a main method to run the java program and main method i mean the execution of the program java program starts from the main method right similarly in springboot application the execution of the springboot application starts from the main method and it internally called a run method and all the logic have been written in a run method to boost graph springboard application so bootstrap in the sense run method will load all the you know auto configuration classes and run method is responsible to you know execute all the auto configuration classes and it it create the spring ioc container and it returned the spring ios container that we'll see step by step okay look at here the steps springboard application execution start with start from the main method and the main method internally called springapplication.run method okay main method internally called spring.run method and then and spring application dot run method performs a bootstrapping for our springboard application so this is a very important the run method internally you know load all the autoconfiguration classes and it creates the spring ioc container it will trigger the listeners trigger the runners etc okay bootstrapping means it will load all the necessary configuration and components required to run over springboard application and next step is it will start stopwatch to identify time taken to bootstrap the springboot application so we need a timer to calculate the time taken by our springboot application uh for bootstrapping right so that's pre you know that is done by stopwatch class and then it will prefer environment to run over spring boot application the environment can be a production environment dev environment test environment qa uat environment etc okay if you if you configure any environment in application.properties file then run method will prefer environment for that and next it will print the banner for example if you run the springboot application let me quickly run the springboot application so look at here this is nothing but your banner okay so once it will start the stopwatch then it will prefer the environment and then it will print the banner okay this banner you can customize the banner or you can disable the banner mode and then it will start the ioc container so if you don't use spring boot then we have to manually start the application context or spring ioc container right but spring boot will take care of starting you know spring ioc container or application context all right so in spring boot we can see three types of projects a default project web servlet project and reactive project this this will talk in in upcoming videos just you know take a note like run method internally start ioc container based on the class path okay and refresh context so once it will start the ioc container then it will refresh all the context and it will load all the necessary components and then it will trigger the renders for example in spring boot we have two kinds of runner application runner and command line renderer so these runner we basically used to execute our logic only once during our application startup so just remember these runners and we use these runners in our you know applications springboot application in this course to execute our logic like if we want to save some records in a database then we can use command line runner to you know run our database related logic only once during application startup and then run method will basically return application context object application contact is nothing but our spring ioc container alright so these are the steps in order in springboot application execution process now we will deep dive into the source code of run method and we will understand all these steps okay let's switch to our spring tool suit id let me stop the server okay so all our springboard application execution start from this class so this is called start class or springboot application entry point class so this class contains a main method and main method internally contain internally called spring application not a run method so look at here run method is a static method and we are calling run static method using a class name we are not creating object of this class to call a run method because run method is static so we use class name to call the run method iron method takes two arguments first argument the start class name and then command line command line arguments okay great now let's jump into run method and look at here now we are in springapplication.class and this class contains a run method and look at here run method is a static method okay another method internally called another run method so there are overloaded static run methods okay and again this run method internally called run method and there we go so this is the run method which contains all the steps for example the the first step is star stopwatch starts and if you look at the source code of run method run method internally starts a stopwatch to calculate the time taken by our springboard application okay so this is the lineup code okay run method internally starts stopwatch to calculate the time taken by our springboot application and it will you know declare configurable application context object so this is nothing but our spring ioc container object this is not yet created this will be created in next step okay and it will get all the listeners okay and then it will start all the listeners so these are the spring boot application level listeners okay whatever the command line argument that we pass to run method that will be password to this get run listeners method okay so first step is starting stopwatch and next preparing the environment to run our springboard application and here is here is the logic which will prefer environment for our springboard application if you go inside prefer environment then you can able to see all this source code so this source code is responsible to create the environment okay so based on the web application type it will create the environment all right so at a post i know at the post time you may not be able to understand the source code but just try to understand the steps what does what does the run method you know does behind the scene so once it will prepare the environment the next step is it will print the banner okay look at here print banner next step it will create the application context it means it will create the spring ioc container if you go inside create application context so look at here it will create the application context if you go inside create method and if you can see that it will create the application context or ioc container based on the based on the uh the web application type whether application is a web application or a reactive application or a default application just remember run method internally starts ioc container so if you don't use springboot then we need to manually start the application context okay next step is it will refresh the application context and then it will stop the watch and then it will you know call the runner it will trigger the runners if you go inside call runners it has two types of runner application runner and command line learner so whatever runner we use in our spring boot application that will be triggered here okay great after that it will return a context contact synthetic configurable application context object okay so configurable application context is interface it and internally extends application context and application context is nothing but our spring ioc container okay understood so let me revise the steps so run method internally call stopwatch.start method to just calculate the time taken by our screenboot application then next it will prefer the environment and next it will print the banner next it will create the application context and then it will refresh the application context and then it will stop the watch and then it will call all the runners or internally it will trigger all the runners and then it will return application context object okay understood so very important guys you should understand how spring boot you know application you know bootstrap behind the scene well you can see here these are the three you know simplest ways to create spring boot project so the first way is using spring initializer all right so first way is using spring initializer and second way is using spring starter project in spring 2 suit id and you can install spring plugin in eclipse id and you can get option to create a spring boot project in eclipse itself okay this is the second way and third way is using spring boot cli okay this is a command line interface which you can use to create spring based project all right so spring boot cli is a command line tool that you can use if you want to quickly develop spring application okay now let's quickly take a look into how we can create spring boot project using these three ways so the first way is using spring initializer well look at here star dot spring dot io so this is the you know url for spring initializer spring initializer nothing but a website which we can use to quickly create spring base application so this is the basically a standard way to create a spring boot project and okay and you can integrate this initializer in any id by using plugins okay just remember this is the standard way guys this is standard way to create a springboot project using spring initializer all right so you can use this urlstar.spin.io to access spring initializer and you can go ahead and you can create springboot project all right great so this is the first way and secondly is using springs starter project in sds id well let me show you this this step how we can create spring boot project in spring tool suite id so go to spring initializer so go to spring tools id and in order to create spring boot project in spring two suite id go to file new and then spring starter project okay all right so look at here this is the spring initializer website url alright so spring team has integrated spring initializer in spring to suit id itself all right so if you want to use spring initializer then what you need to do is there are not many manual steps right you first need to create a spring boot project from this spring initializer and then you need to generate the project as a zip file and then you need to unzip it and then you need to import in ide so these are some manual steps and it takes some time right it consumes some time so instead of doing this spring team has integrated spring initializer in spring to suit id itself so that we can quickly go ahead and create spring boot project in spring tool suit id itself okay so this is the second way and the third way is using spring boot cli so spring boot cli is basically a command line tool that you can use if you want to quickly develop spring applications okay so first you need to install the spring boot cli you can install spring boot cli manually by using sdk man manager all right and you can use these commands to quickly create spring boot project using spring boot cli so these are the three you know simplest ways to create a springboard project you will understand one important annotation that is add springboard application annotation so this is a very very important annotation and it does a lot of thing while running our springboard application okay all right and this annotation should be annotated for our spring boot you know application starter class or springboard main entry point class okay this annotation should be annotated for this starter class okay and now if you can go inside this source code of this annotation you can see this annotation internally contains three annotation first is spring boot application spring boot configuration annotation enable auto configuration and component scan annotation all right add springboard application annotation internally contains these three annotations all right let me show you in a diagram so look at here add spring good application annotation is the combination of add spring boot configuration annotation plus at enable auto configuration plus at component scan annotation and at springboard configuration annotation internally uses add configuration annotation so let me show you in a in a source code so look at here this is the add springboard configuration and it internally uses add configuration annotation okay so understood just remember spring boot application annotation it internally contains these three annotations now let's understand these three annotations step by step so that you will be able to understand the usage of springboard application annotation okay as we know springboot configuration annotation internal uses at configuration annotation so this annotation should work as ad configuration annotation now let's understand the usage of add configuration annotation so this is very very important annotation and we use add configuration annotation to configure you know to provide a configuration as job is configuration okay java based configuration so there are three types of configuration uh we can you know we can do in a spring based application right one is xml based configuration all right and second is java based configuration and third is annotation based configuration okay so in order to do a job-based configuration we use add configuration annotation so basically we create a java class and then we annotated the annotate that java class with add configuration annotation and then and then that java class becomes a configuration class and we can define all the spring rated configuration in that class for example let's say i can create a class over here for example let's say spring config okay so this is a normal java class okay it doesn't related to spring now what we can do we can use at configuration annotation on top of this class so that this class becomes spring java based configuration class all right now we have annotated this class with add configuration annotation and now this class becomes a job-based configuration and we can define a bins or we can configure bins inside this class okay understood the usage app at configuration annotation so we use that configuration annotation to to make a java class as a spring java based configuration class and we can define the bins inside this configuration class so next is at enable auto configuration class so this annotation is very very important this annotation enables auto configuration classes for our springboard application so whenever our springboot finds spring starter we have dependency in a class path then this annotation basically enable a dispatcher solid auto configuration class we resolve our auto configuration class and other you know auto configuration classes so this annotation is responsible to enable auto configuration classes whenever our springboot application will find a dependencies on a class path okay just remember this annotation and next is add component scan and rotation so this come this annotation is also very very important guys so we use add component scannation to scan the j you spring components in a particular package for example for example look at here this is the project structure of our spring boot application and this is called a base package for our spring boot application let me create one more package let's say dot sub one and let me create quickly one more package let's say dot sub 2 okay and as we know that this entry point class that is spring boot application entry point class annotated with add spring boot application annotation and it internally contains add component scan annotation okay now what this annotation will do is it will automatically scan all the spring components inside this base package plus its sub packages okay all right so at component scan annotation will scan all the spring components within a base package plus all the packages i mean the sub packages inside this base package a demo is the base package and within a demo we have sub 1 sub 2 so these are the sub packages of demo package right and at component scan annotation will scan all the spring components present in demo package as well as sub 1 and sub 2 package so spring component means the class which is annotated with you know spring provided stereotype annotations for example let me quickly create a class let's say spring component and i will create a method here public string let's say get message and i will return some string here let's say hello world okay so this is the class it has a method get message method and this is not a spring component yet okay we need to annotate this class with some spring provided stereotype annotation for example let's say if we use add component annotation so whenever we add add component annotation to any class then this class becomes spring component okay and this add component scan annotation will scan this component okay during our application startup and then it will scan and then it will register this component in spring con spring application context or spring ioc container alright i hope you understood the blue now what we'll do will get this bin this spring component bin from the application context so component let me rename this okay so go to our springboot main entry point class it has a main method and this is run method so run method returns application context right that is configurable application context and this is nothing but a spring ios container it contains all the spring components now let's go ahead and let's get that application context from the run method configurable application context and let's application context okay now what we'll do spring component component component equals to application context and then we have a get bin method so whenever you want to get a bin that is spring bean then you can call getbin method and there are overloaded getpin methods so you can choose appropriate so i am going to choose first one and then i will just pass spring component here dot class now we got spring component from the application context so here application context is nothing but our spring ioc container and spring irish contain container contains all the spring beans and spring component is nothing but a bin okay because we have annotated this spring component with add component annotation okay so this is the annotation based configuration so now what i'm going to do is i'm gonna put this out here and i'm going to call get message method now let's run the springboot application and let's verify whether we have configured this component properly or not so look at here hello world it means that add components can annotation successfully scanned the sub package okay i hope you understood the usage of at come at component scan annotation so add components can annotation basically scan the base package along with that it will also scan all the sub packages for spring components so here we have a spring component or spring bin and it is parent in a sub one package and sub one package it is i know it is stored in a base package so this is the hierarchy all right i hope you understood the usage of at configuration annotation at enable configuration annotation and add component scan notation once we understood add configuration annotation at enable auto configuration annotation and add component scanner notation then let's go back to at springboard application annotation so as we know at spring root application application annotation is a combination of these three notation so instead of using these three annotation we can use this single annotation that is at springboard application rotation that's why whenever we create a spring boot application using spring initializer by default spring boot you know will add this annotation to starter class or spring boot you know entry point class okay so instead of using three and these three notations we can use this single add spring boot application annotation and so this single annotation will do the purpose of these three annotations okay i hope you understood the usage up at spring root application annotation so this is a very very important and just remember what this annotation does behind the scene so it will it will you know auto configure the you know the class as a java configuration class and it will enable the auto configuration and it will you know scan the components in a base package as well as its packages okay great well spring boot status is an important feature provided by spring boot and we'll first take a look into what is the problem and then what solution the spring boot starters features provides well probably you have already worked on spring based applications right so i am talking about developing spring based application without spring boot okay so when we develop spring based application then we need to maintain a lot of configuration we need to maintain a lot of dependencies right so managing the dependencies in complex spring-based application is really really you know hard for example look at here so these are the dependencies so whenever we develop spring emu's application then we need to add these uh three dependencies right spring webmuc spring orm hibernate validator for form validation and this is only a simple spring energy development uh dependencies so whenever you develop a spring image application and if you want to use hibernate then you need to add a couple of hibernate dependencies along with these spring mvc dependencies and if you want to use other dependencies uh for example if you want to use view layer that is jsp or timeline then you need to add a couple of dependencies okay so basically whenever we develop spring image application then we need to add a lot of dependencies guys and we have to maintain the compatible version okay let's say if we are using spring image application with hibernate then we need to maintain a compatible version between spring and hibernate framework and if we are using other third-party libraries then we need to maintain a compatible version okay so this is a really really hard right and developers sometimes spend a lot of time on you know hunting these dependencies and you know maintaining the compatible versions all right so this is exactly the developer we are facing the issues okay and the spring boot starters were built to address exactly this problem okay well this is the problem and the solution can be like this so whenever we know that these are the dependencies required to develop a spring much application then why not we have a single dependency would which will manage all these dependencies internally okay so basically spring boot status has provided a starter dependency that is spring boot starter web dependency and this dependency internally manages all these three dependencies with their compatible versions and other transitive dependencies all right so spring boot status are a set of convenient dependencies that we can include in our application for example so this is the spring boot starter dependency that is spring boot starter web dependency so whenever we want to develop spring amuse application or whenever we want to develop spring rest api application then we need to add only this dependency we don't have to add all these dependencies okay so this is the problem that spring boot starter solves so this is a very simple example so whenever you want to develop other applications like reactive application spring reactive application or if you want to use hibernate or if you want to use other spring data jpa all right so we need to maintain a lot of dependencies right i need to maintain a compatible version so that is where the spring boot developed with the features being boot starters so spring boot team has created a status so we need to only add the status so spring boot will internally take care of adding all the dependencies require dependencies and their compatible versions okay so let's jump into our ide and let's understand more about spring boot status and how we can see what is there inside this springboard status so go to palm.xml okay and here you can see spring boot starter web dependency so this is basically springboard starter dependency and if you want to see what this spring starter dependency contains then you can just click on it okay and here you can see the version and then you can click on this and you can see what this starter dependency contains for example this starter dependency contains spring web dependency spring web mvc dependency okay along with that spring starter web dependency also provides a tomcat server as a default web container along with that it will add all the jackson dependencies for us okay so look at here jackson dependencies all right so this is a really awesome right we don't have we don't have to add all these dependencies guys okay we don't have to add spring um related dependencies we don't have to you know download and install top tomcat server starter dependency automatically provides a tomcat server to deploy the application as a standalone we don't have to provide the json libraries okay starter dependency will provide us json uh dependencies and starter dependency will manage the compatible versions okay so that is the beauty of spring boot starters feature okay so it does a lot of stuff behind the scene we don't have to maintain dependencies guys springboard starters take care of adding all the dependencies and other related third-party dependencies in their compatible versions etc so this is a very simple example for spring boot that web depends similarly if you can look a spring boot starter test so whenever we want to write a junit test cases then we need to add a lot of dependencies right for example j we need fio and mokito and other dependencies so if you go inside this static dependency you can see it uses all the geometry related dependencies so look at here it internally uses jupiter okay that is j5 dependency and it internally uses multi-dependency so what are the popular dependencies that are required to develop a geonic test cases that this spring boot test uh starter dependency will include that okay ham crest so so awesome guys we don't have to really add these dependencies and we don't have to maintain their compatible versions okay so spring boot starter test dependency will take care of all this stuff so these are really uh important starters guys so just take a look into it okay this springboard starter web dependency so this starter dependency we use to develop spring games application as well as spring rest application and spring boot starter test dependency we use to write a journey test cases we can use a junit file we can use makito we can use spring unit testing annotations a lot of stuff okay and apart from that few more very important dependencies like you can just copy paste and you can just change this string here i can say spring data jpa so whenever you want to use hibernate and spring data gpa in your project then you can add this spring with starter dependency you don't have to add hibernate dependencies manually because this starter dependency will take care of adding hibernate internally okay great so look at here it adds a abundant core dependency here okay we don't have to add manually hypernet and their compatible version so this starter dependency will take care of adding all the dependencies so apart from this if you can see the spring security you can just change here spring boot starter security okay and if you want to see what it contains then you can go ahead and you can see it's whatever dependency it provides okay you can see here spring security web spring security okay config spring aop okay whenever we want to use spring security in our spring application then we need to add all these spring security dependencies right and we do to maintain all the versions okay so instead of adding all these dependencies spring boot starter uh spring boot team provided spring boot starter that is called spring boot starter security so we can use this single dependency to you know have a spring security in our spring based applications okay so just remember spring boot starters are nothing but a starter dependencies that contains a set of convenient this you know dependency descriptors and they maintain all the versions so we don't have to hunt for the compatible versions of the dependencies okay so it's awesome guys uh really you know the developer we are facing a lot of issues while developing spring based application and you know maintaining the dependencies there it's compatible versions so this spring boot uh you know solve that problem okay we don't have to maintain uh the compatible version we don't have to add all the required dependencies we just need to add a single starter dependency and we'll get a one-stop shop of all the dependencies that require to develop a particular functionality for example if you want to develop spring muc then go ahead and use spring boot starter web dependency if you want to use hibernate in spring project go ahead and use spring boot starter data jp dependency if you want to use spring security then go ahead and use spring boot starter security starter dependency if you want to use unit testing or jna test case then you go ahead and use spring boot starter test dependency so these are the starter dependencies that we frequently use in our the projects okay just remember these starter dependencies so apart from this spring boot provides 30 plus starters okay to work with other different technologies to develop other different applications okay so this is all about spraying wood starters basically whenever you create spring boot application using spring initializer then spring initializer will add this dependency as a parent dependency for your spring root application and this parent dependency basically provides a default configuration for your springboard application now the question is what default configuration well if you go inside this dependency you can see that java version here okay within a property section you can see java origin by default the java version is 1.8 so this springboard starter parent dependency provides a default version for java that is 1.8 so if you don't specify java version while creating springbook project then this parent dependency will provide a java version 1.8 as a default java version okay and the question is i want to use java 16 which is the latest release then how can i do it well you can override this property and you can specify whatever the java version that you want for example so look at here i can specify a properties section here and within that i can override a java version section that is java version property and i can specify whatever the java version that i want for example i want jdk or java 16 okay so this is how i can override and i can specify java version that i want okay but if you remove this then spring boot starter parent will provide a java version that is default 1.8 okay so this one all right so this this is you know the flexibility guys so whatever java you want to use you can just go ahead and you can override this property and specify the java version that you want but if you don't specify then by default uh it will provide 1.8 okay so this is one default configuration and other default configuration you can see the plugins okay so it provides a lot of moment plugins guys okay we don't have to add a maven plugins in our springboot application so this springboard starter parent will provide a lot of moment plugins that is required for our marvin project all right so spring boot starter parent dependency will provide a default java version as well as the marvin plugins apart from that it will also manage the version of all the dependencies for example if you if you can dive into this dependency and you can see here the properties section and within properties you can see the dependencies versions okay active mq version and uh cassandra version okay and if you want to see the hypernet you can go ahead and you can see the hypernet version over here all right have a net version about validator version okay http core version all right so all the versions has managed by springboard starter parent we don't have to manage the versions over here and these are the default versions guys okay and we can override and we can provide our own versions okay that flexibility spring boot starter parent provides all right spring boot starter parent dependency it provides a default java configuration and maven plugins as well as it will manages the default version for uh you know the dependencies and apart from that one more vg is important uh very very important feature that spring boot starter parent provides is that if you can just go ahead and you can observe the starter dependency versions here spring boot starter web gpa security and test so these starter dependencies don't have the version right then how the versions you know maintained for these spring boot starter dependencies well this is maintained by spring boot starter parent dependency well whatever the version that we have specified to spring boot starter parent so this version by default added to all these starter dependencies okay we don't have to manually add this version here okay we don't have to manually add the version for these starter dependencies all right so spring boot starter parent will take care of adding these versions for these starter dependencies okay so let me show you how this added so if you can go inside springboard starter web dependency you can see the version here okay so this is how spring boot starter parent will add the version for all the springboot starter dependencies alright guys so this is the you know importance of springboard starter parent dependency alright it provides a default configuration for your supreme boot application and we can override the default configuration even in our springboard application all right i hope you understood the importance of spring boot starter parent dependency there are three kind of project the spring framework will create for example look at here these are the starter dependencies spring boot starter spring boot starter web dependency spring boot starter web plugs so these three dependencies are very very important guys and spring boot framework will create three kinds of projects based on these three dependencies we can we can create a spring boot application with spring boot starter dependency to develop the utilities and the standalone applications or the desktop-based applications okay because spring boot starter dependency does not contains the embedded tomcat server or any other web servers so we can use this dependency to create a standalone applications and spring boot starter web dependencies so we can use this dependency to create a spring emulation of application or spring muc you know rest ap applications and spring boot starter web plugs so this dependency we can use to create reactive applications okay spring boot we have reactive applications well as we know that spring boot application comes with a default embedded web server that is tomcat server right so it's a very important feature guys we no need to you know download and install a web server or application server on our machine okay so spring boot provides a very important feature that is you know embedded servers so spring boot application comes with a default web server that is embedded web server that is called a tomcat server okay and basically spring boot application supports three very popular embedded servers first is tomcat second jetty and then third is under town okay by default springboot comes with tomcat server and if you want to use jetty or undertown then you need to exclude tomcat dependency from your springboot application and then you need to add jetty or undertown dependency to your springboot application okay then you will be able to run your springboot application in embedded gtr undertone web server okay that will see bit later just try to understand embedded servers in a spring boot application okay three types of embedded servers tomcat jetty and undertown now we have talked about lot about embedded server right so what exactly is embedded server so whenever we have a java application and then we include a server as a part of that java application that is called embedded server in a java application okay so traditionally how we deploy our java application so we typically you know download tomcat server from the internet and then we'll install that tomcat server in our machine and then we package our java application as a wire file and then we deploy that war file in an external tomcat server and then we start the server so this is how we basically deploy our war file in a tomcat server right let's say if we include that tomcat server as a part of our java application then the tomcat becomes embedded tomcat server in our java application okay so let me show you the diagram here so look at here these are the steps when we deploy our springboot application without embedded server and these are the steps when we deploy our springboot application with the embedded server let's first consider if we don't use embedded server then these are the steps first step is we need to you know install the java and then we download and setup web server consider we have a tomcat server and then we need to package our springboot application as a war file and we explicitly deploy our war file in a web server that is tomcat server so these are the steps typically we do whenever we deploy springboard application without embedded server okay so look at here this step so these steps take some time right we need to download and setup web server okay so instead of downloading and setuping web server on our machine why not we include this server as a part of our java application so that will be better and that will save our time right okay and instead of deploying this war file explicitly in a web server then why not whenever we package our application then why not we run simply this package as a java application so we can also you know remove this step okay so whenever we include server as a part of java application then we can remove these two steps okay so here is the result so whenever we deploy our spring boot application with embedded server then we need we can skip these two steps okay so we need to have only installed java after that we package our spring boot application as a jar file which already contains embedded server and simply we run the jar file as a normal java application that's it we don't have to you know make a war file we don't have to explicitly copy that war file in a web apps folder of the tomcat server and then explicitly we need to start the tomcat server so we don't have to do in case of you know embedded server okay so this will simplify a lot right so that's why spring boot i know comes with a feature that is embedded server okay springboot application by default comes with embedded server that is a tomcat server okay tomcat server and if you want to have a jetty or under town server in your springboard application as an embedded server then you simply need to remove this tomcat dependency and then you need to add a jetty or under term dependency okay so that will see a bit later okay i hope you understood what is the embedded server and how we can include that embedded server in a springboard application well whenever we include our server as a part of java application then we call it as an embedded server okay just try to understand the concept now let's understand more about embedded servers so let's jump into id and now i'm i will show you how the spring boot application comes with default web server that is tomcat server go to palm.xml and here we have a dependency spring boot starter web dependency so this dependency internally provides i know default stomcat as a embedded server so let me show you so just dive into this dependency and again go to inside this dependency and here you can see spring starter tomcat okay so we don't have to add this dependency to our palm.xml because this spring boot starter web dependency internally provides a tomcat dependency okay so whenever we add springboot starter web dependency to our project then this dependency will provide a tomcat server as embedded server by default guys we don't have to add tomcat dependency again okay so let me jump into this this dependency so look at here springboot starter tomcat and if you go inside this dependency you can see tomcat dependencies okay so whatever the tomcat source code that you have in a tomcat server that source code is available with these dependencies okay guys so by looking into these dependencies you can see that by default tomcat server is a part of spring boot application and whenever we run our spring boot application then spring boot application will run in this tomcat server as a standalone okay we don't have to explicitly download and install the tomcat server spring boot comes with a default web server that is tomcat server okay so let's go ahead and let's run the springboot application and let me show you how our springboot application will run an embedded tomcat server so look at here our springboot application is running in a embedded terminal server okay embedded tomcat web server which is running on port 8080 by default the tomcat server will run on port 800 but you can customize this port okay you can give any port that you want so you need to provide that port by using some you know property so you can go to application.properties here and you can define the properties something like server dot port okay by using this property you can just change the tomcat server port by default it is edge right zero now you can go ahead and you can you can give eight zero eight one or whatever the port that you want okay great so this is how the tomcat server comes with springboard application as embedded server now the question is i want i don't want to use embedded server i want to use other embedded server like jetty or undertown then how how i can configure uh you know jetty or unreturned as an embedded server in our springboard application well let me show you how to do it so go to palm.xml and this springboard starter web dependency it internally provides a tomcat dependency right so we we have to exclude that dependency from this springboot starter web dependency and then we need to add separate gtr and return dependency okay so in order to do that just just call here the tag exclusions and inside that add the dependency exclusion tomcat now we have excluded spring boot starter tomcat dependency from spring boot starter web now we can able to add you know other embedded servers for example zdr undertone now let me show you first how to add jetty as embedded server so let me quickly copy this dependency and paste it here okay let me remove this scope and here instead of test we can just add jetty here okay that's it now let's go ahead and let's run our springboot application in embedded web server so go go to our springboot main entry point class right click run as springboard application now instead of running our spring boot application in embedded tomcat server now it will run in an embedded jetty server so look at here embedded jetty jetty web server and by default jt server will run on port 800 but you can able to customize this port okay you can change this port by you know adding the property in application.properties file so you can add a property called server.port and here you can specify the port 8081 or any port that you want okay by default the port is eight zero zero let's say we provide a port it's rate one okay now if you stop the server and if you run the springboard application then you can able to see that springboard application should run on a jetty web server on port 8081 so look at here so this is how you can change the port okay now we'll see how to add under down server as an embedded server so go to pom.xml again okay and here we have earlier added jetty so just replace from jetty to under down okay under town so in order to run our spring boot application in embedded undertown server again go to our springboard main entry point class right click and run as a springboard app okay and let's see here there we go our springboard application is running on under tau web server and port it's red one so the port it's red one because we have added a port it's red one right so let me remove this and if you can run spring boot application again then the under tab server will run on port 8080 and there we go our springboard application is successfully running on embedded undertale web server on port 8080 all right guys this is how you know we run our spring boot application on different embedded web servers okay we have seen how to run our spring boot application in a tomcat server jetty server and under town server so during development we spend a lot of our time in ide and we run our spring boot application from main entry point class okay which has a main method all right so whatever the ide you use for example you may use integer idea eclipse or netbeans so you have to run your springboard application using springboot starter endpoint class which has a main method right so sometime the developer may want to run spring boot application from outside the id and one option for that is using command line okay you can use a command line to run your springboard application from outside the id all right so as we know that springboard application whenever we run as a standalone springboot application will run in embedded tomcat server so we just have to run our spring boot application as a standalone it will automatically deploy an embedded tomcat server we don't have to download the tomcat server from the internet we don't have to install tomcat server in our machine okay great so there are two options to run our springboard application from the command line so one option is using java hyphen jar command and another option is using mavin plugin okay so in this video i want to show you both the options so before running our spring boot application as a standalone in the command line one more very important point is if you look at the project structure of our springboard application here we have two files okay these these two files are called moment wrapper files okay so what these files will do if we don't install maven in our machine then whenever we run our springboard application using the these commands then spring boot application will automatically install a maven for us by using these files we don't have to install mavin and setup on our machine okay so if you have already installed marvin then you don't have to use these two files you can simply delete these two files from your project okay so in this video what i'm going to do is i'm going to use these two wrapper wrapper packages okay so now what we'll do we will head over to this project in a file system okay and open command line in this folder just type cmd and hit enter so this will open a command line in your project and here we're going to use movnw okay just look at here m v and w so this is the wrapper file that we are using to package our springboot application as a jar file okay so if you have already installed marvin in your machine then you can simply use mvn okay so this is the maven cell so i am assuming that you haven't installed marvin in your machine okay so i am using mvnw this is a marvin wrapper and then just type package so this will package your spring boot application as a jar file so notice that our springboot application is successfully built okay and here you can just type dir you can able to see the folder structure and then just navigate to target folder and here just type dir and here you can see spring boot first app okay so this is the jar file that we are going to run our spring boot application as a standalone all right so in order to run this jar file as a standalone just type the command java hyphen jar followed by the jar name just copy it and paste here so make sure that you have installed java because we are using java command here all right so go ahead and just hit enter so this will run our springboot application as a standalone embedded tomcat server so look at here our springboard application is up and running and it is running in embedded tomcat server on port 8080 great so this is the first option now let us see the second option second option is using marvin plugin okay so in order to run our spring boot application using maven plugin just type the command m v in w so again here we are using marvin wrapper file okay maven wrapper command here so if you have already installed marvin then you can simply use a movn followed by the plugin name but i am assuming that you haven't installed marvin so i am using mvnw here okay i have followed by spring hyphen boot colon run so this is the marvin plugin command that we are going to use to run our spring boot application as a standalone in embedded tomcat server so go ahead and hit enter so it says m vnw is not recognized so we are in a target folder so we don't we no need to go back to one folder so here i'm just enter the command m v and w spring hyphen boot okay call and run okay make sure that you are in a project folder okay so earlier i was in a target folder that's why i got this error but make sure that you are in a project board okay because this mvnw wrapper file is present in this project folder okay now go ahead and hit enter so look at here our springboot application is running in embedded truncate server which is running on port 8080 okay so this is a very important guys whenever we develop a real-time project sometime as a developer we we need to run our spring boot application from the command line let's take a look into spring boot project architecture so this is a pretty simple springboard project architecture we call basically it has three layer or three tire architecture okay so this is a three layer or three tire architecture and this is a widely used you know architecture in spring based applications all right so look at here this architecture contains controller layer service layer dow or repository layer and each of these layer have their own responsibilities for example controller layer basically you know process all the request that's sent by the client okay and controller layer is also called api layer because we defined all the rest apis inside a controller layer in spring based applications we typically create a controller and we you know we define all the rest apis in the controller okay the rest api can be get rest api post put or delete rest api okay so just remember controller layer is responsible to process the request request can be get request post put or delete request controller layer is also called api layer okay next come to service layer service layer we basically create or do you know to maintain all the business logic so whenever you have a business logic in your application then make sure that you will create a service layer and you keep all your application level business logic in a service layer so apart from business logic if you have any you know third-party rest api call or any public third-party rest api call in your application that you that logic you can keep in a service layer okay just remember whenever you have business logic in your application make sure that you will create a service layer okay and you keep all the business logic in a service layer next come to dow or repository layer well tao or repository layer is responsible to talk with the database and in this course we are going to use mysql database well down here or i know repository layer holds all the database related logic or persistence logic okay so these are the three layers uh you know that we are going to create in our springboard application and again uh in a controller we call a service and in a service we call a dow or repository so basically in spring based applications we inject service class or service interface in a controller and we call service class methods in a controller similarly in service class we basically inject a repository or a dao and then we call dow or repository you know methods in a service class okay so these three layers are independent to each other and then we'll inject the dependencies and we call its methods now let's take a look into spring boot muc project architecture so this is the standard architecture that we are going to follow to develop our springboot application and this is the three tire or three layer architecture it has a controller layer service layer a repository or a dowel layer okay this is a widely used architecture in you know project development so basically controller layer holds all the spring image controllers and service layer basically holds all the service classes it has a business logic and repository our downlair holds all the persistence or database related logic and repository is responsible to talk with you know mysql database so this is the three layer architecture now let's come to spring emuc architecture so look at here this is the muc architecture or you can say emuc design pattern we have a controller view and there is one more component called model so controller is basically responsible to process the request and it will you know return model and view to the browser to render the page so controller layer basically process the request and it will get the data from the database by using service and repository layer and that and that model data basically you know bind to you and that that generates a web page that web page will be displayed in a browser it's pretty simple uh spring games architecture so sometime the use case is like let's say if you want to deploy our spring boot application in you know application server for example jboss wildply server okay in this case we need to generate our spring boot application as a war file not a jar file right so in such scenarios we need to you know package our spring boot application as a war file and then we need to deploy an external server server can be a tomcat server or jbuster right so in this video we are going to take tomcat as an external server okay what we will do we will package our spring boot application as a war file and then we will download tomcat server from the internet and then we will deploy that springboot war file in a tomcat server okay great so let's head over to spring to suit id and let's quickly create springboard project so in order to create a springboot project go to file new okay go to other and choose ping starter project here and just give name as spring boot or demo okay just give project name as spring boot or demo and then type choose marvin and choose packaging as a war okay so this is very important guys whenever you want to deploy your springboot application as a war file in external tomcat server then you have to choose packaging as a or okay so don't choose jar so if you choose jar then you need to deploy your spring boot application in embedded tomcat server as a standalone application okay so now we are going to deploy our springboot application as a war file in our external tomcat server hence we need to choose packaging as a war over here okay then java origin choose 16 and then language java and keep group as com.springboot.app and then artifact is same as project name and then version description let's say springboard or demo something like this okay and packaging has a com.spring boot dot or something like that and go ahead and click on next and keep being boot version default as it is and then here we are going to choose spring web dependency okay so if it is not available over here then you just need to type spring we have dependency okay so go ahead and choose spring web starter dependency and go ahead and hit finish so this will create a new brand spring boot application in spring tools root id okay now what we'll do we'll add a simple rest endpoint in order to test our springboot application deployment in external tomcat server so before that let me clarify a few points here go to palmrot xml and here you can see packaging as a war okay so make sure that the packaging should be war we have selected war as a packaging type right while creation of spring boot application and if you can see here spring boot starter tomcat dependency with scope provided so whenever you choose packaging type as a wire then this dependency will automatically add it to your spring boot application because spring boot starter web dependency internally provides embedded tomcat server right and whenever we want to deploy our spring boot application as a war file in external tomcat server then we need to add this dependency with scope provided and one more important point is go to source folder and you can see here servlet initializer so whenever you choose packaging as a war then these three differences you can typically see first is packaging as a wire and then this tomcat dependency with scope provided and servlet initializer so this class is needed to deploy our spring boot application as a war file in external tomcat server because this class internally contains a servlet related file for example if you go inside this and you can see here spring boot context loader and then contacts loaded listener and you can see servlet contacts listener so this listener is required for our spring boot application in order to deploy an external tomcat server or any other servers right whenever you create spring boot application with packaging as a wire then typically you see these three changes okay packaging as a war and then spring boot starter tomcat dependency with scope provided and servlet initializer class okay and rest of the things remain same okay so just try to understand when you you know when you choose packaging as a jar and when you choose packaging as a war file all right great now what we'll do we'll simply create one restain point here i am going to create a restaurant point in this class itself let me add a annotation here that is a rest controller annotation and let's quickly create a simple rest api here public and this rest api should return a string and let's give a name something like greeting all right and i'm going to i rotate this method with add get mapping annotation and just give rest and point url something like greeting okay and just return spring boot or demo something like that all right so very simple standpoint we are not going to create a standard http here we are just wanted to test our springboot application deployment in external tomcat server that's why we are going to just write a simple dpa here okay great now what we'll do we'll we'll package our spring boot application as a wire file for that right click on the project run as marvin build okay and here just type clean install okay and go ahead and apply and run so look at here the maven build is success right and go to the project again so let me minimize this so go to project right click and refresh okay and then go to target folder over here and within a target folder you can see spring boot or demo dot war file all right so this is the war file generated whenever we build our spring boot application successfully okay great now we are going to deploy this war file in an external tomcat server so for that first we need to download and set up tomcat server for that what i'm going to do is i'm going to go to browser here and let's say download tomcat 9 okay and go to first link that is tomcat.apache and here i'm going to download this chip file over here okay once apache term get g file downloaded on your file system go ahead and open your folder and now let's unzip it right click and extract all right now you can see here web apps folder so we are going to copy our wire file and paste in a web apps folder so go ahead and copy this wire file from the target folder okay and go to web apps folder and just simply paste it here all right so once we copy paste to our file in our web apps folder then go back to apache tomcat folder go to bin here and search for startup file okay so make sure that you choose windows batch file here so go ahead and double click on startup so this will deploy our springboot application you know tomcat server okay let's wait couple of seconds now you can see here our spring boot application is up and running in external tomcat server on port 800 so look at here now what we're going to do is we're going to test our deployment right we have developed one simple rest apart greeting rest api so we are going to test this restful from the browser so that will make sure that our springboot application deployment in external tomcat server is successful we'll go to the browser and in a new tab just type localhost 8080 and if you try to call this greetings rest api with this url it will give you an error not found because we have deployed our springboot application in external tomcat server right so we have to use this application context okay this is this application name let me copy this application name that is war file name and go to browser and if you try to add this application name here and then slash grating this will give you a result okay so make sure that whenever you deploy your spring mode application as a war file in external tomcat server then make sure that you will give your the application contact name okay that is a war file name and then followed by rest api url name that is greeting all right let me quickly recap what we have done in this video we have created a spring boot application with packaging type as a war okay and then we have created one simple rest and point to test to test the setup and then we have package our spring boot application as a war file and the war file is created under target folder and then what we have done we have downloaded a tomcat server from the internet and we have you know downloaded on our file system and then we unzip and then we have copy pasted wire files in our web apps folder and we have simply started the apache template server and we have tested the rest api that's it right very simple steps guys let's first understand what is a developer problem and why developers would have to use spring boot devtools dependency while developing spring boot project well the problem is whenever we develop spring boot application we do a lot of changes right and let's say if we do some changes in our spring boot application then we need to restart our spring boot application then only the changes will get reflect right and this is kind of a tedious process right so developer need to restart the application again and again whenever you do some changes right so that is kind of a tedious process and the solution could be you know uh application should be restarted automatically whenever we do some changes in our spring boot application so that is what the spring boot devtools dependency will solve the problem okay the solution for this problem is spring boot devtools dependency right for example let's say we are we are developing spring boot first application project okay and we have added some rest and points to this project for example hello world and some student related rest endpoint okay and let's say if we deploy our springboot application in embedded tomcat server so typically we go to uh start class that is springboard entry point class and we run as a springboot application so this will run our spring boot application in embedded tomcat server okay great now our spring boot application is up and running on embedded tomcat server all right now we can able to you know test our rest apis for example let's say we are testing a hello world rest api here okay so whenever we hit this url we get a response okay because our springboard application is up and running in an embedded tomcat server now we want to make changes to our springboot application consider we want to add one more aspen point for example let's say i will simply add one one restain point here let's say hello world i have something like this to just uh give you an idea i'm going to just simply add one a restaurant point something like this and if we save okay so look at here server is not restarted automatically and if we try to call this rest api from the browser for example hello world app so this will give an error because whenever we do some changes in our sprint application we have to restart our server right for example here we need to stop the server okay and we need to again start our springboard application right click run as springboot app so once our boot application is up and running in embedded template server which is running on port 800 now we can go ahead and we can call this rest api from the browser now we can get the response because changes are reflected okay so whenever we do uh changes like this we need to restart our spring boot application then only the changes will get reflected right and this is kind of a tedious process so developer don't want to restart the application again and again okay so there should be a solution the solution could be this should be automated process whenever udam do some changes in springboot application this server should be restarted automatically okay so this is what uh the problem the developer faces and spring boot provided uh the feature that is spring boot devtools feature so this feature or a model basically solve this problem okay so basically spring boot devtool dependency will detect our changes in our project and based on that it will restart the server the server can be in other servers like right now we are using embedded tomcat server but if you are using jetty or tundratown same process will apply for these servers as well okay so whenever we do some changes in our project then spring boot out to dependency will automatically restart the underlying server the server can be tomcat server jt server or under tab server okay now what we'll do we'll use springboot.tools dependency in our project and we'll verify how spring boot devtool dependency will automatically restart our application whenever we do some changes okay great so let me stop the server here okay and go to palm.xml and here we need to add springboard downloads dependency so in order to do that i'm going to copy this dependency and i'm going to paste it here okay and here i will just make a changes so i will remove spring boot starter web and here i will just add spring boot dev tools so this is the dependency that we are going to use to restart our spring boot application automatically whenever we do some changes okay now once we add this dependency that is springboard double dependency then what we'll do we will start our springboot application so go to main entry point class right click run as springboot app okay now if you can see here spring boot application is up and running now what we'll do we'll do some changes to our spring boot application so go to hello world controller and here what i'm going to do is i'm going gonna just remove this rest endpoint that we have added right i'm going to remove this and just focus on the console here okay once i remove this rest api and then i will just press ctrl s to save this file and if you can see here in the console the server will get automatically restarted okay the server will automatically get restarted and the changes will get automatically reflected for example if i try to call this rest endpoint this won't be available because we have just removed it okay okay so let's let's uh taken one more example let's say we have a rest endpoint hello standpoint and if we make some changes in this rest endpoint then the server should be automatically restarted right so what i will do i will make some changes here i will just remove hello i will just remove word and i will keep hello here and i will just remove this word okay and if i save this file and if you can observe the console here so look at here springboard application is restarted again and if i go to browser and if i just calls hello world so this should gives me not helloworld if i call hello so this should give me hello because we have done changes here to the existing rest endpoint so earlier it was hello world and we had a changes from helloworld to hello so this is how we use spring boot outputs dependency okay so this will gives you a developer uh you know better experience guys so whenever you develop spring boot application try to use spring boot devtools dependency so it will save your time you don't have to restart your springboot application again and again whenever you do some changes in your project well in order to add security to our springboard application first go to the firmware xml file and let's add you know spring boot status security dependency let's go ahead and let's add a dependency here artifact id is spring boot status security and group id is org dot spring framework dot boot so this is the dependency that we need to add in order to you know provide a security to our springboard application well this dependency basically you know internally download all the spring security related libraries so look at here it internally uses spring security web spring security config and eop and other dependencies we need to add only spring boot starter dependency spring boot starter security dependency and this dependency will take care by adding all the spring security related libraries and the compatible versions etc all right now once we add you know dependency to our springboard application next we need to enable a debug log to our spring security module so that we can able to understand the default configuration of spring security we'll just add dot spring logging.label.org.springframe dot security equals to dig up so this is the property we need to add in application dot properties file all right now what we do we will run our supreme boot application and let's quickly explore the default spring security configuration in our springboot application go ahead and just run springboard application so look at here as soon as we start our boot application then in a console log we can see the statement using generated security password followed by random generated id all right so this side is nothing but a password random generated password well the spring security by default provides a user with this random generated password and we can use this default user to authenticate authenticate to our application and one more important point is by default spring security enables you know security to all the urls in our application and we need to white list the required urls in our application that we'll see in upcoming videos just remember two finds by default spring security provides a user whose user id is user and password is this random generated password which is printed in a console and the second point is spring security by default you know enables security for all the urls in our application okay now let's head over to the browser and let's try to access one of the rest api go to the chrome browser in a new tab just type localhost 8080 api slash sports so just we are trying to retrieve all the posts by using this respond url hit enter and look at here as soon as we try to access a rest api spring security will you know navigate to us to this screen so this is the default login page provided by spring security it's awesome right we have just added a dependency that is springboard status security dependency and we are getting a lot of functionality like this is out of the box login page provided by spring security and this is called a form based authentication well let's go ahead and let's enter a username as user and password we can simply you know grab it from the console just go ahead and copy this password and go to the browser again and enter password in a password field and hit sign in button and there we go we got a response of the rest api as soon as we logged in our sign in to the application then we can able to access rest apis so this is called a form based authentication now we logged in using user and password from the random generated password in a console so we can also standardize the username and password by using some of the spring security provided properties so let's go ahead and let's add few properties to standardize the username and password and role let's go to application.properties file and here just provide the properties to configure standard username and password so look at here spring.security.user.name so this is the property that we can use to specify the user so i am going to just call it as ramesh and we have another property spring.security.users.password we can specify password here and we can specify role as well let's say admin alright so instead of using the default provided user and password we can also provide our own standard user and password by using these fields well let's go ahead and let's rerun our springboard application and let's try to login using these credentials go ahead and run springboot application so notice that in a console the random password is not printed so as soon as we provide a standard username and password by using these properties then the random password is not generated in a console all right now let's go ahead and let's use this username password to authenticate our application now let's go let's just refresh and this will navigate to the login page again and just enter username as ramesh and password as password and hit sign in button and there we go we have successfully logged in to our application and we can able to access the rest endpoints all right we have just entered username as ramesh and password as a password so this is how we can you know provide our own standard username and password instead of using spring security provided user and password before deep dive into spring data rest let's first understand what is the problem spring data rest model solves okay so [Music] in typical spring muc project we typically create a controller layer service layer and underwater right and in controller layer let's say if we want to create a rest and points for employee then we need to create a employee controller and then we develop a rest and points for employee okay and similarly in service layer we first create interface employee service and then we create a class employee service implements employee service interface and implements all its methods okay so for single entity let's say employee we need to create three files okay and let's say if we want to create a rest apis for other entities like department project company users etc then we need to create a controller service interface service class for each of these you know entities all right so if you can observe the code only the entity name is changing over here and rest of the code remains same okay so we are basically repeating the same code again and again all right so we can think the solution for this problem the solution can be you know automatically creating a rest apis for a ntt okay we are only changing the entity name here but if we can see the code remains same okay so that is the problem that spring data rest solves okay so spring data rest module is a solution for this problem so basically spring data rest uses interfaces which extends jpeg repository and spring data rest creates a crude rest apis for entity for free okay so basically spring data rest helps to minimize the boilerplate code required for controller layer alright so i hope you understood how spring data solve the problem well probably you know that whenever we develop our enterprise application we need to deploy our application on different different environments right for example we need to deploy on qa environment for testing we need to deploy on ebt environment for end user testing we need to deploy on production environment right for customers right so in order to deploy our application on different different environments we need to implement profiles in our application well spring provides profiles concept so with the help of spring provided profiles we can configure our springboot application to deploy on different different environments so let me show you how to do that well go to resource folder within resource folder we have application dot properties file so in case of local development we have used this file to configure all the mysql details hybrid properties and jwt properties right let's say we want to deploy our spring boot application on production environment then we need to change this application.properties file to configure production environment related details similarly let's say we want to deploy our springboot application on different environments for example given environment then we need to change this properties file to configure qa environment related details right we are here basically changing this properties file as for the environment but basically we don't want to change this file we need to implement profiles so let's go ahead and let's implement profiles in our springboot application to deploy our springboot application on different different environments so let me show you how to do that go to resource folder and we have application dot properties file so go ahead and copy this file okay and paste it here and just you property's name something like application hyphen dev so in order to deploy your springboot application in development environment then you can go ahead and create a file something like application hyphen dev dot properties file and you can keep all the development environment specific configuration in this file similarly let's say you want to deploy your springboot application on production then you can create a production specific profile here application hyphen prod dot properties and you can keep all the production environment specific configuration in this file okay so similarly if you want to deploy your springboard application in qa environment then you can go ahead and you can create a profile or properties file something like this and you can keep all the qa environment specific configuration in this file okay so this is how we create a property file specific to environment so whenever we run our spring mode application by default spring boot will refer application dot properties file and it will read all the configuration from application.properties file but we want to deploy our springboot application on production so we need to tell springboot that springboot should read a configuration from application hyphen prod dot properties file so in order to you know do that we need to activate this profile production profile so we have a property that is spring dot profiles dot active property that we need to add in application dot properties file so let me quickly add that so look at here spring dot profiles dot active equal to prod now fraud profile is activated well once we run our springboard application spring boot will refer application.properties file and it will read all the configuration from application.properties file and then it will see the activated profile now in our case broad profile is activated so spring boot will go into this file application hyphen pro dot properties file and it will read all the configuration from this file okay so if you activate their profile then spring boot will first read application.properties file and next it will also read this file okay so whatever the common configuration you have in your in your springboard application then that you can define in application dot properties file because this file is invoked first and based on the activated profile it will invoke corresponding properties file in our case application hyphen prod dot properties file
Info
Channel: Java Guides
Views: 4,607
Rating: undefined out of 5
Keywords: spring boot, interviews, spring framework, java
Id: M7kxVg7RKKA
Channel Id: undefined
Length: 129min 19sec (7759 seconds)
Published: Mon Nov 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.