1

In Spring we have @Componenet, @Repository annotations. I have researched some stuff and found the below differences between the @Component and the other one.

@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.

  1. My confusion is if we replace @Repository with @Component, do we get any error(i suspect). If so, is there any possibility to achieve the stereotype/functionality of a Repository by using @Component + some manual config(what we call a boilerplate code). I am asking this just to understand the concept better.

  2. The second main significance that is posted across blogs is, exception handling will be better if we use layer-based annotations like @Service, @Controller, @Repository. But could not find a good explanation or example for the same.

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
  • 1
    please read this: [baeldung spring-component-repository-service](https://www.baeldung.com/spring-component-repository-service) and [baeldung spring-component-scanning](https://www.baeldung.com/spring-component-scanning) – Dirk Deyne Jun 07 '19 at 18:44
  • *"if we replace @Repository with @ComponentScan"* You would never do that, because `@ComponentScan` is not similar to `@Repository`, `@Component` is. The `@ComponentScan` annotation is used to define *which* classes should be scanned for `@Component` annotated classes and classes annotated with a similar derived annotation like `@Controller`, `@Service`, and `@Repository`. – Andreas Jun 07 '19 at 18:52
  • @Andreas yes i meant Component. i have edited the question,, was confused while writing. – Gopalakrishnan Jun 07 '19 at 18:56
  • 1a) *"do we get any error"* No. --- 1b) *"any possibility to achieve the stereotype/functionality"* Yes. --- 2) Only `@Repository` does automatic translation of exceptions. The others don't. – Andreas Jun 07 '19 at 19:12

1 Answers1

1

From the Spring Framework Documentation:

Spring provides 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 (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. @Repository, @Service, and @Controller can also carry additional semantics in future releases of the Spring Framework. Thus, if you are choosing between using @Component or @Service for your service layer, @Service is clearly the better choice. Similarly, as stated earlier, @Repository is already supported as a marker for automatic exception translation in your persistence layer.

This means that they are there so you can use them to indicate function. @Service for services, @Repository for repositories, etc. We would do this so we can more easily identify the intent of the code being decorated. It also helps when defining aspects (as in Aspect Oriented Programming), so we can say "All repositories should have X pointcut" without having to spell out packages or anything. We would just look for the @Repository annotation.

I have miscategorized my classes before (using @Component for a @Service) and never ran into any problems. But that's not to say there won't be issues, especially if Spring or some other library is using them for AOP purposes.

Todd
  • 30,472
  • 11
  • 81
  • 89