Catalog / Spring Boot Cheat Sheet
Spring Boot Cheat Sheet
A quick reference guide to Spring Boot, covering essential annotations, properties, and best practices for rapid application development.
Core Annotations
Application Setup
|
Example:
|
Component Management
|
Marks a class as a Spring-managed component. Generic stereotype for any Spring-managed component. |
|
Specialization of |
|
Specialization of |
|
Marks a class as a controller, handling incoming web requests. |
|
Combination of |
Dependency Injection
|
Injects dependencies into a class. Can be used on fields, constructors, or setter methods. Example:
|
|
Used with Example:
|
|
Injects values from properties files or environment variables. Example:
|
Web Development
Request Mapping
|
Maps web requests onto controller methods. Supports various attributes like Example:
|
|
Shortcut for Example:
|
|
Shortcut for Example:
|
|
Shortcut for |
|
Shortcut for |
|
Shortcut for |
Request Parameters
|
Extracts values from the URI path. Example:
|
|
Extracts values from query parameters. Example:
|
|
Binds the request body to a method parameter. Often used to receive JSON or XML data. Example:
|
Response Handling
|
Indicates that a method’s return value should be bound to the web response body. Typically used in |
|
Allows fine-grained control over the response, including status code, headers, and body. Example:
|
|
Sets the HTTP status code of the response. Example:
|
Data Management
JPA Repositories
|
|
|
|
Example:
|
Data Source Configuration
Spring Boot automatically configures a data source if you provide the necessary properties in |
Example properties:
|
Transactions
|
Marks a method or class as transactional. Ensures that database operations are executed within a transaction. Supports various attributes like Example:
|
Advanced Features
Profiles
Spring Profiles allow you to configure different parts of your application for different environments (e.g., development, testing, production). |
Example:
|
Activate profiles using the |
Actuator
Spring Boot Actuator provides endpoints for monitoring and managing your application, such as health checks, metrics, and audit events. |
Common endpoints include |
Enable Actuator by adding the |
Testing
|
Loads the full application context for integration tests. |
|
Tests only the web layer, loading only the necessary components (e.g., controllers, filters, interceptors). |
|
Tests only the JPA components, setting up an in-memory database. |
|
Mocks a bean in the application context, allowing you to isolate the component being tested. |