What are Common Spring Boot Annotations?
Spring Boot provides many annotations that simplify Java application development.
These annotations help developers:
- Create REST APIs
- Manage Beans
- Perform Dependency Injection
- Handle databases
- Configure applications
- Build microservices
Annotations reduce boilerplate code and make Spring Boot applications easier to develop.
Simple Definition
Spring Boot annotations are metadata used to configure and manage Spring applications automatically.
Annotations = Instructions given to Spring Framework.
Why are Annotations Important?
Before annotations, Spring applications used large XML configurations.
Annotations simplified:
- Configuration
- Dependency Injection
- REST API development
- Database mapping
- Bean management
Modern Spring Boot applications mainly use annotations instead of XML.
Most Common Spring Boot Annotations
| Annotation | Purpose |
|---|---|
@SpringBootApplication |
Main annotation used to start Spring Boot application. |
@RestController |
Used to create REST APIs. |
@Controller |
Used for MVC controllers. |
@Service |
Marks business logic classes. |
@Repository |
Marks database layer classes. |
@Component |
Marks generic Spring Beans. |
@Autowired |
Performs Dependency Injection. |
@RequestMapping |
Defines base URL mapping. |
@GetMapping |
Handles HTTP GET requests. |
@PostMapping |
Handles HTTP POST requests. |
@PutMapping |
Handles HTTP PUT requests. |
@DeleteMapping |
Handles HTTP DELETE requests. |
@Entity |
Marks database entity classes. |
@Id |
Marks primary key field. |
@GeneratedValue |
Automatically generates IDs. |
1. @SpringBootApplication
This is the main annotation used to start a Spring Boot application.
Example
@SpringBootApplication
public class DhanishEmpowerApplication {
public static void main(String[] args) {
SpringApplication.run(
DhanishEmpowerApplication.class,
args
);
}
}
Internally Includes
- @Configuration
- @EnableAutoConfiguration
- @ComponentScan
2. @RestController
Used to create REST APIs.
Example
@RestController
public class CourseController {
}
Combines:
- @Controller
- @ResponseBody
3. @Controller
Used in Spring MVC applications for returning HTML pages.
Example
@Controller
public class HomeController {
}
4. @Service
Used to mark business logic classes.
Example
@Service
public class PaymentService {
}
Real-Time Example
- PaymentService
- CourseService
- EmailService
5. @Repository
Used to mark database access layer classes.
Example
@Repository
public interface UserRepository
extends JpaRepository<User, Long> {
}
6. @Component
Used to create generic Spring Beans.
Example
@Component
public class JwtUtil {
}
7. @Autowired
Used for Dependency Injection.
Example
@Service
public class CourseService {
@Autowired
private PaymentService paymentService;
}
Spring automatically injects the dependency.
8. @RequestMapping
Used to define base URL paths.
Example
@RequestMapping("/api/courses")
public class CourseController {
}
9. @GetMapping
Handles HTTP GET requests.
Example
@GetMapping
public List<Course> getCourses() {
}
10. @PostMapping
Handles HTTP POST requests.
Example
@PostMapping
public Course saveCourse(
@RequestBody Course course) {
}
11. @PutMapping
Handles HTTP PUT requests.
Example
@PutMapping("/{id}")
public Course updateCourse(
@PathVariable Long id,
@RequestBody Course course) {
}
12. @DeleteMapping
Handles HTTP DELETE requests.
Example
@DeleteMapping("/{id}")
public void deleteCourse(
@PathVariable Long id) {
}
13. @Entity
Marks a class as a database entity.
Example
@Entity
public class Course {
}
14. @Id
Marks primary key field.
Example
@Id
private Long id;
15. @GeneratedValue
Automatically generates primary key values.
Example
@GeneratedValue(strategy =
GenerationType.IDENTITY)
private Long id;
Real-Time Example in Dhanish Empower
@RestController
@RequestMapping("/api/courses")
public class CourseController {
@Autowired
private CourseService courseService;
@GetMapping
public List<Course> getCourses() {
return courseService.getCourses();
}
}
Here:
- @RestController → Creates REST API
- @RequestMapping → Defines base URL
- @Autowired → Injects dependency
- @GetMapping → Handles GET request
Advantages of Spring Boot Annotations
- Reduces XML configuration
- Improves readability
- Simplifies development
- Supports auto configuration
- Improves maintainability
- Speeds up development
- Supports enterprise applications
Production-Level Importance
Spring Boot annotations are heavily used in:
- REST APIs
- Microservices
- Enterprise applications
- Cloud-native systems
- Security modules
- Database integrations
They simplify enterprise-level development significantly.
Interview Answer
Common Spring Boot annotations are metadata used to configure Spring applications automatically.
Important annotations include:
- @SpringBootApplication
- @RestController
- @Autowired
- @Service
- @Repository
- @Component
- @GetMapping
- @PostMapping
- @Entity
These annotations simplify configuration and application development.
Short Interview Answer
Spring Boot annotations are used to configure and manage Spring applications automatically.
Frequently Asked Questions
What is the purpose of @SpringBootApplication?
It starts and configures the Spring Boot application.
What is @RestController used for?
It is used to create REST APIs.
What is @Autowired?
It performs Dependency Injection automatically.
What is @Entity?
It marks a class as a database entity.
What is the difference between @Component and @Service?
@Service is a specialized form of @Component used for business logic classes.
Conclusion
Spring Boot annotations are essential for building modern Java applications.
They simplify configuration, reduce boilerplate code, and improve development speed for enterprise applications, REST APIs, and microservices.