What's the difference between @Component, @Repository & @Service annotations in Spring?

what w
whatsapp app
what c
what p
what h
whatsapp web scan
whatsapp web qr code

Can @Component, @Repository and @Service annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device?

In other words, if I have a Service class and I change the annotation from @Service to @Component, will it still behave the same way?

Or does the annotation also influence the behavior and functionality of the class?

From Spring Documentation:

In Spring 2.0 and later, the @Repository annotation is a marker for any class that fulfills the role or stereotype (also known as Data Access Object or DAO) of a repository. Among the uses of this marker is the automatic translation of exceptions.

Spring 2.5 introduces further stereotype annotations: @Component, @Service, and @Controller. @Component is a generic stereotype for any Spring-managed component. @Repository, @Service, and @Controller are specializations of @Component for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.

Therefore, you can annotate your component classes with @Component, but by annotating them with @Repository, @Service, or @Controller instead, your classes are more properly suited for processing by tools or associating with aspects. For example, these stereotype annotations make ideal targets for pointcuts.

Thus, if you are choosing between using @Component or @Service for your service layer, @Service is clearly the better choice. Similarly, as stated above, @Repository is already supported as a marker for automatic exception translation in your persistence layer.

│ Annotation │ Meaning                                             │
│ @Component │ generic stereotype for any Spring-managed component │
│ @Repository│ stereotype for persistence layer                    │
│ @Service   │ stereotype for service layer                        │
│ @Controller│ stereotype for presentation layer (spring-mvc)      │

What, Quickly send and receive WhatsApp messages right from your computer. Quickly send and receive WhatsApp messages right from your computer. Quickly send and receive WhatsApp messages right from your computer. WhatsApp Web.

As many of the answers already state what these annotations are used for, we'll here focus on some minor differences among them.

First the Similarity

First point worth highlighting again is that with respect to scan-auto-detection and dependency injection for BeanDefinition all these annotations (viz., @Component, @Service, @Repository, @Controller) are the same. We can use one in place of another and can still get our way around.

Differences between @Component, @Repository, @Controller and @Service


This is a general-purpose stereotype annotation indicating that the class is a spring component.

What’s special about @Component <context:component-scan> only scans @Component and does not look for @Controller, @Service and @Repository in general. They are scanned because they themselves are annotated with @Component.

Just take a look at @Controller, @Service and @Repository annotation definitions:

public @interface Service {


public @interface Repository {


public @interface Controller {

Thus, it’s not wrong to say that @Controller, @Service and @Repository are special types of @Component annotation. <context:component-scan> picks them up and registers their following classes as beans, just as if they were annotated with @Component.

Special type annotations are also scanned, because they themselves are annotated with @Component annotation, which means they are also @Components. If we define our own custom annotation and annotate it with @Component, it will also get scanned with <context:component-scan>


This is to indicate that the class defines a data repository.

What’s special about @Repository?

In addition to pointing out, that this is an Annotation based Configuration, @Repository’s job is to catch platform specific exceptions and re-throw them as one of Spring’s unified unchecked exception. For this, we’re provided with PersistenceExceptionTranslationPostProcessor, that we are required to add in our Spring’s application context like this:

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

This bean post processor adds an advisor to any bean that’s annotated with @Repository so that any platform-specific exceptions are caught and then re-thrown as one of Spring’s unchecked data access exceptions.


The @Controller annotation indicates that a particular class serves the role of a controller. The @Controller annotation acts as a stereotype for the annotated class, indicating its role.

What’s special about @Controller?

We cannot switch this annotation with any other like @Service or @Repository, even though they look same. The dispatcher scans the classes annotated with @Controller and detects methods annotated with @RequestMapping annotations within them. We can use @RequestMapping on/in only those methods whose classes are annotated with @Controller and it will NOT work with @Component, @Service, @Repository etc...

Note: If a class is already registered as a bean through any alternate method, like through @Bean or through @Component, @Service etc... annotations, then @RequestMapping can be picked if the class is also annotated with @RequestMapping annotation. But that's a different scenario.


@Service beans hold the business logic and call methods in the repository layer.

What’s special about @Service?

Apart from the fact that it's used to indicate, that it's holding the business logic, there’s nothing else noticeable in this annotation; but who knows, Spring may add some additional exceptional in future.

What else?

Similar to above, in the future Spring may add special functionalities for @Service, @Controller and @Repository based on their layering conventions. Hence, it's always a good idea to respect the convention and use it in line with layers.

Drake - Nice For What, I will do what I can to help you. TranslationsEdit. show ▽what? (interrogative pronoun)  the true nature or identity of something, or the sum of its characteristics: a lecture on the whats and hows of crop rotation.

WhatsApp Web, Colorado Coronavirus Updates: What You Need To Know Today. By Colorado Public Radio Staff. April 20, 2020. Share: A man who declined to be named  We use interrogative pronouns to ask questions. They are: who, which, whom, what and whose. These are also known as wh-words. Questions using these are called wh-questions:

what, what meaning: 1. used to ask for information about people or things: 2. used in questions that show you are…. Learn more. informal a punishment or reprimand (esp in the phrase give (a person) what for) 7. what have you someone, something, or somewhere unknown or unspecified: cars, motorcycles, or what have you. 8.

In Spring @Component, @Service, @Controller, and @Repository are Stereotype annotations which are used for:

@Controller: where your request mapping from presentation page done i.e. Presentation layer won't go to any other file it goes directly to @Controller class and checks for requested path in @RequestMapping annotation which written before method calls if necessary.

@Service: All business logic is here i.e. Data related calculations and all.This annotation of business layer in which our user not directly call persistence method so it will call this method using this annotation. It will request @Repository as per user request

@Repository: This is Persistence layer(Data Access Layer) of application which used to get data from the database. i.e. all the Database related operations are done by the repository.

@Component - Annotate your other components (for example REST resource classes) with a component stereotype.

Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.

Other class-level annotations may be considered as identifying a component as well, typically a special kind of component: e.g. the @Repository annotation or AspectJ's @Aspect annotation.

Colorado Coronavirus Updates: What You Need To Know Today , Medical researchers have been studying everything we know about Covid-19. What have they learned – and is it enough to halt the pandemic? WhatsApp Messenger: More than 2 billion people in over 180 countries use WhatsApp to stay in touch with friends and family, anytime and anywhere. WhatsApp is free and offers simple, secure, reliable messaging and calling, available on phones all over the world.

WHAT, "A bird's experience is far richer, complex, and 'thoughtful' than I'd imagined." This sentence on the first page of David Allen Sibley's What It's  This is the ORIGINAL "What What (In the Butt)" video. The South Park version aired over a year after we made this video. There's been a lot of confusion about this. We can assure you, we are not

Coronavirus: five months on, what scientists know about Covid-19 , Beyond 'Trolls' and 'Frozen': What Are Our Kids Listening To? Children's films lean heavily on pop, but the elementary school set has access to a  Seen a font in use and want to know what it is? Upload an image to WhatTheFont to find the closest matches in our database.

David Allen Sibley Offers Some Thoughts On 'What It's Like To Be A , Find out what your public IPv4 and IPv6 address is revealing about you! My IP address information shows your location; city, region, country, ISP and location on a map.

  • Being a developer with Microsoft background, I recall the semantic definition of services in the old MS SmartClientSoftwareFactory framework (now a long deprecated complex framework for distributed desktop apps). That definition (nicely documented by Rich Newman) defined the services as stateless reusable objects, preferably with singleton scope, that are used to perform business logic operations on other objects passed as arguments. I tend to view Spring services the same way
  • Doesn't matter!! Whatever works for you :) I've always hated this about Spring that they always tend to define "rules" for you, which only add trivial value to your application. Not to mention Spring comes with huge stack of its own.
  • @TriCore Sprting is a framework, define "rules" for you is its job :)
  • Would it make sense to add @Controller (or @Component) to an @WebServlet? It's not a Spring MVC controller, but that's the conceptually closest match. What about servlet filters?
  • what does "@Repository is already supported as a marker for automatic exception translation in your persistence layer." mean?
  • It's referring to the fact that these annotations are good targets for AOP, and while the other annotations do not define a pointcut yet, they might do that in the future. On the other hand @Repository is already a target for a pointcut at present. That pointcut is used for exception translations, i.e. translating technology-specific exceptions to more generic Spring-based ones, to avoid tight coupling.
  • @stivlo : I have really tried to understand the term 'stereotype', still not understand. Could you please help me to understand this terminology? It helps a lot and thank you very much
  • @xenoterracide There is practically not a lot of difference. Something annotated with @Service is also a @Component (because the @Service annotation itself is annotated with @Component). As far as I know, nothing in the Spring framework explicitly makes use of the fact that something is a @Service, so the difference is really only conceptual.
  • 'PersistenceExceptionTranslationPostProcessor' will be automatically registered if JPA is detected.
  • Fantastic Explanation. You've cleared up a lot of my misunderstandings. Coming from a university where we built all our projects from the bottom up, I had difficulty understanding why Spring Applications just worked even though you're not explicitly linking the program together yourself. The annotations make a lot of sense now, thank you!