Core Spring Annotations — Deep Explanation with
Examples
@Component
What It Is
@Component marks a Java class as a Spring-managed bean. Spring automatically
detects and registers it in the ApplicationContext.
Why It Is Used
• To enable Inversion of Control (IoC).
• Spring handles object creation, lifecycle, and dependency injection.
How It Works Internally
During startup, Spring performs component scanning and registers classes annotated
with: - @Component - @Service - @Repository - @Controller
These become beans stored in the IoC container.
Where It Is Used
• Utility classes
• Custom validators
• Helper components
Code Example
@Component
public class EmailValidator {
public boolean isValid(String email) {
return email != null && [Link]("@");
}
}
@Service
What It Is
A specialization of @Component that marks a class as a service-layer bean.
Why It Is Used
• Represents business logic.
• Makes code cleaner by grouping all business operations.
How It Works Internally
• Spring marks it as a service component.
• Enables transaction management if used with @Transactional.
Where It Is Used
• User service, Order service, Payment service.
Code Example
@Service
public class UserService {
public String getUserRole(String username) {
return "ADMIN";
}
}
@Repository
What It Is
A specialization of @Component used for DAO / persistence logic.
Why It Is Used
• Helps separate DB logic from business logic.
• Adds Spring’s exception translation (converts SQL exceptions → Spring
DataAccessException).
How It Works Internally
Spring wraps methods in a proxy and intercepts database exceptions.
Where It Is Used
• Data access layer
• JPA repositories
Code Example
@Repository
public class UserRepository {
public void save(User user) {
// DB logic
}
}
@Controller
What It Is
Used in Spring MVC to de ne controllers that return HTML views, not JSON.
Why It Is Used
• To create UI-based applications.
• Returns templates like JSP, Thymeleaf, Freemarker.
How It Works Internally
• Uses DispatcherServlet.
• Matches request → returns view name.
Where It Is Used
• Web applications (non-REST)
• Portals, dashboards
Code Example
@Controller
public class HomeController {
@GetMapping("/home")
public String homePage() {
return "home"; // [Link]
fi
}
}
@RestController
What It Is
A combination of @Controller + @ResponseBody.
Why It Is Used
• To create REST APIs.
• Automatically converts response objects → JSON using Jackson.
How It Works Internally
• Every method returns data instead of a view.
• Spring uses HttpMessageConverters.
Where It Is Used
• APIs, microservices, backend servers.
Code Example
@RestController
public class UserApi {
@GetMapping("/user")
public User getUser() {
return new User("John", "Developer");
}
}
@ComponentScan
What It Is
Tells Spring where to search for components to auto-register.
Why It Is Used
• Required when project has multiple packages.
How It Works Internally
• Scans speci ed packages recursively.
• Registers all classes annotated with stereotype annotations.
Where It Is Used
• Custom Spring con guration.
Code Example
@Configuration
@ComponentScan(basePackages = "[Link]")
public class AppConfig {}
@Autowired
What It Is
Injects dependencies automatically.
Why It Is Used
• Avoids manual creation using new.
• Promotes decoupled architecture.
How It Works Internally
Dependency resolution happens via: 1. By Type (primary) 2. By Quali er 3. By Name
Where It Is Used
• Service injection
• Repository injection
• Utility injection
Code Example
@Service
public class OrderService {
@Autowired
private PaymentService paymentService;
fi
fi
fi
public void placeOrder() {
[Link]();
}
}
@Quali er
What It Is
Used to specify exact bean when multiple beans exist of the same type.
Why It Is Used
• Avoids ambiguity during dependency injection.
How It Works Internally
Matches bean name with the quali er value.
Where It Is Used
• Multiple payment methods
• Multiple implementations of same interface
Code Example
@Service("credit")
public class CreditCardPayment implements PaymentService {}
@Service("paypal")
public class PaypalPayment implements PaymentService {}
@Service
public class OrderService {
@Autowired
@Qualifier("paypal")
private PaymentService paymentService;
}
fi
fi
@Primary
What It Is
Marks a bean as the default candidate when multiple beans exist.
Why It Is Used
• Helps avoid repeated usage of @Qualifier.
How It Works Internally
Spring selects the @Primary bean automatically unless overridden using @Qualifier.
Where It Is Used
• Default implementations
• Major business ows
Code Example
@Primary
@Service
public class DefaultPaymentService implements PaymentService {}
@Service
public class BackupPaymentService implements PaymentService {}
fl