Spring Boot @Component Annotation Explained

📌 Spring Boot Annotation Series – Part 8 ✅ @Component annotation The @Component annotation is used to mark a class as a Spring-managed bean 👇 🔹 Why do we use @Component? To tell Spring: 👉 “Create an object of this class and manage it.” - To enable dependency injection - To let Spring detect the class during component scanning 🔹 How does it work? When Spring Boot starts: - It scans packages (using @ComponentScan) - Finds classes annotated with @Component - Creates and registers them as beans in the Spring container 🔹 Simple example @Component public class EmailService { public void sendEmail() { System.out.println("Email sent"); } } Now this class can be injected using: @Autowired private EmailService emailService; 🔹 In simple words @Component tells Spring to automatically create and manage this class as a bean. 👉 🧠 Quick Understanding - Marks a class as a Spring bean - Detected during component scanning - Enables dependency injection #SpringBoot #Java #Component #BackendDevelopment #LearningInPublic

To view or add a comment, sign in

Explore content categories