• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on @Resource Annotation

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

Why is the type attribute necessary when we apply the @Resource Annotation at class level?
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ho Jothi,

According to the javadoc of the @Resource annotation:


The Java type of the resource. For field annotations, the default is the type of the field. For method annotations, the default is the type of the JavaBeans property. For class annotations, there is no default and this must be specified.


In other words, when annotating a field or property the type attribute can be inferred form that field/property's java type. When you annotate the class, however, the Container is not able to infer it and thus it needs to be specified.
[ August 25, 2008: Message edited by: Sergio Tridente ]
 
S Bhanage
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Just to note: The rule applies for almost all annotations.

Container tries to fetch implicitly as much information as it can.
Whenever there are situations like "type" for @Resource when declared
at class level, Container is unable to get values for this attribute by its own.
So declaration of this attribute is made mandatory.

Regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic