0% found this document useful (0 votes)
2 views12 pages

Spring and Spring Boot Annotations Cheat Sheet

The document is a comprehensive cheat sheet for commonly used annotations in Spring and Spring Boot, detailing their functionalities and usage. It includes annotations for various purposes such as component scanning, request mapping, dependency injection, and exception handling, along with example code snippets. This resource is aimed at helping developers quickly reference and understand the key annotations in the Spring framework for building Java applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Spring and Spring Boot Annotations Cheat Sheet

The document is a comprehensive cheat sheet for commonly used annotations in Spring and Spring Boot, detailing their functionalities and usage. It includes annotations for various purposes such as component scanning, request mapping, dependency injection, and exception handling, along with example code snippets. This resource is aimed at helping developers quickly reference and understand the key annotations in the Spring framework for building Java applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Ramesh Fadatare Java Libraries English My Blogs YouTube My Courses About Me

Java Java API Spring Boot Python Python API SQL Kotlin C C++ C# Go R HTML JS CSS

TypeScript React JS MySQL

← Previous Post Next Post →


Search

Spring and Spring Boot Annotations Cheat


Useful Cheat Sheet
Sheets 1 Comment / By Ramesh Fadatare / July 17, 2024

Spring and Spring Boot Introduction


Annotations Cheat Sheet
This guide will provide a comprehensive cheat sheet for the most commonly used annotations in
JPA and Hibernate Annotations
Spring and Spring Boot.
Cheat Sheet

Java 8 Cheat Sheet


What is Spring Framework?
JUnit 5 Annotations Cheat Sheet
The Spring Framework is a comprehensive framework for building Java applications. It provides a
Mockito Annotations Cheat Sheet
wide range of functionalities, including dependency injection, transaction management, and aspect-
Java String Methods Cheat Sheet oriented programming. Spring helps developers create high-performance, reusable, and easily
Java Keywords Cheat Sheet testable code by managing the lifecycle and interactions of Java objects.

Java Stream API Cheat Sheet


What is Spring Boot?
TestNG Annotations Cheat Sheet

Apache Kafka Cheat Sheet Spring Boot is developed on top of Spring Framework to simplify the configuration and deployment
process. It provides a set of defaults and auto-configuration options that allow developers to get
RabbitMQ Cheat Sheet
started quickly with minimal configuration. Spring Boot also includes an embedded web server,
Maven Commands Cheat Sheet making it easy to create standalone applications.

Linux Commands Cheat Sheet


Spring and Spring Boot Annotations Cheat Sheet
Gradle Commands Cheat Sheet

Docker Commands Cheat Sheet Here’s a handy cheat sheet of the most commonly used Spring and Spring Boot annotations:

IntelliJ IDEA Cheat Sheet Keyboard


Shortcuts Annotation Description

Git Commands Cheat Sheet


Marks the main class of a Spring Boot application. Combines
Cheat Sheet for Java Object- @SpringBootApplication @Configuration, @EnableAutoConfiguration, and @ComponentScan.
Oriented Programming (OOP) This is where your application starts.

HTML Tags Cheat Sheet


@Component Marks a class as a Spring component, making it a managed bean.
Eclipse Cheat Sheet Keyboard
Shortcuts Special type of @Component for service layer classes, typically
@Service
containing business logic.
Cheat Sheet for Unix Commands

Cheat Sheet for MySQL Database Special type of @Component for data repository classes. Enables
@Repository
Commands exception translation.

Cheat Sheet for PostgreSQL Special type of @Component for Spring MVC controllers that handle
Commands @Controller
web requests.
Cheat Sheet for MongoDB
Commands Combines @Controller and @ResponseBody. Handles web requests
@RestController
and returns JSON/XML responses directly.
SQL Commands Cheat Sheet
Automatically injects dependencies into a field, setter method, or
Oracle Database Commands Cheat @Autowired
Sheet constructor.

Microsoft SQL Server Commands Injects values from properties files into fields, setter methods, or
@Value
Cheat Sheet constructor parameters.
Cheat Sheet for GoF Design
Annotation Description
Patterns

HTTP Status Codes Cheat Sheet @Configuration Indicates that a class contains Spring bean definitions.

Cheat Sheet for HTTP Methods Declares a method that returns a Spring bean to be managed by the
@Bean
Apache Cassandra Cheat Sheet Spring container.

Redis Commands Cheat Sheet Automatically configures your Spring application based on the
@EnableAutoConfiguration
NetBeans Cheat Sheet dependencies present in the classpath.

Visual Studio Code (VSCode) Cheat Configures component scanning directives for use with
Sheet for Windows @ComponentScan @Configuration classes. Specifies base packages to scan for
Sublime Text Cheat Sheet for annotated components.
Windows
@RequestMapping Maps web requests to specific handler classes or handler methods.
Visual Studio Code (VSCode) Cheat
Sheet for macOS Shortcut for @RequestMapping(method = RequestMethod.GET).
@GetMapping
Handles GET requests.

Shortcut for @RequestMapping(method = RequestMethod.POST).


@PostMapping
Handles POST requests.

Categories Shortcut for @RequestMapping(method = RequestMethod.PUT).


@PutMapping
Handles PUT requests.
ArrayDeque
Shortcut for @RequestMapping(method = RequestMethod.DELETE).
ArrayList @DeleteMapping
Handles DELETE requests.
Arrays
Shortcut for @RequestMapping(method = RequestMethod.PATCH).
C Functions @PatchMapping
Handles PATCH requests.
C Programming
@RequestParam Binds a web request parameter to a method parameter.
C# Programming

C++ Programming @PathVariable Binds a URI template variable to a method parameter.

Certifications @RequestBody Binds the body of a web request to a method parameter.


Cheat Sheet
Indicates that the return value of a method should be used as the
Class @ResponseBody
response body.
Collections
@CrossOrigin Enables Cross-Origin Resource Sharing (CORS) on a method or class.
CompletableFuture
Defines a method to handle exceptions thrown by request handler
ConcurrentHashMap @ExceptionHandler
methods.
CopyOnWriteArrayList
Allows you to handle exceptions across the whole application, not
CopyOnWriteArraySet @ControllerAdvice
just to an individual controller.
CSS Tutorial
Combines @ControllerAdvice and @ResponseBody. Applies to REST
Currency @RestControllerAdvice
controllers.
English Grammar
@RequestScope Indicates that a bean is request-scoped.
Enum

EnumMap @SessionScope Indicates that a bean is session-scoped.

EnumSet @ApplicationScope Indicates that a bean is application-scoped.

Executors
@SessionAttributes Used to store model attributes in the session.
Functional interfaces
Binds a method parameter or method return value to a named model
Go bytes @ModelAttribute
attribute.
Go filepath
@Async Indicates that a method should be executed asynchronously.
Go fmt

Go http Marks a method to be scheduled with a cron expression or fixed


@Scheduled
delay/rate.
Go json

Go log @EnableScheduling Enables support for scheduled tasks.

Go math @Conditional Registers a bean based on a condition.

Go os
@Profile Registers a bean only if a specified profile is active.
Go Programming

Go reflect
Go regexp
Annotation Description
Go slices
Creates a mock bean in a Spring application context (used with
Go sort @MockBean
Spring Boot for testing).
Go strconv
Creates a spy bean in a Spring application context (used with Spring
Go strings @SpyBean
Boot for testing).
Go time

Go unicode Explanation with Code Snippets


Go url
@SpringBootApplication
HashMap

HashSet Marks the main class of a Spring Boot application. Combines @Configuration,
@EnableAutoConfiguration, and @ComponentScan.
HTML Tutorial

IdentityHashMap
@SpringBootApplication
Iterator
public class MySpringBootApplication {
Java public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
Java Date and Time
}
Java Date Time }
Java Interview

Java Keywords
@Component
Java Lang
Marks a class as a Spring component, making it a managed bean.
Java Math

Java Methods
@Component
Java Programming
public class MyComponent {
Java Stream // Bean methods and properties
}
Java Util

JavaScript Tutorial

JUnit Tutorial
@Service

Kotlin Special type of @Component for service layer classes, typically containing business logic.

Kotlin Functions

Kotlin Types @Service


LinkedHashMap public class MyService {
// Business logic methods
LinkedHashSet
}
LinkedList

Milestones
@Repository
Mockito Tutorial
Special type of @Component for data repository classes. Enables exception translation.
Module

MySQL Tutorial
@Repository
Number
public class MyRepository {
Object // Data access methods
Objects
}

Package

PriorityQueue @Controller
Python Array Special type of @Component for Spring MVC controllers that handle web requests.
Python Built-in Functions

Python cmath @Controller


Python Collections public class MyController {
@RequestMapping("/hello")
Python Datetime
public String sayHello() {
Python functools return "hello";
}
Python itertools
}
Python JSON
Python Math @RestController
Python Modules
Combines @Controller and @ResponseBody. Handles web requests and returns JSON/XML responses
Python NumPy directly.
Python operator

Python OS @RestController
Python Programming public class MyRestController {
@GetMapping("/greet")
Python Random
public String greet() {
Python RE return "Hello, World!";
}
Python requests
}
Python statistics

Python String
@Autowired
Python SYS

Python threading Automatically injects dependencies into a field, setter method, or constructor.

R Programming

React Tutorial @Component


public class MyComponent {
Scanner
@Autowired
Spring AI private MyService myService;

Spring Boot
// Use myService in methods
Spring Boot Tutorial }
Spring Framework

SQL Tutorial @Value


String
Injects values from properties files into fields, setter methods, or constructor parameters.
StringBuilder

System
@Component
Thread public class MyComponent {
ThreadGroup @Value("${app.name}")
private String appName;
ThreadLocal

ThreadPoolExecutor // Use appName in methods


Throwable
}

TreeMap

TypeScript Tutorial @Configuration


Wrapper Class Indicates that a class contains Spring bean definitions.

@Configuration
public class MyConfig {
Latest Posts @Bean
public MyService myService() {
Difference between HashMap and return new MyService();
LinkedHashMap in Java }
}
Difference between ArrayList and
HashSet in Java

HashSet vs LinkedHashSet vs @Bean


TreeSet in Java
Declares a method that returns a Spring bean to be managed by the Spring container.
Difference between HashSet and
TreeSet in Java
@Configuration
Difference Between ArrayList and
public class MyConfig {
LinkedList
@Bean
public MyService myService() {
return new MyService();
}
}
@EnableAutoConfiguration

Automatically configures your Spring application based on the dependencies present in the classpath.

@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}

@ComponentScan

Configures component scanning directives for use with @Configuration classes. Specifies base
packages to scan for annotated components.

@Configuration
@ComponentScan(basePackages = "com.example")
public class MyConfig {
// Configuration methods
}

@RequestMapping

Maps web requests to specific handler classes or handler methods.

@Controller
public class MyController {
@RequestMapping("/hello")
public String sayHello() {
return "hello";
}
}

@GetMapping

Shortcut for @RequestMapping(method = RequestMethod.GET). Handles GET requests.

@RestController
public class MyRestController {
@GetMapping("/greet")
public String greet() {
return "Hello, World!";
}
}

@PostMapping

Shortcut for @RequestMapping(method = RequestMethod.POST). Handles POST requests.

@RestController
public class MyRestController {
@PostMapping("/create")
public String create(@RequestBody MyEntity entity) {
// Process entity
return "Created";
}
}

@PutMapping
Shortcut for @RequestMapping(method = RequestMethod.PUT). Handles PUT requests.

@RestController
public class MyRestController {
@PutMapping("/update")
public String update(@RequestBody MyEntity entity) {
// Update entity
return "Updated";
}
}

@DeleteMapping

Shortcut for @RequestMapping(method = RequestMethod.DELETE). Handles DELETE requests.

@RestController
public class MyRestController {
@DeleteMapping("/delete/{id}")
public String delete(@PathVariable Long id) {
// Delete entity by id
return "Deleted";
}
}

@PatchMapping

Shortcut for @RequestMapping(method = RequestMethod.PATCH). Handles PATCH requests.

@RestController
public class MyRestController {

@PatchMapping("/patch")
public String patch(@RequestBody MyEntity entity) {
// Patch entity
return "Patched";
}
}

@RequestParam

Binds a web request parameter to a method parameter.

@RestController
public class MyRestController {
@GetMapping("/param")
public String param(@RequestParam String name) {
return "Hello, " + name;
}
}

@PathVariable

Binds a URI template variable to a method parameter.

@RestController
public class MyRestController {
@GetMapping("/path/{id}")
public String path(@PathVariable Long id) {
return "ID: " + id;
}
}

@RequestBody

Binds the body of a web request to a method parameter.

@RestController
public class MyRestController {
@PostMapping("/body")
public String body(@RequestBody MyEntity entity) {
return "Received: " + entity;
}
}

@ResponseBody

Indicates that the return value of a method should be used as the response body.

@Controller
public class MyController {
@RequestMapping("/json")
@ResponseBody
public String json() {
return "{\"message\":\"Hello, World!\"}";
}
}

@CrossOrigin

Enables Cross-Origin Resource Sharing (CORS) on a method or class.

@RestController
@CrossOrigin(origins = "http://example.com")
public class MyRestController {
@GetMapping("/cors")
public String cors() {
return "CORS Enabled";
}
}

@ExceptionHandler

Defines a method to handle exceptions thrown by request handler methods.

@Controller
public class MyController {
@ExceptionHandler(Exception.class)
public String handleException() {
return "error";
}
}

@ControllerAdvice

Allows you to handle exceptions across the whole application, not just to an individual controller.

@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public String handleException() {
return "error";
}
}

@RestControllerAdvice

Combines @ControllerAdvice and @ResponseBody. Applies to REST controllers.

@RestControllerAdvice
public class GlobalRestExceptionHandler {
@ExceptionHandler(Exception.class)
public String handleException() {
return "{\"error\":\"An error occurred\"}";
}
}

@RequestScope

Indicates that a bean is request-scoped.

@Component
@RequestScope
public class MyRequestScopedBean {
// Bean methods and properties
}

@SessionScope

Indicates that a bean is session-scoped.

@Component
@SessionScope
public class MySessionScopedBean {
// Bean methods and properties
}

@ApplicationScope

Indicates that a bean is application-scoped.

@Component
@ApplicationScope
public class MyApplicationScopedBean {
// Bean methods and properties
}

@SessionAttributes

Used to store model attributes in the session.

@Controller
@SessionAttributes("user")
public class MyController {
@ModelAttribute("user")
public User createUser() {
return new User();
}
}

@ModelAttribute
Binds a method parameter or method return value to a named model attribute.

@Controller
public class MyController {
@ModelAttribute("attribute")
public String addAttribute() {
return "value";
}
}

@Async

Indicates that a method should be executed asynchronously.

@Service
public class MyService {
@Async
public void asyncMethod() {
// Async method logic
}
}

@Scheduled

Marks a method to be scheduled with a cron expression or fixed delay/rate.

@Component
public class MyScheduledTask {
@Scheduled(cron = "0 0 * * * ?")
public void scheduledMethod() {
// Scheduled task logic
}
}

@EnableScheduling

Enables support for scheduled tasks.

@Configuration
@EnableScheduling
public class MyConfig {
// Configuration methods
}

@Conditional

Registers a bean based on a condition.

@Configuration
public class MyConfig {
@Bean
@Conditional(MyCondition.class)
public MyService myService() {
return new MyService();
}
}

@Profile

Registers a bean only if a specified profile is active.


@Configuration
@Profile("dev")
public class DevConfig {
@Bean
public MyService myService() {
return new MyService();
}
}

@MockBean

Creates a mock bean in a Spring application context (used with Spring Boot for testing).

@SpringBootTest
public class MyTest {
@MockBean
private MyService myService;

@Test
public void test() {
// Test logic
}
}

@SpyBean

Creates a spy bean in a Spring application context (used with Spring Boot for testing).

@SpringBootTest
public class MyTest {
@SpyBean
private MyService myService;

@Test
public void test() {
// Test logic
}
}

Download Cheat Sheet

Conclusion
Mastering Spring and Spring Boot annotations is essential for writing effective and efficient Java
applications. This cheat sheet provides a quick reference to the most commonly used annotations,
helping you streamline your development process and ensure your applications are well-structured
and maintainable.

By understanding and using these annotations effectively, you can simplify your configuration,
enhance your application’s functionality, and write cleaner code. Keep this guide handy to enhance
your productivity and make the most of Spring and Spring Boot. Happy coding!

← Previous Post Next Post →

Related Posts:

JPA and Hibernate Maven Commands Linux Commands


Annotations Cheat Cheat Sheet Cheat Sheet
Sheet Leave a Comment / Cheat Sheet / Leave a Comment / Cheat Sheet /
By Ramesh Fadatare By Ramesh Fadatare
Leave a Comment / Cheat Sheet /
By Ramesh Fadatare

Gradle Commands Docker Commands JUnit 5 Annotations


Cheat Sheet Cheat Sheet Cheat Sheet
Leave a Comment / Cheat Sheet / Leave a Comment / Cheat Sheet / Leave a Comment / Cheat Sheet /
By Ramesh Fadatare By Ramesh Fadatare By Ramesh Fadatare

Mockito Annotations IntelliJ IDEA Cheat Git Commands Cheat


Cheat Sheet Sheet Keyboard Sheet
Leave a Comment / Cheat Sheet / Shortcuts Leave a Comment / Cheat Sheet /
By Ramesh Fadatare By Ramesh Fadatare
Leave a Comment / Cheat Sheet /
By Ramesh Fadatare

1 thought on “Spring and Spring Boot Annotations Cheat Sheet”

ANIL
AUGUST 13, 2024 AT 1:10 PM

Very useful

Reply

Leave a Comment
Your email address will not be published. Required fields are marked *
Save my name, email, and website in this browser for the next time I comment.

Post Comment

Copyright © 2025 Ramesh Fadatare

You might also like