Marketplace for Content, Tests and Assessment
 
 
Home > Programming / tutorials > Difference between @Service and @Component in Spring

Difference between @Service and @Component in Spring

January 12th, 2010 Vinay 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 org.springframework.stereotype.Service

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. No comments yet.
  1. No trackbacks yet.
Get Adobe Flash playerPlugin by wpburn.com wordpress themes