Spring boot basic annotations

Introduction:

Spring is a popular java application framework for enterprise application. and spring boot is the next evolution for spring framework. It helps to create stand-alone, production grade spring based applications with minimal effort.

spring boot completely based on annotation configurations. and it doesn't use xml based configurations. and it's a form of metadata which provides data about a program that's not a part of the program itself.

Spring boot basic annotations:

Below annotations are frequently used by developers.

  1. @Bean: annotation used at method level. used to instantiate bean object and that will be managed by spring container. and created method should be inside any stereotype annotated class.

  2. @PreDestroy: this annotation used at method level to remove our bean from application context. if spring bean scope is "prototype" then it's not managed by spring container means predestroy method won't be called.

  3. @PostConstruct: this is also used at method level. annotated method will be execute after dependency injection perform any initialization.

  4. @ComponentScan: this annotation used to scan base packages of the project classpath. and spring will auto detect all the components.

  5. @Component: annotated class can be auto detected by spring container. it turns class into spring bean.

  6. @Repository: annotated class indicates as a repository. which is an abstraction of data access and storage.

  7. @Configuration: indicates class is a configuration class. and that may contain bean definitions.

  8. @Controller: marks a class web controller. and that handles all incoming requests.

  9. @RequestMapping: maps http request with a path to the controller method

  10. @Autowired: marks a constructor, field and a setter method to be autowired by dependency injection.

  11. @SpringbootApplication: enables spring auto configuration and component scanning. this is used at main() method. it's a combination of @Configuration, @EnableAutoConfiguration, @ComponentScan.

  12. @CrossOrigin: this is used both at class and method level to enable cross origin requests. if ui application and backend application runs on different hosts and port then this annotation comes into picture.

  13. @InitBinder: this annotation used at method level. which is used to binds the request parameter to java bean object. and this method will be called for each http request.

  14. @RestController: This annotation is used at the class level. The @RestController annotation marks the class as a controller where every method returns a domain object instead of a view.

  15. @Lazy: This annotation used at class level and that should be component class. By default all autowired dependencies are created and configured at startup. But if you want to initialize a bean lazily, you can use @Lazy annotation over the class. This means that the bean will be created and initialized only when it is first requested for. You can also use this annotation on @Configuration classes.

Composed RequestMapping Annotations:

  1. @GetMapping: this annotation used for mapping HTTP GET requests onto specific handler methods.

  2. @PostMapping: this annotation used for mapping HTTP POST requests onto specific handler methods. for insert or update data.

  3. @PutMapping: this annotation used for mapping HTTP PUT requests onto specific handler methods to update data.

  4. @DeleteMapping: this annotation used for mapping HTTP DELETE requests onto specific handler methods to perform delete operations.

  5. @RequestBody: this annotation indicates that a method parameter should be bound to the value of the HTTP request body. and the httpmessageconverter is responsible for converting http request message to object.

  6. @RequestHeader: Annotation used to map controller parameter to request header value. and helps us to get the header details within controller class.

  7. @PathVariable: Annotation can be used to handle dynamic changes in the URI where certain URI value acts as a parameter. You can specify this parameter using a regular expression.

  8. @RequestParam: used to annotate request handler method arguments. Sometimes you get the parameters in the request URL, mostly in GET requests. In that case, along with the @RequestMapping annotation you can use the @RequestParam annotation to retrieve the URL parameter and map it to the method argument.

  9. @ResponseBody: Annotation indicates that the result type should be written straight in the response body in whatever format you specify like JSON or XML. Spring converts the returned object into a response body by using the HttpMessageConveter.

Task Execution and Scheduling annotations:

  1. @Scheduled: Annotation that marks a method to be scheduled. Exactly one of the cron, fixedDelay, or fixedRate attributes must be specified. and method has no return type. If not, the returned value will be ignored when called through the scheduler.

  2. @Async: Annotation that marks a method as a candidate for asynchronous execution. Can also be used at the type level, in which case all of the type's methods are considered as asynchronous. Note, however, that @Async is not supported on methods declared within a @Configuration class.