Mock tests, Interview questions, Tutorials and Tech news
 
 
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

Be Sociable, Share!
Categories: Programming / tutorials Tags:
  1. March 20th, 2012 at 14:54 | #1

    @vibhas
    Yes. you are right. Fixed it

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

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

  1. No trackbacks yet.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes