Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What do @Component, @Service, @Repository and @Controller have in common, and what does @Repository add?

All four are stereotype annotations, and each is meta-annotated with @Component, so component scanning registers any class carrying them as a bean. @Repository adds behaviour: a post-processor wraps repository beans so that persistence exceptions, such as a Hibernate or JDBC exception, are translated into Spring's DataAccessException hierarchy. @Controller marks a Spring MVC controller, with @RestController adding @ResponseBody, and @Service adds nothing beyond documenting intent.

Why this question is asked

These annotations appear in nearly every Spring codebase, and many developers treat them as interchangeable labels. Interviewers ask about them to check two things: that we know how scanning recognises them at all, and that we know the one place, @Repository, where the choice changes behaviour. In this answer, we will recap component scanning and meta-annotations, see what the four have in common, look closely at what @Repository's exception translation does with a before-and-after example, and then cover @Controller, @RestController and @Service.

1A Quick Recap: Scanning and Meta-Annotations

Component scanning is the process in which Spring walks through packages on the classpath, by default the package of the @SpringBootApplication class and everything below it, and registers a bean for every class annotated with @Component. It finds them by reading class files, so it does not have to load every class.

A meta-annotation is an annotation placed on another annotation. When Spring checks whether a class is a component, it also looks at the annotations on the class's annotations. So if @Service itself is annotated with @Component, a class annotated with @Service counts as a component. We can use the same trick to define our own stereotypes.

Java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component // this line is what makes @Service work with scanning
public @interface Service {
@AliasFor(annotation = Component.class)
String value() default ""; // the bean name, as with @Component("name")
}

The other three are declared the same way. That is everything they have in common: each is a @Component, each accepts an optional bean name, and scanning registers each the same way.

2What @Repository Adds: Exception Translation

Data access code can fail with many different exception types: SQLException from JDBC, JPA's PersistenceException, Hibernate's own ConstraintViolationException, and vendor-specific ones. A service that wants to react to a duplicate key would have to know which persistence technology is underneath. Spring solves this with a common hierarchy rooted at DataAccessException, with subclasses such as DuplicateKeyException, DataIntegrityViolationException and OptimisticLockingFailureException.

@Repository is what connects the two. When PersistenceExceptionTranslationPostProcessor is registered, which Spring Boot does automatically, it wraps every bean annotated with @Repository in a proxy. The proxy catches runtime exceptions thrown from the bean's methods and asks the available PersistenceExceptionTranslator beans, for example the one from the JPA EntityManagerFactory, to convert them into the matching DataAccessException.

Java
@Repository
class JpaCustomerRepository {
@PersistenceContext EntityManager em;
void add(Customer c) { em.persist(c); em.flush(); } // called in a transaction; email is unique
}
try {
repo.add(new Customer("ann@example.com")); // a second time
} catch (DataIntegrityViolationException e) {
System.out.println(e.getClass().getSimpleName()); // DataIntegrityViolationException
System.out.println(e.getCause().getClass().getSimpleName()); // ConstraintViolationException (Hibernate)
}
// Same class annotated with @Component instead of @Repository:
// the caller receives Hibernate's ConstraintViolationException, a raw jakarta.persistence.PersistenceException
Step throughA translated exception
ServiceRepository proxyJpaCustomerRepositoryTranslator
add(customer)
add(customer)
throws PersistenceException
translate the exception
DataIntegrityViolationException
throws DataIntegrityViolationException

6 messages in order. Play them, or step through them one at a time.

Notice that the original exception is kept as the cause, so no information is lost; the caller simply gets a stable type to catch. Interfaces that extend Spring Data's JpaRepository get this translation without the annotation, because Spring Data applies it to the repositories it creates. JdbcTemplate also translates SQL exceptions itself. So @Repository matters most for hand-written DAOs that use an EntityManager or a native API directly.

3@Controller and @RestController

@Controller marks a class as a Spring MVC controller. Scanning registers it as a bean like any component, and in addition Spring MVC's RequestMappingHandlerMapping looks for beans with @Controller and maps their @RequestMapping, @GetMapping and similar methods to URLs. By default a controller method's return value is treated as a view name, which a view resolver turns into an HTML page from a template.

@RestController is itself annotated with @Controller and @ResponseBody. @ResponseBody tells Spring MVC to write the return value directly into the response body with a message converter, usually as JSON with Jackson, instead of looking up a view. So @RestController is the shortcut for REST APIs: we no longer need @ResponseBody on every method.

Java
@Controller
class PageController {
@GetMapping("/home") String home() { return "home"; } // renders templates/home.html
}
@RestController
class ApiController {
@GetMapping("/api/home") String home() { return "home"; } // response body: home
}

4@Service: Intent Only

@Service adds no behaviour of its own today. A class annotated with @Service is registered exactly like one with @Component. Its value is documentation: it tells readers and tools that the class holds business logic, and it gives a convenient target for pointcuts in aspects or architecture tests, for example a rule that controllers may call services but not repositories directly.

So the practical guidance is simple: use the stereotype that describes the class's role. Behaviour only changes for @Repository, and for @Controller in a Spring MVC application. Using @Component everywhere would work, except that hand-written repositories would lose exception translation and the code would say less about its own structure.

The four stereotypes
AnnotationRegistered by scanningExtra behaviour
@Componentyesnone, the base stereotype
@Serviceyesnone, documents a business service
@Repositoryyesexception translation to DataAccessException
@ControlleryesSpring MVC maps its handler methods
@RestControlleryes (via @Controller)adds @ResponseBody to every method

5Follow-up questions

Do I need @Repository on a Spring Data repository interface?

No. Spring Data creates the implementation of interfaces such as JpaRepository and applies exception translation to them itself. Adding @Repository is harmless but redundant. It matters on our own classes that use an EntityManager or a native persistence API directly.

Can I create my own stereotype annotation?

Yes. Declare an annotation with @Component on it, add @AliasFor for the name attribute if needed, and scanning will register classes annotated with it. Teams sometimes create @UseCase or @Adapter annotations to describe their architecture, and can combine them with other annotations such as @Transactional.

Does @Service make methods transactional?

No. Transactions come from @Transactional, handled by a separate proxy. @Service only registers the bean. A common misconception is that the service layer is transactional by default; it is not unless we add @Transactional at the class or method level.

6Conclusion

In this answer, we saw that @Component, @Service, @Repository and @Controller are stereotypes meta-annotated with @Component, so component scanning registers all of them as beans. @Repository adds translation of persistence exceptions into Spring's DataAccessException hierarchy through PersistenceExceptionTranslationPostProcessor. @Controller marks a Spring MVC controller whose methods are mapped to requests, and @RestController adds @ResponseBody so return values become the response body. @Service has no extra behaviour; it documents intent.

What to say in the interviewAbout 80 seconds aloud

Open with

All four are stereotype annotations, and each one is itself annotated with @Component. Spring's scanning checks meta-annotations, so a class with any of them is detected and registered as a bean in exactly the same way, and each takes an optional bean name.

Then, if they want more

The difference is in what else happens. @Repository is the one with extra behaviour: Spring Boot registers PersistenceExceptionTranslationPostProcessor, which wraps @Repository beans in a proxy that catches persistence exceptions, like a JPA PersistenceException or a Hibernate constraint violation, and translates them into Spring's DataAccessException hierarchy, for example DataIntegrityViolationException, keeping the original as the cause.

That way the service layer catches one stable set of exceptions regardless of JPA, JDBC or another technology.

Spring Data repositories get that automatically, so it matters most for hand-written DAOs. @Controller marks a Spring MVC controller, so its request mapping methods are registered and by default return view names; @RestController is @Controller plus @ResponseBody, so return values are written as the body, usually JSON. @Service adds nothing technically; it documents that the class holds business logic and is useful for architecture rules and pointcuts. It also does not make anything transactional; that is @Transactional's job.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What do @Component, @Service, @Repository and @Controller have in common, and what does...": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/cb4ecb1677/