Spring Boot Annotations – Complete List with Real World Usage

Spring Boot is powered by annotations. These small markers in your code tell the framework how to behave, how to create beans, how to map REST endpoints, how to configure security, transactions, profiles, and more.

But not all annotations are equal — some only affect compile-time visibility, others change runtime behavior, trigger proxies, or modify bean lifecycles.

In this guide, we’ll explore the most essential Spring Boot annotations, when and why to use them, how they affect your application, and how they matter in production systems.

This article is a definitive reference you can bookmark and return to throughout your Spring Boot journey.

Core Application Annotations

@SpringBootApplication

This annotation:

✔ Enables component scanning
✔ Enables auto-configuration
✔ Marks the entry point of your Spring Boot app

It combines:

  • @Configuration
  • @EnableAutoConfiguration
  • @ComponentScan

Usage:

@SpringBootApplication
public class App { … }

Production note: Place this annotation in the root package to ensure Spring scans all sub-packages.

Component & Stereotype Annotations

@Component

Marks a class as a Spring bean.
Used for generic components.

Example:

@Component
public class EmailValidator { … }

Use when there’s no clear layer (service or repository).

@Service

A specialized @Component for business logic.

Example:

@Service
public class OrderService { … }

Why use it?
Improves stack traces and aids AOP tools.

@Repository

Marks a persistence layer component.

Additional behavior:
✔ Exception translation (e.g., SQL exceptions → Spring DataAccessException)

Example:

@Repository
public class UserDao { … }

@Controller

Marks a Spring MVC controller for web requests.

Often paired with:
@RequestMapping, @GetMapping, @PostMapping

Example:

@Controller
public class HomeController { … }

@RestController

A combination of @Controller + @ResponseBody.

Use this for REST APIs, not HTML views.

Example:

@RestController
public class UserController { … }

Dependency Injection Annotations

@Autowired

Injects dependencies automatically.

Example:

@Autowired
private PaymentService paymentService;

Production-grade caution:
Avoid field injection — prefer constructor injection:

public OrderService(PaymentService paymentService) { … }

This makes beans immutable and testable.

@Qualifier

When multiple beans of the same type exist:

@Autowired
@Qualifier("stripeService")
private PaymentService paymentService;

This resolves ambiguity.

Configuration & Bean Creation

@Configuration

Marks a class that declares bean definitions.

Example:

@Configuration
public class AppConfig { … }

Classes with @Configuration are proxied to enforce singleton semantics for beans.

@Bean

Method-level annotation inside @Configuration classes.

Example:

@Bean
public RestTemplate restTemplate() { … }

Spring uses the method return value as a managed bean.

When to use:
Third-party libs or clients where classes cannot be annotated.

@Value

Injects values from properties.

Example:

@Value("${app.name}")
private String appName;

Use for simple values only — avoid for complex configuration trees.

@ConfigurationProperties

Binds configuration properties to POJOs.

Example:

@ConfigurationProperties("app")
public class AppProperties { … }

Preferred for: large, hierarchical config.

Profile & Environment Annotations

@Profile

Load beans only under certain environments.

Example:

@Profile("prod")
@Service
public class ProdEmailService { … }

Production use:
Dev/test vs prod bean specialization without code branching.

@ActiveProfiles

Used in tests to activate specific profiles.

Web Layer & REST Annotations

@RequestMapping

Maps HTTP requests to controllers.

Can specify path, methods, headers, etc.

Example:

@RequestMapping("/api/users")

@GetMapping, @PostMapping, @PutMapping, @DeleteMapping

Shorthand for @RequestMapping with HTTP verbs.

Example:

@GetMapping("/{id}")
public UserDto get(@PathVariable Long id) { … }

@RequestBody

Binds HTTP request body to a Java object.

Example:

@PostMapping
public ResponseEntity create(@RequestBody UserDto user) { … }

Validation Annotations

@Valid

Triggers validation on request body or other objects.

Example:

public User create(@Valid @RequestBody UserDto user) { … }

@NotEmpty, @NotNull, @Size, @Email

Hibernate Validator annotations for rule enforcement.

Exception Handling Annotations

@ExceptionHandler

Handles exceptions thrown in controllers.

Example:

@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity handleNotFound(…) { … }

@ControllerAdvice

Global exception handling.

Example:

@ControllerAdvice
public class GlobalExceptionHandler { … }

Data & JPA Annotations

@Entity

Defines a JPA entity.

Example:

@Entity
public class User { … }

@Id

Marks primary key field:

@Id
private Long id;

Combines well with:

@GeneratedValue

@Transactional

Transactional behavior.

Example:

@Transactional
public void processOrder() { … }

Production note:
Ensure transactions are at service layer, not inside repos.

Scheduling & Async Annotations

@EnableScheduling

Enables scheduled tasks:

@EnableScheduling

@Scheduled

Runs methods at intervals:

@Scheduled(fixedRate = 5000)

@EnableAsync + @Async

Executes methods asynchronously:

@Async
public void processAsync() { … }

Security & CORS Annotations

@CrossOrigin

Used sparingly for CORS.

Example:

@CrossOrigin(origins = "https://app.example.com")

Production-safe tips:
✔ Prefer global CORS config
✔ Avoid wildcard origins

Testing Annotations

@SpringBootTest

Loads full context for integration tests.

Example:

@SpringBootTest
public class AppTests { … }

Use for end-to-end tests, not unit tests.

@MockBean

Overrides real beans with mocks.

Example:

@MockBean
private PaymentService paymentService;

Miscellaneous Useful Annotations

@Primary

Marks a bean as default when multiple exist.

@Primary
@Bean
public DataSource primaryDataSource() { … }

@ConditionalOnProperty

Conditional bean creation based on config values.

Example:

@ConditionalOnProperty(name="feature.email.enabled", havingValue="true")

Very useful in feature flag scenarios.

Recommended Annotation Practices

✔ Keep your annotation usage consistent
✔ Prefer annotation grouping for clarity
✔ Avoid mixing injection patterns
✔ Use qualifiers and primary beans judiciously
✔ Avoid component scan across huge packages

When Not to Use Certain Annotations

❌ Don’t use @Autowired on fields — prefer constructor injection.
❌ Avoid using @CrossOrigin("*") in production.
❌ Do not place @SpringBootApplication outside root package.

Internal Links for Deeper Context

These posts expand on annotation usage in real systems:

Frequently Asked Questions

Are all Spring annotations mandatory?

No — many exist for convenience or structural clarity, but only a few drive core behavior.

Do annotations affect performance?

Not directly — but annotation-triggered behavior (like proxies or transactions) does influence runtime.

Can I define custom annotations?

Yes — Spring is meta-annotation friendly.

Summary — Your Go-To Annotation Guide

Spring Boot annotations are the DNA of your application architecture.
Some:
✔ Drive bean creation
✔ Enable auto-configuration
✔ Control REST APIs
✔ Manage transactions
✔ Handle errors
✔ Configure security

Understanding them deeply helps you build:
📌 Reliable services
📌 Scalable systems
📌 Easily testable code
📌 Production-grade architectures

Bookmark this as your reference guide.

Leave a Comment

Your email address will not be published. Required fields are marked *