Annotations in Spring and Spring-Boot

Annotation is a core concept that works with many technologies. Annotations mean adding extra notes or remarks on something. It gives a detailed explanation of a particular part of something which is being annotated. It is used in many technologies like Java, C#, Python, Kotlin, Kubernetes, etc. Here we will discuss spring and spring-boot annotations.

Spring annotation and its types

Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect o the operation of the code they annotate. It does not change the action of the compiled program.

Mainly there are total 6 types of annotations:

  • Spring Core Annotations
  • Spring Web Annotations
  • Spring Boot Annotations
  • Spring Scheduling Annotations
  • Spring Data Annotations
  • Spring Bean annotations

Spring-Boot annotations

Spring annotations present in the org.springframework,boot.autoconfigure and org.springframrework.boot.autoconfigure.condition packages are commonly known as Spring Boot annotations.
Spring Boot’s annotations are key to getting up to speed with the framework. They allow you to direct the framework to do the bidding, taking control and overriding its default when needed. Annotations are quick, easy to use, and ad orders of magnitude faster than building the equivalent functionality yourself.

Basic Setup

In order to set up the initial state in the Spring Boot application, we use the @SpringBootapplication annotation. Which is a combination of @Configuration, @EnableAutoConfiguration, and @ComponentScan.

Request Response

This type of annotation helps in responding to any HTTP request easily. Using various types of mapping annotations we can control how functions behave and what they return. These annotations are:

  • @RequestMapping
    • @GetMapping
    • @PostMapping
    • @PutMapping
    • @DeleteMapping
    • @PatchMapping
  • @RequestParam

Component Type

This type of annotation let you label components in your application aside from @RestController. These are functionally identical but allow you to organize your application and mark classes for specific roles helping to keep your application modular. These annotations are:

@Component
@Service
@Repositroy
@Controller
@RestController (@Controller + @ResponseBody) – not a component type

Testing:

As it should SpringBoot makes it easy to write tests, identifying and fixing errors using its annotations. By testing, creating temporary services, and validating
@SpringBootTest
@MokeBean
@Validate

Misc

@Bean: Dependency Injection is central to Spring Boot, and the @Bean annotation allows to make tightly coupled classes. That tells spring containers to handle their lifecycle.
@ConditionalOnJava: This annotation helps your application to run on a particular version of java. There are several other conditional annotations that apply conditions on either class, methods, WebApp, property, and many more.

VIBHUTI

Vibhuti is pursuing her Bachelors in Computer Engineering from Marwadi Education Foundation Group of Institutions. She is working with GlobalVox from Jan 2022 .Vibhuti is fond of drawing & traveling.
Vibhuti Patel, Intern at GlobalVox. | Posted on: Aug 4, 2022