• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

@Resource vs @Component

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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/
 
Rakesh Basani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
His brain is the size of a cherry pit! About the size of this ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic