Java & Spring
Updated for 2026

Spring Boot Core Annotations Cheatsheet 2026

Comprehensive guide to core Spring Framework and Spring Boot annotations including Stereotypes, REST Controllers, DI, Data/JPA, and Configuration.

Target Version Compatibility

Interactive Skill Mastery

Mark commands as learned to build your customized reference tracker. Retained locally in this browser.

Level:Novice
Command Mastery Progress0 of 22 Mastered (0%)

Stereotypes

@SpringBootApplication
BeginnerBasics
Indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning.

When to Use

When building Spring Boot applications to define component beans, configure mapping controllers, or structure database JPA repositories.

Common Mistakes

Placing annotations on classes outside Spring's package component-scan search scope, causing BeanCreationExceptions.

Shortcut / Pro-Tip

Use specialized annotations like @RestController and @GetMapping to keep controller code clean and concise.

Example

@SpringBootApplication
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@Component
BeginnerBasics
Marks a Java class as a bean/candidate for auto-detection and dependency injection.

When to Use

When building Spring Boot applications to define component beans, configure mapping controllers, or structure database JPA repositories.

Common Mistakes

Placing annotations on classes outside Spring's package component-scan search scope, causing BeanCreationExceptions.

Shortcut / Pro-Tip

Use specialized annotations like @RestController and @GetMapping to keep controller code clean and concise.

Example

@Component
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@Service
BeginnerBasics
Specialization of @Component for classes holding business services logic.

When to Use

When building Spring Boot applications to define component beans, configure mapping controllers, or structure database JPA repositories.

Common Mistakes

Placing annotations on classes outside Spring's package component-scan search scope, causing BeanCreationExceptions.

Shortcut / Pro-Tip

Use specialized annotations like @RestController and @GetMapping to keep controller code clean and concise.

Example

@Service
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@Repository
BeginnerBasics
Specialization of @Component for classes managing database access and encapsulating storage/DAO.

When to Use

When building Spring Boot applications to define component beans, configure mapping controllers, or structure database JPA repositories.

Common Mistakes

Placing annotations on classes outside Spring's package component-scan search scope, causing BeanCreationExceptions.

Shortcut / Pro-Tip

Use specialized annotations like @RestController and @GetMapping to keep controller code clean and concise.

Example

@Repository
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context

Spring MVC & REST

@RestController
BeginnerBasics
Combines @Controller and @ResponseBody, marking a class as a RESTful web controller.

When to Use

When exposing RESTful API endpoints and binding web query paths to specific Java controller methods.

Common Mistakes

Forgetting to annotate methods with @ResponseBody (or failing to use @RestController), returning raw strings instead of serialized JSON.

Shortcut / Pro-Tip

Leverage @PathVariable for REST paths and @RequestParam for clean query key-value parameter bindings.

Example

@RestController
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@GetMapping("/path")
BeginnerBasics
Shortcut annotation for mapping HTTP GET requests onto specific handler methods.

When to Use

When exposing RESTful API endpoints and binding web query paths to specific Java controller methods.

Common Mistakes

Forgetting to annotate methods with @ResponseBody (or failing to use @RestController), returning raw strings instead of serialized JSON.

Shortcut / Pro-Tip

Leverage @PathVariable for REST paths and @RequestParam for clean query key-value parameter bindings.

Example

@GetMapping("/path")
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@PostMapping("/path")
BeginnerBasics
Shortcut annotation for mapping HTTP POST requests.

When to Use

When exposing RESTful API endpoints and binding web query paths to specific Java controller methods.

Common Mistakes

Forgetting to annotate methods with @ResponseBody (or failing to use @RestController), returning raw strings instead of serialized JSON.

Shortcut / Pro-Tip

Leverage @PathVariable for REST paths and @RequestParam for clean query key-value parameter bindings.

Example

@PostMapping("/path")
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@PathVariable
BeginnerBasics
Extracts template variables from the URI path (e.g., /users/{id}).

When to Use

When building Spring Boot applications to define component beans, configure mapping controllers, or structure database JPA repositories.

Common Mistakes

Placing annotations on classes outside Spring's package component-scan search scope, causing BeanCreationExceptions.

Shortcut / Pro-Tip

Use specialized annotations like @RestController and @GetMapping to keep controller code clean and concise.

Example

@PathVariable
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@RequestParam
BeginnerBasics
Binds web query parameters to method arguments (e.g., ?page=1).

When to Use

When building Spring Boot applications to define component beans, configure mapping controllers, or structure database JPA repositories.

Common Mistakes

Placing annotations on classes outside Spring's package component-scan search scope, causing BeanCreationExceptions.

Shortcut / Pro-Tip

Use specialized annotations like @RestController and @GetMapping to keep controller code clean and concise.

Example

@RequestParam
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@RequestBody
BeginnerBasics
Binds the incoming HTTP request body raw payload to a Java object deserialized by Jackson.

When to Use

When building Spring Boot applications to define component beans, configure mapping controllers, or structure database JPA repositories.

Common Mistakes

Placing annotations on classes outside Spring's package component-scan search scope, causing BeanCreationExceptions.

Shortcut / Pro-Tip

Use specialized annotations like @RestController and @GetMapping to keep controller code clean and concise.

Example

@RequestBody
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context

DI & Configuration

@Autowired
BeginnerBasics
Marks a constructor, field, or setter method to be dependency-injected automatically by Spring's container.

When to Use

When implementing Dependency Injection (DI) to automatically wire collaborating components and manage object lifecycles.

Common Mistakes

Creating circular dependencies between Spring beans, which crashes the application context at boot time.

Shortcut / Pro-Tip

Prefer constructor-based dependency injection over field injection for clean unit testing and immutability.

Example

@Autowired
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@Qualifier("beanName")
BeginnerBasics
Disambiguates which bean should be injected when multiple candidates of the same type exist.

When to Use

When building Spring Boot applications to define component beans, configure mapping controllers, or structure database JPA repositories.

Common Mistakes

Placing annotations on classes outside Spring's package component-scan search scope, causing BeanCreationExceptions.

Shortcut / Pro-Tip

Use specialized annotations like @RestController and @GetMapping to keep controller code clean and concise.

Example

@Qualifier("beanName")
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@Configuration
BeginnerBasics
Declares that a class contains one or more bean definition methods annotated with @Bean.

When to Use

When implementing Dependency Injection (DI) to automatically wire collaborating components and manage object lifecycles.

Common Mistakes

Creating circular dependencies between Spring beans, which crashes the application context at boot time.

Shortcut / Pro-Tip

Prefer constructor-based dependency injection over field injection for clean unit testing and immutability.

Example

@Configuration
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@Bean
BeginnerBasics
Declares a class instance returned from this method is managed as a singleton component by the Spring container.

When to Use

When implementing Dependency Injection (DI) to automatically wire collaborating components and manage object lifecycles.

Common Mistakes

Creating circular dependencies between Spring beans, which crashes the application context at boot time.

Shortcut / Pro-Tip

Prefer constructor-based dependency injection over field injection for clean unit testing and immutability.

Example

@Bean
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@Value("${property.name}")
BeginnerBasics
Injects specific application property values from application.properties or application.yml configuration files.

When to Use

When building Spring Boot applications to define component beans, configure mapping controllers, or structure database JPA repositories.

Common Mistakes

Placing annotations on classes outside Spring's package component-scan search scope, causing BeanCreationExceptions.

Shortcut / Pro-Tip

Use specialized annotations like @RestController and @GetMapping to keep controller code clean and concise.

Example

@Value("${property.name}")
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context

Spring Data JPA

@Entity
BeginnerBasics
Specifies that the annotated class is a JPA entity mapped to a database table.

When to Use

When building Spring Boot applications to define component beans, configure mapping controllers, or structure database JPA repositories.

Common Mistakes

Placing annotations on classes outside Spring's package component-scan search scope, causing BeanCreationExceptions.

Shortcut / Pro-Tip

Use specialized annotations like @RestController and @GetMapping to keep controller code clean and concise.

Example

@Entity
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@Table(name = "users")
BeginnerBasics
Specifies the primary database table name for the annotated JPA entity.

When to Use

When building Spring Boot applications to define component beans, configure mapping controllers, or structure database JPA repositories.

Common Mistakes

Placing annotations on classes outside Spring's package component-scan search scope, causing BeanCreationExceptions.

Shortcut / Pro-Tip

Use specialized annotations like @RestController and @GetMapping to keep controller code clean and concise.

Example

@Table(name = "users")
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@Id
BeginnerBasics
Specifies the primary key of an entity.

When to Use

When building Spring Boot applications to define component beans, configure mapping controllers, or structure database JPA repositories.

Common Mistakes

Placing annotations on classes outside Spring's package component-scan search scope, causing BeanCreationExceptions.

Shortcut / Pro-Tip

Use specialized annotations like @RestController and @GetMapping to keep controller code clean and concise.

Example

@Id
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@GeneratedValue(strategy = GenerationType.IDENTITY)
BeginnerBasics
Defines primary key generation strategy (e.g., auto-increment).

When to Use

When building Spring Boot applications to define component beans, configure mapping controllers, or structure database JPA repositories.

Common Mistakes

Placing annotations on classes outside Spring's package component-scan search scope, causing BeanCreationExceptions.

Shortcut / Pro-Tip

Use specialized annotations like @RestController and @GetMapping to keep controller code clean and concise.

Example

@GeneratedValue(strategy = GenerationType.IDENTITY)
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@OneToMany / @ManyToOne
BeginnerBasics
Declares structural entity relationships (one-to-many or many-to-one mapping rules).

When to Use

When building Spring Boot applications to define component beans, configure mapping controllers, or structure database JPA repositories.

Common Mistakes

Placing annotations on classes outside Spring's package component-scan search scope, causing BeanCreationExceptions.

Shortcut / Pro-Tip

Use specialized annotations like @RestController and @GetMapping to keep controller code clean and concise.

Example

@OneToMany / @ManyToOne
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context

Testing

@SpringBootTest
IntermediateDebugging
Triggers the full Spring application context loading for comprehensive integration tests.

When to Use

When building Spring Boot applications to define component beans, configure mapping controllers, or structure database JPA repositories.

Common Mistakes

Placing annotations on classes outside Spring's package component-scan search scope, causing BeanCreationExceptions.

Shortcut / Pro-Tip

Use specialized annotations like @RestController and @GetMapping to keep controller code clean and concise.

Example

@SpringBootTest
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context
@MockBean
IntermediateDebugging
Creates a Mockito mock instance and registers it inside the Spring application context to substitute real beans.

When to Use

When implementing Dependency Injection (DI) to automatically wire collaborating components and manage object lifecycles.

Common Mistakes

Creating circular dependencies between Spring beans, which crashes the application context at boot time.

Shortcut / Pro-Tip

Prefer constructor-based dependency injection over field injection for clean unit testing and immutability.

Example

@MockBean
public class UserResource {
  // Controller metadata
}

Output Example

Console / Terminal
// Registers structural Java configuration or bean in Spring's Application Context

Spring Boot Annotations Best Practices

1Prefer Constructor Injection

Avoid field injection with @Autowired. Use constructor injection instead to make classes immutable, clear in dependencies, and easier to unit test without Spring runner.

2Keep Stereotypes Semantic

Use @Service for business logic and @Repository for data access rather than general @Component to benefit from specialized Spring middleware features.

3Define Custom Config Properties

Use @ConfigurationProperties instead of scattering multiple @Value annotations. It provides type-safety, validation, and structures configuration properties.

4Leverage Profile-Specific Beans

Use @Profile to configure environment-specific beans (e.g. @Profile("prod") vs @Profile("dev")), keeping environments clean and isolated.

5Minimize Heavy Auto-Configurations

Exclude unneeded auto-configurations to optimize application boot times and reduce memory overhead.

Common Spring Boot Annotations Errors & Solutions

Error

NoSuchBeanDefinitionException

Solution

Spring is unable to find a bean matching the injection target. Solution: Ensure the target class has a stereotype annotation and resides inside a scanned package.

Error

CircularDependencyException

Solution

Two or more beans depend on each other, creating an infinite loading loop. Solution: Restructure dependencies or resolve them lazily using @Lazy.

Error

BeanCreationException

Solution

An error occurred while Spring was constructing a bean at startup. Solution: Inspect the root cause stack trace to find configuration, DB connection, or property typos.

Error

HttpMediaTypeNotSupportedException

Solution

The incoming request media type is invalid. Solution: Ensure the client sends 'Content-Type: application/json' and the controller declares appropriate consumes parameters.

Error

PropertyAccessException

Solution

A configuration key is missing or cannot be cast to the specified Java field type. Solution: Declare default property values or check application.properties structures.

Common Spring Boot Annotations Interview Questions

Q1What is @SpringBootApplication and what does it combine?

It is a convenience annotation that combines @Configuration (enables Java-based config), @EnableAutoConfiguration (enables Spring Boot auto-config), and @ComponentScan (scans for beans).

Q2How do @Component, @Service, and @Repository differ?

They are all stereotype annotations. @Component is the general bean candidate. @Service specializes it for business services, and @Repository specializes it for database DAOs, including translation of database exceptions.

Q3What is the purpose of @Autowired?

It enables automatic dependency injection. Spring resolves and injects collaborating beans into constructors, setter methods, or fields automatically.

Q4What is the difference between @Controller and @RestController?

@Controller is used to define web controllers that render traditional views (like Thymeleaf templates). @RestController combines @Controller and @ResponseBody, returning serialized JSON/XML data directly.

Q5How does @Bean differ from @Component?

@Component is class-level and automatically scanned by Spring. @Bean is method-level inside a @Configuration class, giving you explicit control over bean instantiation.