Annotations in Spring boot with Explanation| Spring boot Interview Questions and Answers Code Decode

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome to code decode today in this video we are going to cover a very important and mostly used springboot annotations interview questions please like share and subscribe to support us and we are setting a like target of 500 likes so we're going to start with a very basic thing what is spring boot and what are annotations so spring boot as you all know is a very popular java framework and springboard is actually built upon spring framework and why was it introduced in i.t industry that is to minimize the amount of configuration and boilerplate code that you need to get started with but spring boot is the way to create quickly at least configuration release boilerplate code a code which is capable enough to be deployed in fraud java started giving support to annotations only from java 5. how did spring manages itself and its dependencies prior to java 5 was introduced so prior to java 5 the behavior of spring framework was largely controlled through xmls so while covering the spring interview questions we have seen that you can inject your dependencies and manage your dependencies and let this con the container know where to find it through three ways first through xml configuration secondly through annotations and thirdly through java configurations using address configuration and at the rate bean annotations after java 1.5 the life of spring people also become much more easier because they have started using annotation now what are annotations so annotations are nothing but metadata for either your compiler or jvm to understand what this class actually do so let's start with very first and very important spring boot annotation which is 8th grade spring boot application so either its spring boot application is nothing but a combination of three annotations internally so i'll give you a live demonstration so whenever we created an any kind of application like i'll take an example of our rest application itself so the very first thing that actually loads and actually runs is our main class you need this annotation to kick start your application right now this at the right spring boot application is consisting of three major annotations connected configuration and enable auto configuration and component scan i'll give you a quick overview and then we will get into inside each and every of these annotations so as said earlier spring boot is not standalone it is built upon spring so spring boot annotations also uses existing spring annotations so if you can see here component scan it's a spring framework annotation and rest of them are spring boot annotations so you can see here spring boot auto configure and component scan is existing spring framework now coming to these three the very first is at the red configuration consider this as accurate configuration it is just inherited by a string boot to make it spring boot configuration if i make you go into this annotation you can see it's nothing but at the right configuration so even though your spring boot configuration is a spring is in spring boot framework it actually uses ignite configuration which is internally a spring framework annotation only either it configuration marks or tags your class that it is a source of wheel so when you use aggregate spring boot application internally it also has three things at the rate configuration this particular annotation says that this particular class is a source of all the beams available in your application now you will ask me these are just two lines how can you say these two lines are source of all the beans that exist in your application so for that spring and spring boot application also has 8th rate enable auto configuration and at the rate component scan agreed component scan says that particular package is the default package goes can each and every class inside each and every sub package of this base package and find out all the beans and create that bean in the container spring container so if i say that this class is the base and will be the class containing all the beans in this whole application then it's true because component scan is the one who is responsible for actually scanning each and every sub packages and each and every class in that sub package find out these beans and create that bean in container and what does this enable auto configuration do enable auto configuration enables this creation of beans in container we will go in deep in all these three in few minutes but remember this accurate springboard and application is combination of three at the right configuration which says i am colon sold a class which is responsible to create and give you all the beans to container component scan is the one who is responsible for actually scanning and auto configuration is actually responsible for creating those scanned beans as a bean in container so all the three together makes your life very comfortable and easy so we can say that this particular annotation cannot exist alone it needs stream framework annotation also in its com as its own annotation to work now you will ask me what are these target retention documented inherited you just told about the three what are these four above these so i'll tell you quickly what these annotations actually do at the right target means it specifies at which type the annotation is going to be used it should be used in either class or method or constructor we are going to use it so we all we all know that springboard application we always use on a class it's a class level annotation and that is why we have used element type which is nothing but the enum we have used time so type is used when you are going to declare that annotations over a class or over an interface or enum or record now if it would have been an annotation which can be used in a field or variable then it could have been used as target element type dot field similarly method if if particular notation can be used on method now example of this is at the rate override the parameter constructor annotation type package and many more with this we understood that target is on which place are you going to put this annotation on now what is retention so retention is to define in what level your annotation will be available so there are basically three levels first when we write our source code secondly when we compile our source code and thirdly when we run our source code with the help of jvm so there can be a case that you put an annotation but it is discarded during compile time so it is only for reading purpose just to make the code readable so that's when only till the source code is available that particular annotation is retained and during the compile time it is discarded so it will not be available in even the dot class files second retention policy is class so it refers to the dot class file so it this particular annotation when it is in the retention policy of class will be available to compiler but not to jvm during runtime so you can see that annotation in dot class file the last is retention policy dot run time which is our case so here our retention policy is run time so this annotation that we have used here at the red spring boot annotation application is going to be remained even in dot class file even in dot java file that is the source code and also will be available during the runtime also so it will be available to compiler also and it will be available to jvm also so during runtime it sees okay it is a springboard application that means i have to kick start my application with this particular class go and find a scan each and every base package each and every class and create a bean using auto configuration in the container so this was all about retention now what is documented and what is inherited so we'll see that by default annotations are not inherited to sub classes annotations are simple interfaces and they are not inheritable until unless you don't use accurate inherited annotation so at that inherited annotation marks the annotation to be inherited to all the subclasses of that particular annotation so this is inheritable either a documented what does it do it simply means that the annotation is inclusive in the documentation let's go into documentation of it again so it says if annotation is typed and annotated with documented by default tools like javadoc so whenever we want to document our apis we use javadoc for documentation by default a tool like javadoc will also display these annotations in the output internet document is just to document this annotation in your javadocs so now we know all the four of them now we'll go inside all the three of them so we'll start with a great configuration most of the thing i've already told you at the rate configuration says on whichever class you annotate it it will become a source of all the beans so tanks the class is the source of all the beans for the application context so for the whole application you will get bean only in these in this class now just two line of code is not the bean that you're going to get the component scan and auto configuration does the magic it goes and scans and creates bean and container for the spring boot configuration spring boot configuration is an annotation which is actually annotated with configuration only so at the rate configuration is of spring framework we have just used it in spring boot configuration in spring framework in spring boot framework and modified it with some extra methods nothing else it's nothing else but internally at the right configuration only a very important interview perspective question is if you annotate a class with accurate spring boot application or at the right configuration does it become a bill so the answer is yes whenever you annotate anything with at the right configuration it becomes a bean for container the reason being the when you go into the source code of this configuration annotation you can see it's a component accurate component and whenever a class or an interface is annotated with accurate component it becomes a b for the container whenever you have a main class this is also a bill so you cannot see any kind of bean description here explicitly but this spring rest api demo application is a bean because internally springboard application has a great configuration which internally is nothing but at the right component so yes your main class is also been annotated with accurate configuration which is nothing but internally at the right component now what is enable auto configuration so you have a form.xml right so all the dependencies that you add here are set of jars that you download from central maven central repository right so whatever you define in form.xml or you explicitly add in your class path using the build path automatically what enable auto configuration does is it goes into each and every of those class in that particular set of jars and automatically configures and scans the bean out of these jars i'll give you an example if you have spring boot starter web dependency so do we have that yes we have the spring boot starter web dependency that means internally it has embedded tomcat dependency also so spring boot automatically configures auto configures tomcat and spring mvc in your application so if you can see there is no explicit configuration in any of your properties or anything we just added a jar and spring boot is smart and intellectual enough to automatically configure tomcat for you how is that possible that magic is done with auto configuration so even if you have added a explicit jar using the build part then also spring boot is intellectually capable enough to configure your tomcat server web server factory now you will ask me what is tomcat serverless web server factory this is a class responsible for instantiating a tomcat i'll show you quickly so this is the class responsible for instantiating a tomcat at port number 88 a very important thing from interview perspective if you don't want to embed a tomcat serverlet but you want to create an own web server then there's a serverlet named as servletweb server factory so i'll show you that also so this is servlet web server factory this interface will help you to create and link your web server in your spring application so if you have your internally created configuration for your xyz web server and you don't want tomcat embedded server then you can create it and then auto configuration will take a step back it says okay i'm not going to configure default tomcat for you since you have created your own configuration for your own web server i am not going to auto configure tomcat for you rather you can go ahead and have your own configuration so that is the beauty of auto configuration auto configuration takes the least precedence when you define your own configurations so that's the beauty of enable auto configuration the package of the class that is annotated with accurate enabled auto configuration has specific significance and is often used as default one since i have a direct enable configuration like this class this package is named as default package that com code decode spring rest api demo is the base package and dot controller dot entity dot repository dot util are the sub package of this package only so you should always define your enable auto configuration in the default package so that spring can scan the base packages like controller entity reports and auto configure the bills found in the classes in these sub packages so it's generally recommended you should place your enable onto configuration or indirectly or springboard application which internally contains this in the root packet so that all the sub packages and classes will be searched using the component scan and it should always be used within the right configuration annotation now you will ask me why it should always be used with configuration because it is going to configure beans in your container so you should tell explicitly the jvm that this class is going to be the one who gives beans to the container we have two more uh ways in which you can exclude your auto configuration so the first is exclude and second is exclude name so if you want to exclude list list of classes from your auto configuration you can do that with this way at that enable auto configuration exclude your auto configuration jdbc template class so i don't want that my jdbc class should be auto configured i i have some other template say i have some orm template to configure my database connectivity then you can exclude it explicitly using this way if you you know the fully qualified class name and you want to add them in the list then you can also use aggregate enable configuration with exclude name so with exclude name you have to for sure give a fully qualified list of class names that you want to exclude while auto configuration so what rtconfiguration does is automatically creates bin when you exclude a class the b for that particular class is not going to be created in container so jdbc template auto configuration class with this line or this line is not going to be created in your container and your container will not be confused when you ask for a database bean third part is component scan we have covered these two will quickly see the component scan so when we work with spring we annotate our class in order to make them into spring beams furthermore with component scan we tell spring where to search for these annotated class so when we come to spring we want that our controller should be found we want this controller to be available at run time so that we can hit our find all apis or get apis then we have to mark it as 8th grade controller this is the way to let container know this is a bean but how will you tell container where to find it so here with component scan you tell the container go to the base package find all these sub packages so these are the force up packages scan each and every class in each and every sub package and create a beam for all these annotations like categoric controller at the red service android repository at the rate component at the rate b net rate entity create the beans keep it with you we will be needing at run time we can tell with component scan the spring where to search these classes components can it is used with accurate configuration similar to accurate enable auto configuration it is always used with configuration similarly component scan should always be used with configuration because when you scan it that particular package in class becomes the source of beans for containers so you should explicitly annotate it as a direct configuration so that you can tell the spring the packages to scan for annotated components the components can without arguments still to scan current package in all of its sub packages so currently you're going to search for this package and all its sub packages so currently i'll give you a demo quickly if i run this find all i get my outputs right so this is the data which i have in my database now suppose if i modify this package name and make it code decode dot test dot spring rest api demo what will happen so this becomes my base package right now my base package is from code decode test so these are not the sub packages of this package so now if i try to hit and run i am going to find no controller found there is no controller available why because this controller is available in com.codecode.this package our base package is formed on code decode.test so this is not the sub package and hence your code breaks so let me quickly get this back now if i run it it will be all fine the importance of having the right component scan at the default package because the default current package in sub packages are only going to be scanned otherwise it's not going to be scanned and since the controller's beam is not created with spring at run time spring is not able to give you back the bean for your controller and hence your api was failing now we can filter the scanning oh i have so much to cover i have the filters to cover i have types of filters i have type exclusions i have difference between auto configuration spring application i have so much to cover please let me know in the comment section if you want to want me to cover more of this let me know in the comment section if this video is going towards the right direction and you want me to cover each and every annotation used in the spring boot thank you
Info
Channel: Code Decode
Views: 57,781
Rating: undefined out of 5
Keywords: spring boot annotations interview questions, Spring boot annotations code decode, spring boot interview questions code decode, annotations in spring boot, spring boot annotations, Spring boot interview questions code decode, code decode, annotations in spring boot with explanation, annotations spring boot, codedecode, spring boot interview questions and answers code decode, annotations in spring boot interview questions, annotations in spring boot code decode
Id: NKhNnBvufGI
Channel Id: undefined
Length: 19min 56sec (1196 seconds)
Published: Wed May 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.