Spring Boot

Commonly used Spring Boot Annotations –
  • @SpringBootApplication – It is a combination of three annotations @ComponentScan, @EnableAutoConfiguration, and @Configuration.
  • @Required – 
    • It applies to the bean setter method.
    • It indicates that the beans with annotation must populate at configuration time with the required property, else it throws an exception BeanInitilizationException.
  • @Configuration – 
    • It is a class-level annotation.
    • It generates bean definitions and service requests.
  • @EnableAutoConfiguration – It auto-configures the bean that is present in the classpath and configures it to run the methods.
  • @Component – 
    • It is a class-level annotation.
    • It marks Java class as a bean.
    • A Java class with @Component annotation is searched in the classpath.
    • If a class is found, the Spring Framework will configure it in the application context as a Spring Bean.
  • @ComponentScan – 
    • It scans a package for beans.
    • One can use this annotation along with the @Configuration annotation.
  • @Autowired – 
    • It auto wires spring bean on setter methods, instance variable, and constructor.
    • In other words, it allows automatic dependency injection. The spring container injects the dependent beans by matching data-type.
  • @Controller
    • The @Controller is a class-level annotation.
    • It marks the Java class as a web request handler.
    • By default, it returns a string that tells the route to redirect.
    • It is mostly used with @RequestMapping annotation.
Some other annotations –
  • @Bean – 
    • The @Bean is a method-level annotation.
    • It act as an alternative of XML <bean> tag.
    • This annotation tells the method to produce a bean that a Spring Container can manage.
  • @Service – 
    • It is also used at the class level.
    • It tells the Spring that class contains business logic.
  • @Repository – 
    • It is a class-level annotation.
    • The repository is a DAO(Data Access Object) that access the database directly and perform all the operations related to the database.
  • @RequestMapping
    • It is used for mapping the web requests.
    • It consists of optional elements. For Instance consumes, header, method, name, params, path, produce, and value.
  • @GetMapping – It maps the HTTP GET requests on the specific handler method.
  • @PostMapping –  It maps the HTTP POSTrequests on the specific handler method.
  • @PutMapping – It maps the HTTP PUT requests on the specific handler method.
  • @DeleteMapping – It maps the HTTP DELETE requests on the specific handler method.
  • @PatchMapping – It maps the HTTP PATCH requests on the specific handler method.
  • @RequestBody – It is used to bind HTTP request with an object in a method parameter.
  • @ResponseBody – It binds the method return value to the response body.
  • @PathVariable – It is used to extract the values from the URI.
  • @RequestParam – It is used to extract the query parameters from the URL.
  • @RequestHeader – It is used to get the details about the HTTP request headers.
  • @RestController – It can be considered as a combination of @Controller and @ResponseBody annotations. 
How does it work ?
  • @EnableAutoConfiguration annotation allows Spring Boot to automatically configure your application based on the dependencies you have added to the project.
  • The class that contains @SpringBootApplication annotation is the entry point of the spring boot application.
  • To run the Spring Boot application, this class should have the main method.
  • @SpringBootApplication annotation includes Auto- Configuration, Component Scan, and Spring Boot Configuration.
  • SpringApplication.run() inside the main method will bootstrap String application as a stand-alone application.
  • It creates an instance of ApplicationContext and loads all the beans.
  • It then runs the embedded Server.