Skip to content

Latest commit

 

History

History
53 lines (37 loc) · 2.53 KB

File metadata and controls

53 lines (37 loc) · 2.53 KB

Spring Boot Core Concepts

Inversion of Control (IOC)

  1. Instead of creating the object by user itself, if we ask spring/ApplicationContext to get the bean/object of it, this process is referred as inversion of control (IOC).
  2. IOC is kind of container, it will store the all object (i.e beans).
  3. It is part of Spring.
  4. ApplicationContext is way to achieve the IOC container.
  5. How does IOC works ?
    1. It scans the packages and stores the bean of it.
    2. How does it know which class/bean to store ?
      1. It will store all the bean/class which has annotations as - @Component, @Component - helps the class to automatically register as spring bean.

Bean

  1. Object store at the IOC Container as referred as beans/objects.
  2. If you want to make a single instance of an object and you wanted to use the same instance globally at your application at different places, then you can make a bean of that class and applicationContext may contain that bean.
    • Else if you do not use bean then you to create the instance of that class every time and use it.

SpringBootApplication

  1. This annotation can only be used once in the entire spring application
  2. This is also referred as the entry point to the application.
  3. What does @SpringBootApplication do ? It is made with below 2 annotation
    1. @Configuration: This is used at the class level, after using this annotation we can defined @Bean inside the class using methods. And tell spring to make object of these bean as well when the application starts.
    2. @EnableAutoConfiguration: This annotations helps spring IOC to autoConfigure/auto register the bean defined in classpath or jar. Ex: for different databases etc.
    3. @ComponentScan: Scan and search to beans (searches for @Component, and insert the bean in IOC container/ApplicationContext). Search is done only in the defined package only.

Dependency Injection

  1. @Autowired / @Inject : These annotations are the annotations which says - to give the object defined in IOC container. This is field injection.
         import org.springframework.beans.factory.annotation.Autowired;
    
         @Autowired
         private Dog dog;

Annotations

  1. @RestController: This contains @Component which will help to create bean of this class, also it map the response to json.
  2. @RequestMapping: This help us to add additional route to our mapping.

What is classPath ?

  1. classPath - list of jar or directories which JVM uses.