Spring Stereotypes: @Service, @Repository and @Controller Are @Component With a Job Name
PostService gets @Service, a new PostRepository gets @Repository, and EmailNotifier keeps @Component. Spring scans and builds all three the same way; the names tell the reader what job each class does.
What you will learn
- Explain that @Service, @Repository and @Controller are kinds of @Component
- Pick the right stereotype for a class from its job
- Follow a run where Spring builds three beans and injects two of them
- Know that component scanning finds every stereotype
Before you start
- Spring Component Scanning
- Spring Autowiring
So far every class in the Mini Blog wore a plain @Component. Spring has three more names for it: @Service, @Repository and @Controller. Each one is still a @Component; the name just says what job the class does.
A restaurant: same staff, different job titles
The waiter takes your order. That's @Controller, which takes requests from the web (we'll meet it with Spring MVC). The chef cooks by the rules: @Service. The storeroom keeps the food: @Repository. Everyone is staff, and in the same way every one of these is a @Component.
@Repository keeps the posts
package org.studyeasy.blog;
import org.springframework.stereotype.Repository;
// Keeps the posts. @Repository = a @Component whose job is storing data.
@Repository
public class PostRepository {
public void save(String title) {
System.out.println("Post saved: " + title);
}
}
A new class. A real app would talk to a database here; for now it prints. Nobody writes new PostRepository(); Spring does.
@Service holds the blog's rules
package org.studyeasy.blog;
import org.springframework.stereotype.Service;
// The blog's rules: save the post, then tell the readers.
// @Service = a @Component whose job is the app's logic.
@Service
public class PostService {
private final PostRepository repository;
private final EmailNotifier notifier;
public PostService(PostRepository repository, EmailNotifier notifier) {
this.repository = repository;
this.notifier = notifier;
}
public void publish(String title) {
repository.save(title);
System.out.println("Post published: " + title);
notifier.send(title);
}
}
The constructor takes both a PostRepository and an EmailNotifier, and Spring hands in both beans. publish saves first, then prints, then sends the email.
Plain @Component when no job name fits
package org.studyeasy.blog;
import org.springframework.stereotype.Component;
// Tells the readers. No job name fits, so plain @Component.
@Component
public class EmailNotifier {
public void send(String title) {
System.out.println("Email sent: " + title);
}
}
A notifier isn't business rules or storage, so it keeps the plain @Component. If you're not sure which name fits, @Component is always fine.
BlogConfig and main don't change
package org.studyeasy.blog;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
// Tells Spring: look in this package for @Component classes.
// @Service and @Repository count too: they are @Component.
@Configuration
@ComponentScan
public class BlogConfig {
}
Why does a scan for @Component also find @Service and @Repository? Because they are @Component too. main is the same as in the earlier lessons: start the context, getBean(PostService.class), publish("Hello, Spring"), close.
Post saved: Hello, Spring
Post published: Hello, Spring
Email sent: Hello, Spring
Which name when?
| Annotation | Job | In this example |
|---|---|---|
@Service | the app's rules | PostService |
@Repository | storing and loading data | PostRepository |
@Controller | web requests | later, with Spring MVC |
@Component | anything else | EmailNotifier |
Swap @Service for @Component on PostService and the program runs exactly the same. The names are for people, so they can see a class's job at a glance.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Why Spring in 2026: What the Container Does and What Spring Framework 7 Changed
Spring in one line: a container that creates your classes and hands each one what it needs. Plus the Spring Framework 7 baseline (JDK 17 to 25, Jakarta EE 11, Tomcat 11, JUnit 6), what got better than Spring 6, and a two-line version check.
A Plain Spring Project, No Boot: Your First Bean With spring-context
One dependency, one recipe, one getBean: a Maven project with spring-context 7.0.9, a Blog record, an AppConfig with @Bean, and a main that asks the container for the Blog. Then delete @Bean and meet NoSuchBeanDefinitionException.
Inversion of Control and Dependency Injection in Spring: One Program, Built Three Ways
A PostService needs a PostRepository. Build it three ways: the service calls new itself (tight coupling), main hands it in through the constructor (DI by hand), and the Spring container does the wiring from a @Configuration recipe.
Spring Component Scanning: @Component and @ComponentScan Instead of @Bean Methods
Instead of one @Bean method per class, put @Component on EmailNotifier and PostService and @ComponentScan on an empty BlogConfig. Spring walks the package, finds the stickers and builds the beans; main doesn't change.
