โจ 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!";
}
}
๐ฎ 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
}
}
๐งฑ 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...");
}
}
๐ฅ 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();
}
๐ 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() { ... }
๐งฒ 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;
}
๐ 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;
๐ 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);
}
}
๐งฌ 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;
}
๐ 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.