| Author |
@Resource vs @Component
|
Rakesh Basani
Ranch Hand
Joined: Jun 04, 2012
Posts: 38
|
|
Hi guys,
I had a horrible day by searching for the difference betweeen @Resource and @Component.
I know how it works if we use @Resource at field-level and method-level. But, how to use @Resource at class-level. Is it similar to @Component.
Please reply guys.
Thanks in advance.
|
 |
Bill Gorder
Bartender
Joined: Mar 07, 2010
Posts: 1282
|
|
@Component is the root stereo type annotation. Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning. The JSR 330 annotation that is more of a drop in replacement for this is the @Named annotation.
Since most projects I am on are unlikely to ever operate without Spring I prefer to use the subclasses of @Component as they more specifically define the behavior of the class and in some cases (such as @Repository) add additional behavior. There are lots of annotations that extend Component some examples are @Service, @Repository, @MessageEndpoint etc.
For more information you can see the reference documentation
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-standard-annotations
Here is a blog discussing the differences between @Resource, @Inject and @Autowired
http://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/
|
[How To Ask Questions][Read before you PM me]
|
 |
Rakesh Basani
Ranch Hand
Joined: Jun 04, 2012
Posts: 38
|
|
Thanks for your quick reply Bill,
The blog you mentioned for @Resource explained only about field level and method level injections. But I desperately need an example on how to use @Resource at class level. That is what my major concern.
|
 |
Bill Gorder
Bartender
Joined: Mar 07, 2010
Posts: 1282
|
|
I have never used nor seen @Resource used in place of @Component. Will it work? I don't know you would have to try but my guess is it won't. Read the Spring documentation here:
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-resource-annotation
Its says the following:
Spring also supports injection using the JSR-250 @Resource annotation on fields or bean property setter methods.
This implies that Spring only supports this annotation on fields or setters. Like I said I prefer to use the Spring annotations since the applications I work on will always be using Spring. If you have a really odd scenario where that is not the case or you just really want to use an JSR annotation instead than use @Named instead of @Component as was described in a link in my previous post.
Note the @Resource annotation is also useful for wiring in collections that are defined as @Bean's
Thanks,
Bill
|
 |
Rakesh Basani
Ranch Hand
Joined: Jun 04, 2012
Posts: 38
|
|
Have a look at these links Bill
http://www.java.net/node/664016
http://docs.oracle.com/javaee/6/tutorial/doc/bnaeo.html (topic--> Declaring reference to a resource)
finally look at this example http://webspherepersistence.blogspot.in/
They are discussing about @Resources tag at class level. But I did not get that.. Could you please verify that once.
|
 |
Bill Gorder
Bartender
Joined: Mar 07, 2010
Posts: 1282
|
|
Yup and those links have nothing at all to do with Spring. My impression from your original question was you were asking about using @Resource inside a Spring application and you were trying to draw comparisons between @Resource and @Component. As far that question is concerned I think my previous posts addressed it.
Now you are moving out of the realm of Spring and into EJB 3.1. @Resource of course is not a Spring annotation but Spring does support its usage in its own way. Without Spring this annotation would be used in a full JEE container and the class that is annotated would be managed by the container. In this case if the @Resource is applied at the class level a name must be specified and that would result in a JNDI (environment) look-up for the resource. See the java doc here:
http://docs.oracle.com/javase/6/docs/api/javax/annotation/Resource.html
|
 |
 |
|
|
subject: @Resource vs @Component
|
|
|