Video in progress

Junior 🌱 Spring

What is the difference between @Component, @Service, @Repository and @Controller?

All four turn a class into a bean. @Service, @Repository, @Controller are more specific flavours of @Component; on top of that, @Repository translates database errors into Spring exceptions.

📖 Answer

@Service, @Repository and @Controller have @Component inside them, so component scan finds all of them.

Annotation Where Extra behaviour
@Component general helper class —
@Service business logic none, just meaning
@Repository layer that talks to the database translates SQL/JPA errors into DataAccessException
@Controller web layer Spring MVC routes requests to this class

If you write @Component instead of @Service, it still works — it's just that someone reading the code won't see the layer right away. The right annotation is documentation in itself.

You don't need @Repository on Spring Data JPA interfaces (extends JpaRepository) — Spring finds them on its own.