Spring Boot

Commonly used Spring Boot Annotations

  1. @SpringBootApplication
    • It is a combination of three annotations @ComponentScan, @EnableAutoConfiguration, and @Configuration.
  2. @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.
  3. @Configuration
    • It is a class-level annotation.
    • It generates bean definitions and service requests.
  4. @EnableAutoConfiguration  
    • It auto-configures the bean that is present in the classpath and configures it to run the methods.
  5. @Component
    • It is a class-level annotation.
    • It marks Java class as a bean.
    • A Java class with @Component annotation is looked for in the classpath.
    • If a class is found, the Spring Framework will configure it in the application context as a Spring Bean.
  6. @ComponentScan
    • It scans a package for beans.
    • One can use this annotation along with the @Configuration annotation.
  7. @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.
  8. @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.

Some other annotations

  1. @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.
  2. @Service
    • It is a class-level annotation.
    • It tells the Spring that class contains business logic.
  3. @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.
  4. @RequestMapping
    • It helps to map the web requests.
    • It consists of optional elements. For Instance consumes, header, method, name, params, path, produce, and value.
  5. @GetMapping
    • It maps the HTTP GET requests on the specific handler method.
  6. @PostMapping
    • It maps the HTTP POSTrequests on the specific handler method.
  7. @PutMapping
    •  It maps the HTTP PUT requests on the specific handler method.
  8. @DeleteMapping
    • It maps the HTTP DELETE requests on the specific handler method.
  9. @PatchMapping
    • It maps the HTTP PATCH requests on the specific handler method.
  10. @RequestBody
    • It binds HTTP request with an object in a method parameter.
  11. @ResponseBody
    • It binds the method return value to the response body.
  12. @PathVariable
    • It extracts the values from the URI.
  13. @RequestParam
    • It extracts the query parameters from the URL.
  14. @RequestHeader
    • It helps us get the details about the HTTP request headers.
  15. @RestController
    • It is 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.
Scroll to Top