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.