Hi everyone welcome back to Java techie if you are a spring boot developer or aspiring to be one then I have something special lined up for you I will be diving deep into spring boot interview question and answer Series where we'll cover all possible spring boot interview questions from basic to Advanced concept and also I will try to provide you with the clear and concise answers which will not only help you during your interviews Journey but also it will improve your overall understanding of spring boot okay all right so without any further delay let's get
started with the first part of the series thank you [Music] so here we begin with our first question that is why will you choose spring boot over spring framework if you will attend any interview as a spring boot developer then blindly this is the first question You will get from the interviewer that why spring boot why not spring framework okay so interviewer don't want to listen the theory from you he just want to know how far you understand the spring Boot and its advantages so that is the reason I have list down all the bullet
point to identify why spring boot is better than spring framework will go one by one the first key bullet point is dependency resolution or avoid version conflict so Just think before spring boot when I want to design some crowd application let's say I want to use the spring and hibernate then I need to manually add the dependency right I need to add the spring core spring context then spring MBC and spring hibernate lot of dependency I need to add it manually right so to demonstrate that I have some code for you I will just show
you that what all dependency we need to add if you are creating the plain spring Project not the spring boot one so this is the project so this is kind of spring crowd example using the hibernate so if I open the pump.xml let me Zoom this for you can you see here I am using spring core spring context spring transaction spring or M spring web spring webmbc Highway net core hibernate validator then MySQL connector Jackson related dependency Then servlet API okay so these many dependency I added manually also if you will observe I have specified
the dependency version manually so spring core spring context and specifying that I just want to use the 4.3.2 release version of spring framework so again there is another challenges to get the person conflict let's say spring core I am using 4.3.2 and context I want to either downgrade or upgrade so there may be a chance to not support the backward Compatibility because we have specified the dependency manually that is another challenges in Spring framework and the biggest challenges I feel that you need to remember what all dependency you need to add while creating a project
so while creating a spring Cloud application you need to know that these are the dependencies I need to play with fine this is what the biggest challenges in Spring framework in case of dependency resolution and conflict awarding but Let's see how this spring boot make the job easier for us as part of this particular bullet point that version management and dependency resolution so let me open the same project with the spring boot now if I'll go to the palm.xml so just observe carefully what all dependence you have added spring boot starter data jpa spring boot
starter web then which driver I want to use that is MySQL connector and lumbok you can Ignore that for now so we have added only two dependency JP and web to just perform the crowd application using the webmbc so if You observe in the old project using the string we have added minimum 10 dependency right and that we have specified the version manually but in case of spring boot these two dependency is enough to handle the spring MVC crore application and also we have not specified the dependency version how the independence will be Managed here
based on the parent version so if you'll specify that okay this particular parent version of spring boot starter I want to use then internally spring boot will be bring all the corresponding or transitive dependency for these two particular key dependency so if you are spraying data jpa internally spring boot will add the hibernate specific dependency for you if you are the spring boot starter web then by default spring Boot will load spring core context everything for you on your class path that is how the first point that using the spring boot we no need to
take the headache of this version conflict and it is quickly help us to do the dependency resolution just add two dependency it will enough and even you no need to specify the version okay I believe the first point is clear for you now the second bullet point if you're using springboard then you can avoid Additional configuration to perform any task let's take the same example that I want to perform the crowd operation using webmbc then we'll see what all configuration you want to do if you are using the spring framework then what all configuration required
if you are using springboard will do this comparison to understand this upward additional configuration if you are using spring boot so let me go back to the project the old project which we designed using The spring framework if we'll go inside this Java web app now if You observe in the web app I have three XML configuration application hyphen context.xml let me open this what is there let me Zoom this for you so if you object if I am using this plane spring framework then I need to create the data source object by defining all
the required properties of data source then I need to create the Session Factory object and I need to inject the data source which I created to the session Factory then I need to Define all the hibernate properties hibernate so SQL hibernate hbm to ddl all the required properties then I need to create the transaction manager and I need to inject the session Factory to the transaction manager these are the three key steps you need to perform create the data source create the session Factory create the transaction Manager then inject all the required dependency okay and
also makes you to inject all the required properties now let's move to the next configuration in this configuration I am telling to the spring framework hey Spring can you please scan this particular packages all the packages or all the classes inside this particular root packages based on that spring framework will load your classes to the context okay and if you go here in the web.xml can you see here I am telling that inside the webinf I have the web.xml to kicked up my dispatcher servlet manually okay so here I am just defining the web.xml to
kicked up the dispatcher servlet these are the complex configuration using XML I am doing in the spring framework if you can avoid XML that is fine in Spring also you have a option to define the Javas configuration but still you need to create this bin manually data source session Factory transaction manager and This components can enable and this dispatcher servlet everything you need to configure manually but let's compare the same thing that we are doing the spring crowd example using springboard what all minimum configuration we need to do okay so go back to the spring
boot project now in Spring boot project if you go into the project section main Java forget about these packages and classes so I am I just want to show You the configuration okay so go to the application.properties go to the application.aml file here is my configuration in Spring boot I no need to care about creating the data source transaction manager session Factory manually everything will be handled by Spring boot itself can you see here I have just defined okay these are my properties and I am just telling to the jpa that create the table automatically
and these are the so is Quilt true these are the things and I am just using the MySQL dialect these are the things I just Define as part of the properties to tell to the spring boot build the data source object for me and give me give me the jpa transaction manager and JP related classes to me so that I can quickly perform now if I want to use the database in the springboot project simply I need to create a if I'll go here I have a repository I just need to create a interface extends
It from JP repository everything will be sorted for me to start playing with the crowd related operation okay so this kind of headache is not requiring spring boot I am not relying on additional configuration on Spring boot I have just defined the required properties to kicked up my database persistence layer that is what the major difference in Spring and springboard in Spring framework you need to rely on complex additional configuration in Spring boot It's not required okay I believe the second point is clear for you let's move to the next bullet point that is embedded
Tomcat JT is given by Spring boot to quickly deploy your jar or War file to the perform quick testing in Spring boot now if you are running any application in the spring framework then manually you need to build a jar or War file then you need to host it to your external Tomcat server to just just verify your features or to just access The application functionality but in case of spring boot if you open the spring boot project you no need to do anything okay spring boot by default will give you an internal embedded Tomcat
server which will quickly picked up your jar and will Kickstart your application so for example I want to run this application I can simply so if you go to the project and if you go to the SRC Java then there will be a main class annotated with other springboard Application okay just start this particular key or you can start from this particular here ID also this icon just click on this it will start your application and it will host your application to the embedded Tomcat server so it will quickly start within a two to three
seconds can you see here it quickly started on custom Port which I have defined okay I'll come to this kind of question how You can do the properties changes or override the behavior but for now just try to understand how spring boot is better than spring framework okay so it marks take two to three seconds for me to get the jar from the target folder and load it into the application context and kicked up the embedded server for me but same thing in Spring framework is bit complex you need to generate the or file then
you need to give that word to the external Tomcat to kicked up your Application okay now let's go to the fourth bullet point that provide production ready features such as Matrix health and check okay so in Spring boot there is a separate starter called actuator using that actuator you can easily monitor your application in each and every environment okay so if you add the dependency you will get couple of endpoint Point like slash bin slash Health slash Matrix even you can register your own custom metrics to just Monitor your application in any environment but same
thing if you want to perform in the spring framework the approach is not straightforward you need to rely on couple of dashboard to monitor your application or to monitor your health of your application in each and every environment okay so this particular actuator feature I will keep as a side for now because I am going to cover this particular actuator all the possible interview question and its Internal in my upcoming series okay so let's move to the next question that is what all spring boot starter you have used or what all model you have worked
on so interviewer is asking this question to just know that what all module of spring boot you are already familiar or you already worked on it so there are tons of stutter dependency given by springboot to perform different use case but these are the common stutter which is being used in industry I mean based on the use case you need to find out what all starter you need to include so I have just listed down here couple of stutter so spring boot starter wave is being used if you want to design some web specific application
spring data jpa if you want to integrate your application to the any persistence layer then you need to use this springboot starter data jpa and if you want to perform any aop related operation then you need to add the Spring bootstratter aop but if you want to use something or if you want to expose soap based web services then you need to include the spring boot starter web services and if you want to include the security as part of your application then you need to add the Spring bootstratter Security and if you want to play
with the event driven architecture or if you want to use the messaging Channel then you need to include the spring boot starter for Apache Kappa if You are building the microservice and need features like service Discovery configuration management and load balancing then you need to include the spring cloud and if you want to include the static web page to your application then you can use the spring bootstratter time leave okay these are the common dependents you have explained but if you want to know more about what all modules available in springboard or what all starter
is available in springboard then Simply go to the spring.io.project then spring Boot and you can see here lot of modules so these are the modules spring data is a module spring cloud is a module spring boot itself is a module then Spring Security spring batch spring for Apache these are the modules so if you go inside the spring data data jdbc jpa ldap mongodb these are the individual starter available in the spring data module so you can simply explain to the interviewer that okay These are the modules I have worked spring data Spring Security spring
batch then if he is asking more about the starter dependency then you can list down what all dependency you have already worked or what you can remember that time okay that's pretty simple now let's move to the next question that is how will you run your spring boot application so this question looks very simple but there are something you need to know interviewer don't want to know How you are starting your application he just asked this question to know that how internally it loads your jar or from where it will load your jar those things
he just wanted to know okay so I will show you in the code let's go to the springboard application now you need to go to the SRC Main then go to the packages where you have your main class just open that class now here you can run your application I mean it will just run from the main Method so you can simply run from here or you can directly run from here as a springboard application or you can run from directly in the ID so let me run from here so it started on default port
8080 let me stop this now what is the next way if I don't want to run the application from the ID then there is a mbn plugin given by springboot itself that you need to run so let me show you that So can you see here mbn spring boot column drawn so this spring boot column run is a just a plugin as part of the maven so if you will open this if you go to the plugin section can you see here we have this spring boot plugin and this is the run method what you
can run from your terminal to kicked up your spring boot application okay now let me show you go to the terminal let me run this can you see here I cannot zoom this right that's fine spring boot interview question and snapshot this is just building my jar file okay and copying the resources Let It Be finally it started my application on port 8080 right now the interesting fact to know here how this particular command MB and spring boot run load my jar file and execute the jar or from where it will load the jar file
that is what something we need to know right so Forget about any framework if it is a Java application and you are using the maven build tool then by default the jar will be placed inside the target folder can I open this target folder and can you see any jar here classes generate source test suits Maven status test classes can you see any jar file here no right then how come this particular command this particular command mbn spring boot run loads the jar file from where you load The jar file that's quite interesting isn't it
then how it works without the jar file so what it does when you run this particular command mbn spring hyphen boot column drawn it executes your application directly from the Target under classes okay so so it goes to this particular folder and then it picked up your main class and then it trigger the execution from this main class it does not create a separate jar file unless You explicitly package your application using Maven package or some similar command like Gradle or something like that okay so if you are using Maven spring boot run command you
won't find a jar file in your project directory during the development but it is generated during the build processes and include in the jar file okay so if you want to look into the jar structure how it looks at runtime then what I can do So simply I'll just build the jar manually let me build the jar okay so whenever we are running the command it goes to the Target folder it go to the compiled classes inside this and it go to the main class of my directory and it directly trigger from here without storing
the jar file in Target folder okay so that is where our embedded server kicked up so quickly right because it goes to the compelled classes And Trigger the main method simple now if you initiate the actual Maven build then it will generate the jar inside the target Let me refresh yeah can you see here the jar file let me copy this jar file then we'll extract it and we'll see its internal structure okay let me paste it in the desktop then I'll just open a new terminal I'll go to the desktop folder jar is there
right what is the jar name this is the Jar name let me copy this so let me clear this there is a command to extract the jar jar x f then give the jar name okay now if you go to the folder can you see here I have kept my jar in the desktop folder so it created boot hyphen INF inside that it will load all the classes and Library now if we'll go here you will find something meta INF meta hyphen INF then It keep a file called manifest.mf if you open this class can
I zoom this yeah if I will open this class let me show you this class contains everything need to trigger your springboard application can you see here implementation title is this what is the snapshot version is this then what is your start class to trigger your main method this is what the class name spring boot version is 3.1.2 where It contains all the required Library this is the path I mean this is the key file spring boot jar contents So based on when you will run your jar it will pick up this particular file file
and identify who is the main class I need to run okay so this is how it execute the jar in Spring boot fine let's move to the next question that is what is the purpose of at the red springboot application annotation in a spring boot application so this is one of the most Commonly used spring boot interview question that what is the role of at the rate spring boot application annotation okay first of all this particular at the red spring boot application annotation is combination of these three annotation at the rate enable auto configuration are
the rate component scan at the rate configuration so to explain what is at the red spring boot application first you should have knowledge on these three annotation and their purpose of using Okay so to if I just want to demonstrate that let me go to the code I'll go to the main class where I have this annotation this is the class right and this is The annotation we are talking about so if you extract the source code let me Zoom this for you can you see here this particular class this particular annotation at the red
spring boot application is combination Of enable auto configuration either it's spring boot configuration and at the rate components scan now you need to tell to the interviewer about roles of each annotation if you know these three annotation then it is quite easy to impress the interviewer okay so don't worry I will go each and every annotation now so the first one at the rate enable auto configuration if I'll open this particular annotation this is the Address enable auto configuration this is one of the key features in Spring boot I'll come to this question in a
moment with detailed explanation but for now add high level just understand enable auto configuration will automatically configure various component for your application by just scanning your dependency and class path whatever the dependence you have added in your palm.xml this particular Auto configuration will scan this dependency And will load all the required jar for you okay so can you see here I have just added the web dependence in this project and this JPI have just commented out so since I have added the web dependency all the corresponding classes and all the required features to play with
this particular dependency or the starter by default this Auto configuration will enable for us okay so don't worry we'll do further postmortem of this question just keep it aside for now okay now go To the second annotation that is at the rate component scan this particular annotation enables component scanning in the package where you have your main class and its object packages so for example if you extract this particular folder where I have my main class inside this Java techie so by default since this at the rate components can annotation present in the springboot application
annotation by default it will load my Main class which is comes under com.java tech key and it's all the sub packages config controller repository service and Di right but if I have any class present external to this particular packages which is business.com which is external to this Java techy where my main class located then spring boot by default will not load that particular bin or will not add that bin to the context okay so let's prove it first so I have something called business dot common package which Is external to this com.javatiki where my main
class is located now I have this demo class and I have created a Constructor with some CS out statement fine now let me run the application first let me stop from here then I'll run from here we'll see whether this particular statement is getting executed or not so if You observe in the console we Can't see this demo class loaded by Spring boot because the by default components can look into the main class and its corresponding sub packages since it is external to the application or it is external to the root packages then it won't
load automatically for that what you can do you can tell to the spring boot go to the main class you can tilt the springboard hey at the red spring boot application annotation While you are executing your default component scan also include the base packages or scan the base packages which is com Dot what is the package name com.business.com and right you can explicitly tell to the springboot application to load your packages if you are not following the proper hierarchy okay now let me run this when I say proper hierarchy it is always good practice to
keep a root package Where you can have your main class and then Define as much as package you need inside this config okay new let's say new packages uh something like util like this it should be come in this hierarchy okay now since we have tail to the springboard to scan this particular package explicitly now can you see here in the output these particular class is loaded so that is what if You observe this is what this components can behind the Scene he does this but he don't understand if you don't understand the hierarchy main
class and its packages then he won't load that so for that explicitly you need to specify here okay that's fine now what is the fourth third annotation if you go here we understand enable auto configuration at high level we understand at the rate component scan and Its Behavior then the last one is other rate configuration so just go back to the code Then go inside this particular annotation and if I open this what is there yeah if I will open this spring boot configuration this is what the configuration annotation we are talking about right so
what this configuration annotation does behind the scene it allowed to register extra bin in the context or input additional configuration classes so for example Let's say I have created this application I want to customize my security specific config or I want to customize my documentation let's say Swagger specific config or I want to Define my JWT configure something I want to customize the configuration so how spring boot will load those config classes so if You observe here I have defined security config and Swagger config can you see here this is the security One and this
is the Swagger config so to just understand I have defined at the rate configuration to tell to the spring boot hey this is just my configuration class please import while application startup so that is the reason I have just added the sapling statement to just understand that on application startup at the rate configuration is loading these extra config class or not Whether it is inputting those class or not okay so let me run the application let's check the console are you able to see the statement no right it's only loading this demo class card because
that is what we have just defined here so go to the main class here we are telling to the spring boot to only scan these base packages so here we need to include the other packages which is our root packages okay so I can Define Com dot Java techy dot star now let me run this can you see here the output it loaded the security config and Swagger config fine so let me remove this annotation this I just added to explaining about the how this components can internally works okay that's fine now let's move to
the next question that is Can I directly use above 3 annotation in my main class instead of using other springboard application annotation if yes will my application works so simple thing instead of when we understand at the red spring boot application each combination of these three annotation right so instead of writing are the red spring boot application annotation can I use these three annotation let's see let's go to the code We'll remove this annotation we'll add at the rate enable auto configuration then we will also add other rate component scan and we need to tell
what all packages need to be scanned the way we have defined before I want it to be scan com dot Java tech key all the packages then I also wanted him to scan com Dot what is that demo right com.business Com.business and also I want to load the external configuration right so I can annotate your address input and I can Define my custom configuration which I want to load so that is security config dot class and what else Swagger config fine so these are my configuration class using address input I want to load those two
classes Fine let me run the application can you see the output application started on Port default on 8080 and it loaded my security config Sagar config also external class to my root packages so instead of writing at the red spring boot application annotation you can happily play with the other three annotation enable auto configuration component scan and other configuration so there is no doubt on it okay now let's move to the next question That is what is auto configuration in Spring boot this is really a super feature given by Spring boot developer which automatically configures
the application based on the class path dependency property settings and some other conditions so will not go with the theory we'll go one step ahead to understand how this Auto configuration picture Works under the hood okay so just go to the application let me revert it back I'll just add the red spring Boot application annotation okay that's fine let me close everything then first we'll go to the pom.xml will verify what all dependents we have added here we have added the spring boot starter wave and this is the parent dependence we are using the latest
version of spring boot 3.1.2 and lumber lumbuck is fine it's the Third party library but this is the spring bootstrator I have added only that is spring boot starter web and I have comment out the JP and all the things let me remove it for now I don't want you to be confused on the dependency hierarchy so we are using only the spring boot starter wave to understand if I'll add the spring boot starter web dependency then what all classes will be Auto configured by Spring boot okay I Just wanted to know if I'll add
this specific dependency then how this spring boot Auto configuration feature will work what all classes or what all things it will be load for me so for that to know that what you can do go to the resource go to the application.properties just tell that hey well application startup just debug it for me this Auto configuration feature tell me that what all things you can do for me or what all you cannot do For me okay so that is the reason you need to Define this key debug equal to true all good now let me
run my application so it started now there are so many logs can you see in the console while it's starting the application it will evaluate the condition okay so it will evaluate the condition then it will find some positive matches which will be Enabled by default for you after scanning your dependency and class path spring would understand okay these are the things I need to Auto configure for this particular project okay now if you will scroll down there is something called negative matches let me scroll down yeah can you see here there is something called
negative matches these are the things I will not Auto configure for you there are two things positive matches and negative matches so whatever The configuration comes under positive matches those will be enabled by Spring boot for your application again that depends on with some condition so we'll verify with the example okay so there is something called Jackson Auto configuration okay this will by default Auto configure for you because it comes under positive matches If You observe here it comes under positive matches let me search search That Jackson Auto configuration now on which basis spring boot
Auto configuration features decided that okay I need to enable this Junction Auto configuration feature for this specific application okay first thing we understand it will scan your dependency and class path then it will decide then apart from that what all things it consider before enabling the auto configuration for this specific class so to understand that what I can do I will Simply open that class Junction Auto configuration let me minimize yeah can you see here at the right Auto configuration enable this specific Auto configuration class if you found object mapper on your class can you
see here there is a condition are the rate condition on class if you found the object mapper in a class path then only enable this Jackson Auto configuration okay now this is the simple class so now if I'll search I have object mapper project can you see here it is already there in my class path it is there already there in my dependency hierarchy this class so that is the reason spring boot decided okay this is the positive match I found and then I look further if you open the class that is what is checking
another condition Conditional on class if object mapper.class is present in the class path I will happy to Auto configure this for you okay now this is the simple one okay let's move into one common class to understand how this Auto configuration works that will be more simple for us to relate the auto configuration feature this is just the simple one I explained because there are so many condition statement can be come to decided whether need to I need to enable the auto Configuration for this class or not okay that's fine so I'll go to the
run method I will search for something data source Auto configuration now if you object this data source Auto configuration it comes under let me scroll up yeah it comes under negative matches the first region it comes under negative matches because we have not specified The dependency right so if you go and check in the pump.xml we have not specified the JPI dependency that is the reason at the first step spring would scan the dependency and you don't found anything specific to the data source so you simply ignore it and just keep it inside the negative
matches now let me add it let me add this dependency let me rerun my application so now if You observe here application Failed to start forget about this error we are not going to fix that but we just wanted to know what is this Auto configuration now for this particular class the data source Auto configuration can you see your data source Auto configuration let me check the first one can you see here data source Auto configuration matched now if you scroll up it comes under the positive matches earlier it comes under Negative matches because it
didn't found the jpa dependency on our class path now it scan the class path and found that okay this guy have the jpa dependency so I need to enable this class for this particular user but how he will decide whether again I need to enable this or not go to this class there are so many condition based on the condition only the spring boot Auto configuration decided whether OK these pictures need To be enabled for this application or not so let me open that Auto configuration before SQL initialize your auto configuration forget about it I'll
come to this point now understand the Key conditions what is that class if data source dot class is present in the class path and embedded data database type dot class is present either these two either one is present Then I will enable this data source Auto configuration when I am saying it will enable data source Auto configuration for us means it will create the data source object and it will do all the steps to perform the DV operation okay now there is the second annotation condition on missing bin if this particular bin is missing then
only I will enable this data source Auto configuration for this particular Project because if You observe io.r2dbc.spa this is for reactive in rdbms so if that is enable sorry if that is disable then I will just enable this data source Auto configuration for you now the third condition at the rate enable configuration properties if these are the data source properties is available for me class loader driver class name URL username and password if these are the properties I found in the properties configuration Then only I will enable this data source Auto configuration okay now there
are several data source object will be instantiated by this Auto configuration class if you see here there is something called pool data source configuration and there is something called you will find something called embedded okay embedded database there are so many database instance or data source instance will be configured based on These conditions okay now let's brush up once again on which conditions this data source Auto configuration will be enabled for our application if data source.class or embedded data type dot class is present in the class path that is the reason at the rate conditional
on class if conditional on missing bin if this bin is missing from my application context or my class path then we'll enable the data source Auto Configuration and then enable configuration properties if these are the properties driver class name your religion name and password if these are the common properties available in the application under Properties or application.iml with the prefix playing dot data source.driverclass name dot URL username and password then only I will by default enable this data source Auto configuration for this specific project now if You observe here data source Auto Configuration matched and
these are the condition right but we are getting the error even though spring boot Auto configuration is trying to Auto configure this data source Auto configuration for us still there are some condition is not satisfied like we have not added the data source properties with like this spring dot data source driver class URL username we have not added anything so that is the reason what it is saying as Part of the error failed to configure data source URL attribute is not specified and no embedded data source could be configured fine so it is clearly saying
okay I strictly specify while Auto configuring you must need to specify the URL driver class name username and password but you have not specified it so I am just shutting down your application if you don't want to do that go with the embedded database like h2sqlr Derby So to satisfy this Auto configuration feature what I can do I will add these properties okay go to the application.properties file I am just adding the properties so what all properties I have added spring dot data source is the prefix that is what mentioned here then what is the
next field I am adding driver class name URL username and password these are the attribute or variable in this particular properties class so I have defined that Now let me run this so here you can observe application started successfully without any error it means in the auto configuration class if all the conditions satisfied then only that particular class will be Auto configured for your application by Spring boot okay but if any single condition is satisfied still it will show under the positive matches for example let me search data source Configuration matched because we have added
the conditional conditional on properties attributes then only it is completely enabled for us but there are couple of class all the things what you are seeing in the positive matches will not by default enable for you for that all the condition like this the way you have just observed in the data source Auto configuration all the condition must need to satisfied to Auto configure that feature for your Application for example you can see here right aop Auto configuration comes under positive matches but we didn't add any aop dependence in our class path then how come
it's coming under positive matches that's interesting right so if you look into the condition at the rate conditional on property spring dot aop dot Auto equal to true this particular condition is matched even though you don't have the dependency of aop still This particular condition is satisfied so if you will go and check in this particular class let me open this up Auto configuration so let me Zoom this conditional on property prefix spring aop name the attribute name is Auto having value True by default spring boot at this particular value spring dot aop dot Auto
equal to True by default enabled by Spring boot that is the reason this condition is satisfied and It comes under our positive match okay so if you want to validate what we discuss is correct or not let me take this particular property and I will go to the application.properties then what I will do I'll just keep it here spring dot aop dot Auto I will just disable it okay by default spring boot enable it so I just forcefully disable it now if I'll run my application Now if you'll scroll it up and if I go
to the positive matches I cannot see that aop related configuration class right so if any condition satisfied just keep a note if any condition satisfy it comes under positive matches until unless all the conditioner matches it won't Auto configure that feature for your application that is what you just observe the difference in data source Auto configuration we enable all the Features I mean we we satisfied all the condition then it is enabled for us but for aop auto configuration because of this single condition satisfied it was coming under positive matches since we disabled it it
is now not coming under positive matches okay this is the way to identify whether these things is configured for you or not now all the positive matches and negative matches if you want to look in a single place then you can go to the project Then if I will go to the jar section there you will find org dot spring framework this one spring boot Auto configure open this go inside the meta NF go inside the spring earlier it was there inside the spring dot factories but in Spring boot 2.5 x onwards they change the
directory and they kept all the information inside this particular file so here you can find all the positive And negative matches together in a single place so just go to the any class and try to understand how the condition are implemented or how this particular things will be Auto configured okay so this is just to understand the internal flow of Auto configuration it's not dead easy to remember all the classes how they are Auto configuring in Spring boot but if you know how this Auto configuration work the way we have just derived for aop auto
configuration and Data source Auto configuration it will be easy for you to implement your own custom starter or it will easy for you to understand the internal flow of the springboard if you find any exception you can easily identify okay this condition might not be satisfied that is the reason this is not Auto configuring for me okay cool so I believe this Auto configuration knowledge is clear for you you also understand how it internally works okay Let me clear everything now let's move to the next question that is how can you disable a specific Auto
configuration class in Spring boot okay we understand how this Auto configuration work or at which scenario it will evaluate to other Auto configure or not but there could be a use case I want to disable the auto configuration I don't want spring boot automatically load that configuration for me how can I Do that the approach is very simple go to the main class then in the main class this particular annotation are the red spring boot application annotation you can Define which Auto configuration you don't want to be enabled by default for you okay so there
is a attribute called exclude and you can Define which class you don't want I let's say I want this data source Auto configuration right okay let me check the class name Or let's take this one aop Auto configuration this is the class copy this class I don't want this data source Auto configuration dot class will load automatically for me so for that I'll just Define like this okay now this data source Auto configuration will not be enabled for me because I want to exclude it similarly if you want other class you can Define let's say
aop Auto configuration I don't Want to be enabled for me fine so what I'll do I will just remove it from the properties file because we just disable it right I want spring to be enabled and since I have defined this particular exclude this will not be enabled for me so let's run our application you will not find these two class inside your positive matches that is what we understand from the aop auto Configuration I mean spring boot Auto configuration right so it started now let me search this class inside the positive matches that class
is not there right now can you see here exclusion what we have excluded data source Auto configuration and aop auto configuration these are the two class we are just excluding from the springboard to automatically configured for me even though you found the dependency even It's satisfied all the condition okay this is another tricky interview question how we can exclude Auto configure class in your springboot application okay so what I'll do I'll just copy this because anyway I am going to share this source code with you so I'll just comment it fine so is this the
only way we can exclude no you can also play with the properties file so what you can do go to The properties file then in the properties file let me Zoom this for you simply let me add it down you can add the key something like spring dot Auto configure dot exclude spring dot Auto configure dot exclude you can Define array of the classes so I will just Define single class you need to provide the fully qualified package okay org.spring framework.boot aop Just paste it here and what is the class name you want to exclude
the class name is aop auto configuration just add in the properties similarly if you want to exclude multiple classes just add a comma and give the package on class name okay this way also you can exclude that's fine so you can run and test this particular properties file instead of playing with The annotation exclude attribute now let's move to the next question That is how can you customize the default configuration in Spring boot this is very straightforward approach so let's say the default Port spring boot is running is eight zero eight zero I want to
overwrite that behavior how can I do that very simple step right either you can play with the application.properties file or application.to ml file to override the default configuration in Spring boot for example let me create application.aml File I want my application to run in a different port the default Port is 8080 if I will not specify any port it will be run on port 8080 now if I'll specify my own port or I am just overriding the default behavior let's say I want to run it on 8181 now my application will run on Port 8181
so similarly we are overriding the default properties here also right I am giving my own data source URL username And password rather than getting the default from Spring boot okay so two way you can overwrite the configuration in Spring boot by defining the properties file and by defining the application.to ml file I will cover these two application dot properties and application.aml there are couple of question I have to cover that I will cover as part of my next video okay for now just understand we can provide our configuration details In these two format either dot
Properties or dot yml if you want to override anything just Define that since I have defined the port 8181 now if I'll run my application it won't load in the default Port which is eight zero eight zero now you will get in the console that the server is started on Port 8181 so let's verify that let me filter that can you see here Tomcat initialized with Port 8181 before when was not giving it Was getting the eight zero eight zero that is the default Port okay this is how you can override the configuration in Spring
boot okay now let's move to the next question that is how spring boot run method Works internally so this is another commonly asked tricky interview question in springboard will debug and will understand rather than going with the theory okay at high level when you trigger the when from the main Method the run method will be triggered it first create the application context but before it create the application context it load the environment properties and then it check the application type whether it is the servlet application or it is the reactive application or it is the
default application I mean the Standalone application it checked that then based on that it creates the application context then it register all The annotated bin into that specific context then at the end it kicked up embedded Tomcat container to run our jar file so we already discussed that where our jar is there and how in the jar the main class name is defined okay it pick that class name and it trigger the execution fine this is what we understand from the theory now let's deep dive by debugging this particular concept let me go to the
project first let me disable this okay otherwise There are so many statement we can see in our console I mean I'm ending up with the scroll down that's fine I'll just disable it now for I'll just stop it let me close everything apart from this main class so if You observe any spring boot application the execution always started from this main method right insert the main method it calls spring application dot run method by taking the argument is Your class name and if any system argument you are giving at the wrong time fine now this
is the class and this is the method so if I open the source code of this spring application.java class and if I'll go to the run method which is Define above can you see here this is the run method so first let me add a breakpoint here then you can see here configurable environment it load all the environment Properties then it create the application context then it refresh the context and started the embedded Tomcat okay we are going to validate these three steps before that if I don't want to debug and you understand whether it
is creating the context or not just Define a variable what is the return type of this method guys spring application.ron is giving the application context okay this is the Straightforward answer but to just understand internally how it create the context what are the things it consider before creating the context I am just debugging and showing you guys to get the complete picture about this internal flow okay now this first statement cleared that it create the application context now let me add the breakpoint okay we already added the breakpoint that's fine let me add a breakpoint
here Let's run it in the debug mode request came to the main method can you see here let me scroll it down yeah it came to the main method now it will go inside the run method can you see here it came to the wrong method let me Zoom this for you now in the run method okay let me add a breakpoint yeah I already added that's fine I'll just Skip and I'll come To the this step now this is the place it will prepare the environment for you while preparing the environment it will look
into your both the file application.properties file and application.aml file it will load everything I'll show you that okay so I will just come to the next one now if I'll expand this environment and if I expand this property Source property Source list You will find here can you see here there is something called origin tracked map property Source okay there are two Origin tracked map property Source One for your properties file one for your yml file so if you open this if we'll open the source this is what your properties file right these are the
attribute we have defined in our properties file now if we expand the seventh index of the origin drug map property source and if you'll expand the Source it load from the yml file this is clear right it load from the both properties and yml file using this origin tracked mapped property source and you can see the value that's pretty clear that you first load our environment then I mean it's very simple I don't want to explain whatever the banner you are seeing here it create the banner here print the banner here that that is not
something important so I am just Skipping it okay now the key point that it creates the application context now I will open this application context method I already added the breakpoint here fine now let's move into the statement it creates the application context while creating the application context it check the application type so to validate that if you open this method this comes under application context Factory this create method can you see Here and now if I'll check its implementation default application context Factory reactive web server application context Factory and servlet web server application context
Factory there are three implementation of application context Factory I mean anyway at the end you will get the application context but which implementation you will return I mean which implementation you will get that is matter right and again that Depends on the web application type you are using so since we are using the servlet type let me add a breakpoint here okay fine now let's go back to the point yeah this is where it will create the application type now if I evaluate the web application type currently we are using the serblet since we added
the web starter dependency so that will consider that your web application type is servlet if you don't Add any web application type then it will be the default one fine now let's go inside this create method in create method let me Zoom this for you it evaluate a condition what is that if web application type not equal to serblet then null otherwise just create the context in our case it will goes to this else block so I will just add a breakpoint here okay and it will return this particular Annotation config servlet web server application
context so let me go inside this class I have added the breakpoint here that annotated bin definition reader and Here If You observe it registered all the annotated class remove the breakpoint this is the Constructor and register base packages if you have defined anything inside your components scan or if you have defined anything on your other spring boot application Fine let me debug it further I'll skip it simply okay because we understand it load the environment it check the application type then it creates the context so I'll directly go here it came to this class
now it will execute the Constructor and it will register all the bin to the application context let me move from here all set right we'll prepare our context first it create the application context Of type let's see which one guys annotation config servlet web server application context not reactive not default one okay and inside that annotated classes after this refresh context you can see all the bins or the class what you have defined will be loaded here okay now let me verify what is there in the context in Factory bin definition map okay these are
the default class Can you see here interview application this is the bin it loaded and let's see what is there inside yeah this is the bin it loaded right we have this specific bin with annotate at the red spring boot application do you have any other bin defined with other rate component no red okay this is at the rate component but I just want to verify now we don't load right because we understand how the Other component scan works so it will only scan the main packages and its corresponding sub packages which is config controller
because it is present inside the business dot common this is external to our root package so that is how it will not not load that specific class I mean we understand that in the previous question how these other components can work okay now once it refresh the context I don't have anything to show you that how it kicked Up the embedded Tomcat but let me search a class embedded Tomcat okay so let me add a breakpoint if it comes to this particular place it's good otherwise it will just start a tomcat server for you see
here there are also the way I mean you can tell the spring would okay around the JT server okay Run the under two server I mean these are the different type of embedded server okay I'm not sure but yeah let's move further okay and we'll see if breakpoint coming to here or not it's still loading yeah it came to the Tomcat servlet now what is the things is being done here it created the factory then connector customizer order stream To list context customizer okay let's see put it there in the context let's evaluate it nothing
is there right so yeah it's fine I mean I don't want to div dive it how it started the Tomcat that is fine that we understand it create the embedded embedded Tomcat container Factory for us to kick the power jar fine now just simply move it finally it started right So what we verified it creates the application context before it create the application context it check the web application type and it also load the environment properties then it registered all the annotated bin into the context then it kicked up the embedded Tomcat container right so
let me mute and close this flow so I believe this is pretty clear for you to understand how the wrong method Works internally in Spring boot when we Run the main method or when we run the springboard application internally what all things it loaded and how it evaluate or how the things is being worked under the hood we understand it completely okay so fine let's move to the next question that is what is command line runner in Spring boot so in simple word this command line Runner will help you to execute your preprocessing logic or
execute your data setting logic when I say pre-processing logic if anything you Want to run at the time of application startup you can keep those code inside the command line runner for example if I'll go to the source code and if I close everything go to the project I'll just write it here okay so there is a interface given by springboard that is command line Runner if you go inside this command line Runner it contains a method called run method okay and this is a functional Interface so since this is the interface you need to
overwrite the method either you can do it in the same class or you can create a separate bin or you can create separate class and implements it from command line Runner and you can use it so let me overwrite the wrong method okay don't be confused there are two run method run method of this spring application let me remove this or just let me comment this There are two run method one run method from command line Runner and one run method from spring boot application right now if anything you want to execute at the time
of application startup you can keep those logic here let's say you want to establish the DB connection quickly after the application startup or you want to populate some data to the DV On application startup or anything any pre-processing logic you want to perform then you can keep those piece of code inside this particular run method now the question here again the reason is not what is command line interface interview will not ask you what is command line interface or interviewer is not interested to understand the theory about the command line or not he just wanted
to know how You are approaching about this run method okay so we have to run method run method from the command line Runner and run method from the spring application now which which one will execute first that is the tricky question here so to verify that I'll just add a CS out statement here fine I just added statement command line Runner run method executed then I will just add This is from Spring application right so the genuine statement the command line Runner run method will execute after this spring application run method I'll prove it right
away the spring application run method will execute first then next this particular run method will execute okay so I'll simply let me stop this I'll simply run this you can see right first it execute the spring application Run method then if we'll scroll down it execute the command line Runner run method so command line Runner run method will execute after the spring application run method this is clear for you right fine cool so that's it for today's session let's wind up here and in coming days I will come up with more questions okay so I
hope you found it valuable and learn something new today so if you have Any question suggestions or if you have any spring boot questions or interview experience you'd like to share please feel free to leave them in the comment below so until next time keep learning I will see you in the next video that's all about this particular video guys thanks for watching this video meet you soon with A New Concept