Spring Boot Annotations Cheat Sheet
In Spring Boot, annotations are a huge part of how you configure and wire your application without tons of XML. Here are some of the most commonly used ones, grouped by purpose so theyβre easier to remember:
π Core / Bootstrapping
-
@SpringBootApplicationThe main annotation for a Spring Boot app. It combines:@Configuration@EnableAutoConfiguration@ComponentScanYou usually put this on your main class.
-
@ConfigurationMarks a class as a source of bean definitions. -
@BeanDeclares a method that returns an object managed by Spring (a βbeanβ).
π Component Scanning & Dependency Injection
-
@ComponentGeneric stereotype for any Spring-managed component. -
@ServiceSpecialized version of@Componentfor service-layer classes. -
@RepositoryUsed for DAO (data access) classes; also enables exception translation. -
@ControllerMarks a class as a web controller (MVC). -
@RestControllerCombines@Controller+@ResponseBodyfor REST APIs. -
@AutowiredInjects dependencies automatically. -
@QualifierUsed with@Autowiredwhen multiple beans exist.
π Web / REST Annotations
-
@RequestMappingMaps HTTP requests to handler methods. -
@GetMapping,@PostMapping,@PutMapping,@DeleteMappingShortcut annotations for specific HTTP methods. -
@PathVariableExtracts values from the URL path. -
@RequestParamGets query parameters. -
@RequestBodyBinds HTTP request body to a Java object. -
@ResponseBodySends return value directly as HTTP response (usually JSON).
ποΈ Data / JPA Annotations
(Used with Spring Data JPA and ORM tools)
-
@EntityMarks a class as a database entity. -
@IdSpecifies the primary key. -
@GeneratedValueDefines how the primary key is generated. -
@TableSpecifies the table name. -
@ColumnMaps a field to a column.
βοΈ Configuration & Properties
-
@ValueInjects values from properties files. -
@ConfigurationPropertiesBinds external config (likeapplication.yml) to a Java class.
π Validation
-
@ValidTriggers validation on request bodies. -
Common Java validation annotations:
@NotNull@Size@Email
π Transactions
@TransactionalManages database transactions automatically.
π§ͺ Testing
-
@SpringBootTestLoads the full application context for tests. -
@MockBeanAdds mocks into the Spring context.