DEV Community

Vigneshwaralingam
Vigneshwaralingam

Posted on

๐Ÿ” Understanding Key Spring Boot Annotations โ€” What, Why, When, and How

โœจ Introduction

Spring Boot makes backend development faster and cleaner through annotations. If you're a beginner or a student diving into Java full-stack development, mastering these annotations is critical.

In this blog, Iโ€™ll walk you through some of the most important Spring Boot annotations youโ€™ve recently learned:

  • @RestController
  • @Controller
  • @Component
  • @GetMapping
  • @CrossOrigin
  • @Qualifier
  • @Autowired
  • @SpringBootApplication
  • @Entity
  • @Id

Weโ€™ll cover:
โœ… What it is
โœ… Why to use it
โœ… When to use it
โœ… How to use it
โœ… Real-world examples


๐Ÿงฉ 1. @RestController

โœ… What:

Combines @Controller and @ResponseBody. Used to create REST APIs.

๐Ÿ’ก Why:

To build JSON-based REST services without returning views.

โณ When:

When your method returns data (like JSON), not a view (like HTML).

๐Ÿ› ๏ธ Example:

@RestController
public class UserController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฎ 2. @Controller

โœ… What:

Used in MVC applications to return HTML views (like Thymeleaf, JSP).

๐Ÿ’ก Why:

To serve front-end UI pages.

โณ When:

When you're not building REST APIs but handling web page rendering.

๐Ÿ› ๏ธ Example:

@Controller
public class WebController {
    @GetMapping("/home")
    public String home() {
        return "home"; // Loads home.html
    }
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฑ 3. @Component

โœ… What:

Marks a class as a Spring bean (managed by Spring container).

๐Ÿ’ก Why:

To let Spring create and manage your class automatically.

โณ When:

For any generic component (not a controller, service, or repo).

๐Ÿ› ๏ธ Example:

@Component
public class EmailService {
    public void sendEmail() {
        System.out.println("Sending email...");
    }
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ฅ 4. @GetMapping

โœ… What:

Maps HTTP GET requests to a method.

๐Ÿ’ก Why:

To handle URL requests like /users.

โณ When:

Whenever you need to fetch data using GET.

๐Ÿ› ๏ธ Example:

@GetMapping("/users")
public List<User> getAllUsers() {
    return userRepository.findAll();
}
Enter fullscreen mode Exit fullscreen mode

๐ŸŒ 5. @CrossOrigin

โœ… What:

Enables CORS (Cross-Origin Resource Sharing).

๐Ÿ’ก Why:

To allow frontend (React, Angular) to call backend APIs hosted on a different port/domain.

โณ When:

In full-stack apps where backend and frontend run on different ports.

๐Ÿ› ๏ธ Example:

@CrossOrigin(origins = "http://localhost:3000")
@GetMapping("/users")
public List<User> getUsers() { ... }
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฒ 6. @Autowired

โœ… What:

Automatically injects dependencies (beans) into a class.

๐Ÿ’ก Why:

To avoid writing new keyword and promote loose coupling.

โณ When:

When one class depends on another bean.

๐Ÿ› ๏ธ Example:

@Component
public class StudentService {
    @Autowired
    private StudentRepository repo;
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” 7. @Qualifier

โœ… What:

Used with @Autowired to resolve conflicts when multiple beans of the same type exist.

๐Ÿ’ก Why:

To tell Spring which exact bean to inject.

โณ When:

When multiple implementations of an interface exist.

๐Ÿ› ๏ธ Example:

@Autowired
@Qualifier("emailService")
private NotificationService service;
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ 8. @SpringBootApplication

โœ… What:

Main class annotation โ€” combines @Configuration, @EnableAutoConfiguration, and @ComponentScan.

๐Ÿ’ก Why:

To bootstrap and launch your entire Spring Boot application.

โณ When:

Always on your main class.

๐Ÿ› ๏ธ Example:

@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฌ 9. @Entity

โœ… What:

Marks a class as a JPA entity, mapping it to a database table.

๐Ÿ’ก Why:

To let Spring Data JPA manage your DB objects.

โณ When:

For any model class that needs to be stored in the database.

๐Ÿ› ๏ธ Example:

@Entity
public class Employee {
    @Id
    private Long id;
    private String name;
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ†” 10. @Id

โœ… What:

Marks a field as the primary key of your database table.

๐Ÿ’ก Why:

To uniquely identify each record.

โณ When:

Always used inside @Entity classes.


โœ… Summary Table

Annotation Purpose
@RestController Return data (JSON) from REST APIs
@Controller Return web pages in MVC
@Component Register a simple Spring bean
@GetMapping Handle HTTP GET requests
@CrossOrigin Allow frontend access from another origin
@Autowired Automatically inject dependencies
@Qualifier Choose specific bean among multiple
@SpringBootApplication Main app entry point
@Entity Represents a database table
@Id Declares primary key

๐Ÿง  Why Learn These Annotations?

  • Simplify configuration and code
  • Reduce boilerplate
  • Help Spring manage the entire lifecycle of your app
  • Build REST APIs and connect to databases easily
  • Improve readability and maintainability

โœ… ๐ŸŒŸ Similarities Between Spring Boot Annotations

Annotation Similarities
@RestController, @Controller, @Component All are stereotype annotations โ†’ They tell Spring to register a class as a Bean in the Spring container
@GetMapping, @CrossOrigin Both are used in Spring MVC (web apps) and are applied on methods to handle specific behavior for HTTP requests
@Autowired, @Qualifier Both are related to Dependency Injection โ†’ They help Spring decide which bean to inject and where
@SpringBootApplication, @Component, @RestController, @Controller All participate in Component Scanning โ†’ Spring automatically picks them up and adds them to the container
@Entity, @Id Both belong to JPA (Java Persistence API) โ†’ Used for ORM/database mapping
Almost all of them Are metadata instructions to the Spring Framework โ†’ Tell Spring what to do automatically without manual config

๐Ÿ”„ Grouped by Similar Functionality

๐ŸŽฏ 1. Component Scanning Group

These are detected automatically by Spring during component scanning:

  • @Component
  • @RestController
  • @Controller
  • @SpringBootApplication

They make your classes Spring-managed beans.


๐Ÿง  2. Dependency Injection Group

These annotations help Spring inject and wire up beans:

  • @Autowired
  • @Qualifier

They reduce manual object creation and enable loose coupling.


๐ŸŒ 3. Web Layer Group (Spring MVC)

Used in web apps or REST APIs:

  • @RestController
  • @Controller
  • @GetMapping
  • @CrossOrigin

They deal with request handling, routing, and CORS.


๐Ÿ—ƒ๏ธ 4. JPA/Database Group

Used in ORM and database mapping:

  • @Entity
  • @Id

They help convert Java classes into database tables and map primary keys.


๐ŸŽ›๏ธ 5. App Bootstrapping

  • @SpringBootApplication
    This is a meta-annotation that combines:

    • @Configuration
    • @EnableAutoConfiguration
    • @ComponentScan

It starts your entire Spring Boot app.


๐Ÿ”ฅ Final Summary Table

Annotation Purpose Shared Similarity
@RestController REST API controller Detected by component scanning
@Controller MVC web controller Detected by component scanning
@Component Generic bean Spring-managed bean
@GetMapping Handles GET requests Web layer / MVC
@CrossOrigin Allows cross-origin requests Web / CORS handling
@Qualifier Specify which bean to inject Works with @Autowired
@Autowired Inject a bean automatically Dependency injection
@SpringBootApplication Main app class Combines scanning + config
@Entity Database entity Used by JPA
@Id Primary key Used by JPA


Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.