What is the difference between @Component, @Repository and @Service in Spring?
Hi! I started learning Spring and trying to understand stereotype annotations. It seems like all three (@Component, @Repository, @Service) do the same thing — register a bean in the Spring context. But why are there three of them?
I wrote a simple service for working with users. Here is my code:
```java
@Component
public class UserService {
public void saveUser(User user) {
System.out.println("User saved: " + user.getName());
}
}
@Repository
public class UserRepository {
public void saveToDatabase(User user) {
// database work here
}
}
@Service
public class UserManager {
public void processUser(User user) {
// business logic
}
}
```
All three work, I can inject any of them using @Autowired. So what is the fundamental difference?
No answers yet.
Log in to leave an answer