Home > Programming / tutorials > Difference between @Service and @Component in Spring

Difference between @Service and @Component in Spring

January 12th, 2010 Leave a comment Go to comments

Spring 2.5 has introduced 3 stereotype annotations: @Component, @Service and @Controller.

The most widely used are @Component and @Service and let us find what is the difference between them.

They both belong to package org.springframework.stereotype

When annotating your class with @Component   you mark it as a regular java component class When annotating with @Service  you mark it as a “special” type of component  for special purpose like transaction or associating with aspects.

From Spring doc about @Service “This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.

So you may ask that a class marked as @Component cannot participate in Transaction ?

Nope that is not true. I have not tried it but will test and post the results here.

Usage : When creating a class like action class to be used in web layer or mail utility , mark them as @Component and when creating service class annotate them as @Service

In future releases there are additional functionality to be added on @Service and @Repository

Categories: Programming / tutorials Tags:
  1. July 22nd, 2016 at 14:23 | #1

    if you don’t mind I would like to expand on the article that you have written with a blog post I wrote about the same subject here: https://readlearncode.com/2016/02/13/insights-from-stackoverflow-most-voted-for-spring-4-questions/#1

  2. March 20th, 2012 at 14:54 | #2

    @vibhas
    Yes. you are right. Fixed it

  3. vibhas
    April 6th, 2011 at 09:06 | #3

    Component and service do not belong to org.springframework.stereotype.Service

  1. No trackbacks yet.