Annotation @resource in Spring v.s Java EE

In Spring, @Resource annotation can be applied on setter method or field. @Resource has a optional name attribute. If name attribute is not assigned then default matching class type of property is bound. Otherwise, specified name will be bound to be @resource property.

@Resource matching sequence in Spring
    - Matches by Name
    - Matches by Type

    - Restricts by Qualifiers (ignored if match is found by name)


 public interface Person {...  
 }  
 @Component  
 public class Employee implements Person {   
 }  


  • Use case 1:
 @Resource  
 private Person person;  
  • Use case 2:
 @Resource  
 private Person employee;  
  • Use case 3:
 @Resource  
 private Employee person;  
  • Use case 4: use a ‘@Qualifier’ annotation to point to the default name of the Employee component
 @Resource  
 @Qualifier("employee")  
 private Person person;  
  • Use case 5: customized qualifier name
 @Component  
 @Qualifier("employeeBean")  
 public class Employee implements Person {  
 }  
 @Resource  
 @Qualifier("employeeBean")private Person person;  
  • Use case 6: when qualifier name conflicts with matching field name, use field name instead. e.g "newBean" will be ignored in following case.
 @Resource  
 @Qualifier("newBean")  
 private Person employee; 
 
In Java EE: is used to declare a reference to a resource; @Resource can decorate a class, a field, or a method. The name element is the JNDI name of the resource and is optional for field-based and method-based injection. For field-based injection, the default name is the field name qualified by the class name. For method-based injection, the default name is the JavaBeans property name, based on the method qualified by the class name. The name element must be specified for class-based injection.

  • Field Based Injection
      //inferred name of resource myDB, inferred type of resource: javax.sql.DataSource  
   @Resource  
   private javax.sql.DataSource myDB;  
   //inferred name of resource customerDB, type of resource: javax.sql.DataSource  
   @Resource(name="customerDB")  
   private javax.sql.DataSource myDB;  
  • Method based Injection
      //inferred name of resource myDB, inferred type of resource: javax.sql.DataSource  
   private javax.sql.DataSource myDB;  
   @Resource  
   private void setMyDB(javax.sql.DataSource ds) {  
     myDB = ds;  
   }  
   //inferred name of resource customerDB, type of resource: javax.sql.DataSource  
   private javax.sql.DataSource myDB;  
   @Resource(name="customerDB")  
   private void setMyDB(javax.sql.DataSource ds) {  
     myDB = ds;  
   }  
  • Class Based Injection
   //name and type are required  
   @Resource(name="myMessageQueue",  
         type="javax.jms.ConnectionFactory")  
   public class SomeMessageBean {  
    ...  
   }  
   //@Resources annotation is used to group together multiple @Resource declarations for class-based injection.  
   @Resources({  
    @Resource(name="myMessageQueue",  
           type="javax.jms.ConnectionFactory"),  
    @Resource(name="myMailSession",  
           type="javax.mail.Session")  
   })  
   public class SomeMessageBean {  
    ...  
   }  

No comments :

Post a Comment