28

I understand that @Service is used to mark a class as a business logic class. Why is it useful for spring? I can already use @Component to mark a class as a bean. I understand that @Service is more specific than @Component, but how?

webpersistence
  • 894
  • 3
  • 12
  • 29
  • we can use `@Component` but I think `@Service` specifies intent better. both works. – Sachin Chavan Dec 06 '17 at 07:27
  • I do not understand if @Service is handled differently – webpersistence Dec 06 '17 at 07:28
  • See: [What's the difference between Component, Repository & Service annotations in Spring?](https://stackoverflow.com/questions/6827752/whats-the-difference-between-component-repository-service-annotations-in) – Jesper Dec 06 '17 at 07:31

3 Answers3

35

Consider @Component annotation as a Swiss Knife. It can act as cutting knife, as an opener, as a scissor, etc.

Component

Similarly, your Component can act as a Repository, as Business Logic class or as a controller.

Now, @Service is a just one of the versions of @Component, say a knife.

Spring process @Service similar to @Component, since @Service interface itself is annotated with @Component.

From Spring docs.:

@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Component
public @interface Service

Indicates that an annotated class is a "Service" (e.g. a business service facade).

Why to differentiate both of them?

To apply the basic rule of programming: Your code should be easily readable.

Yes, you can use @Component annotation everywhere and it will work fine but for the better understanding of the code, it is preferred to use the respective special types of annotations like @Service in our case.

The other benefit is ease of debugging. Once you know the error, you need not hope from one component class to another, checking it time whether that class is service, repository or controller.

Dhaval Simaria
  • 1,886
  • 3
  • 28
  • 36
28

@Service, @Controller, @Repository = {@Component + some more special functionality}

Click the below link for more details

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

The @Component annotation marks a java class as a bean so the component-scanning mechanism of spring can pick it up and pull it into the application context. The @Service annotation is also a specialization of the component annotation. It doesn’t currently provide any additional behavior over the @Component annotation, but it’s a good idea to use @Service over @Component in service-layer classes because it specifies intent better. Additionally, tool support and additional behavior might rely on it in the future.

Sankar
  • 687
  • 1
  • 13
  • 25
6

The @Service annotation is a specialization of the component annotation. It doesn’t currently provide any additional behavior over the @Component annotation, but it’s a good idea to use @Service over @Component in service-layer classes because it specifies intent better. Additionally, tool support and additional behavior might rely on it in the future.

Ramesh Fadatare
  • 561
  • 4
  • 12