Bean Wiring: @Component, @Service, @Repository
Use stereotype annotations, define beans with @Bean, and understand component scanning.
Stereotype Annotations
Spring provides stereotype annotations that mark classes as Spring-managed beans and convey their role: @Component (generic), @Service (business logic), @Repository (data access), @Controller/@RestController (web layer).
@Component: Generic Bean
Any class annotated with @Component is detected during component scan and registered as a Spring bean. It is the base for all other stereotypes.
@Component
public class EmailFormatter {
public String format(String to, String subject) {
return "To: " + to + "\nSubject: " + subject;
}
}All lessons in this course
- Auto-Configuration and Spring Boot Starters
- Application Properties and Profiles
- Bean Wiring: @Component, @Service, @Repository
- Constructor Injection and Circular Dependencies