Spring Annotations for Experienced Developers #springboot #java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this video we will be understanding  spring annotations such as configuration Bean pre-   destroy and post construct component  scan component property Source service   repository autoi and qualifier annotations without  further Ado let's get started Namaste welcome to   bit science all love the secrets of life and  Technology with our comprehensive biology and   comy Science Education what are spring annotation  spring annotation are special annotation that are   used to annotate classes methods and Fields  these annotation are then processed by the   spring container to configure beings and manage  their life cycles so how does spring annotation   work one way to visualize how spring annotation  work is to think of them as instruction for the   spring container when the spring container  starts up it scan this class path for classes   that are annotated with spring annotations the  spring container then creates beans for these   classes and manages their life cycle according  to the instruction in The annotation let's see   some of the most commonly used spring annotations  configuration annotation is used to indicate that   a class declares one or more beans methods so what  it means that this is one project right where is   the metad data of this project it is actually in  the pong this is the metad data of each so these   are the metadata what is your project structure  this com. XML this is the com. XML is it is under   the artifact demo okay this is how your metad data  so similarly we for each class whatever you you're   going to create right so you can add annotation  so this is the first annotation you are getting   which which is spring boot application if you  write this spring boot application on the main   it has to be on the main then what will happen it  will execute it will automatically execute and you   whenever I'm running on this application right  right click on this and look what is happening   you will get the spring running on your terminal  so this spring annotation has it meanings this   spring Andro is a combination of configuration  enable auto configuration and component scan   what are these three thing if you write just this  three thing you don't need to write this whole   this three thing in separate in the other way we  can do is we can write configuration like this one we can also write instead of  writing this configuration enable auto configuration and we can to write  component scan component SC okay   instead of writing this one line of  statement you might have to write this   three statement to tell that to run  this application okay this will also work if I want to run this application this  will work the same way it is running so   instead of writing this you just need to write  one annotation that is the whole idea okay so   what are these three annotation configuration  enable autoc configuration component scan this   three annotation are you can see in the you  know screen component scan annotation used to   configure component scanning directive for use  with configuration classes this means that you   telling the spring that please scan this package  means when we write component scan we have to also   say the what is your base package which is com  do example you have to tell this if you don't   tell then by default wherever this package it  is actually under this com. XML demo that this   will treat as this is the main main component scan  means other than outside of this if if you write   a class which is not present under this package  then that will not treat do not scan everything   it will not scan any spring specification okay  okay so for now just understand can your package   this is used so under configuration you write  beans Bean annotation is used to indicate that a   method produces a bean to be managed by the spring  container what it means that under configuration   uh so under this you write beans this is how we  write beans one one beans so beans are methods or   function so you write some method sub method this  is one B okay so you can either write under spring   up application that is also fine or you can say  configuration and write under this okay both are   fine so this configuration helps you to write all  the beans so all beans will be under configuration   then we have pre- destroy and post construct  annotation these annotation are used to indicate   method that should be called before and after a  bean is destroyed or created respectively means   what happens there is a cycle in Spring there's  this life cycle of spring where before uh creating   the be and after creating mean this being is been  created this hole has to be created before uh this   reach to this post construct and pre- destroyed  so before destroying something happens we also   can use this init method uh writing a bean like  like this init method so instead of writing this   you have to just use pre this two annotation the  next is component annotation used to indicate that   an annotated class is a component such classes are  considered as candidate for autod detection when   using annotation based configuration and class  path scanning to create a spring controllers class   we need to create a Java class and annotate it  with component annotation the component annotation   tells the spring container to scan the class for  classes that are annotated with this annotation   and create Beam for them this read message method  will be called when the controller receive an sttb   request this method should return return a string  that will be response to the request in this   example the read message method simply Returns  the string spring application is running in this   step we will be adding get mapping annotation to  the read message method this annotation tells the   spring container that this method should be  called when a get request is made to specific   part in our case the part is SL read message so  when a user makes a get request to this URL of   our application the read message method will will  be called the next is service annotation is used   to indicate that annotated class is a service this  annotation serves as a specialized offs component   annotations allowing for implementing classes to  be autodetected through class path scanning let's   see this example I'm creating a product order  of class inside this we are injecting a product   repository being into product service class  using the auto annotation so what is autowire   annotation autowire annotation used for automatic  injection of beads spring qualify annotation used   in conjunction with autowire to avoid confusion  when we have two or more being configured for the   same time at the end will annotate with with at  the rate service annotation the service annotation   tells the spring container to scan the class  path for the classes that are annotated with   this annotation and create means for them the next  is repository annotation used to indicate that an   annotated class is a repository this annotation  servers specialized of component annotation and   advisable to use the Dow classes repository are  responsible for storing and retrieving data from   database when you extend the CED repository  interface you're inheriting a set of method   that allow you to perform C create read update  delete operation on the the data in the database   for example if you have a product repository class  that extends the GED interface like here you will   have methods like save find by ID find all and  delete methods the reposit reannotation is not   required but it is a good practice to use it Mark  your repository classes this will help the spring   country to understand that these classes are  repository and should be treated accordingly   the next is the property annotation is a spring  annotation that is used to load properties from   a file or a s force the properties can be used to  configure the spring application for example the   following Antion loads property from a file  named application. property in the class at   the N property Source within the bracket class  path column application. properties the property   Source annotation can be used to load properties  from multiple files or resource to this you can   specify multiple value attributes for example  the following annotation loads property from two   file application. property and database property  we will write like this property Source within   packet we will use curly bres within that we'll  use two property one is class path application   property and is class path database properties  I hope I'm able to make this understand in this   video we have learned about spring annotations  spring annotation a powerful way to configure   spring beans and manage their life cycle if  you're interested in learning more about spring   inv I recommend checking out more about these  videos I hope you find this video informative   and that you learn something new and if you  have any question please feel fre to leave a   comment below thank you for watching I a and this  has been M SS I will see you in the next video
Info
Channel: Bit Science
Views: 9,627
Rating: undefined out of 5
Keywords: Spring annotations, Spring Boot annotations, Spring configuration, Spring bean lifecycle, Spring autowiring, Spring annotations tutorial, Spring annotations explained, Spring annotations for beginners, Spring annotations for experienced developers, Spring annotations best practices, Spring annotations examples, Spring annotations cheat sheet
Id: uVl607o9M3U
Channel Id: undefined
Length: 10min 50sec (650 seconds)
Published: Wed Sep 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.