Monday, March 30, 2009

What is the difference between resource reference and resource injection in EJB 3.0 ?

The Resource references and Resource injection are used to describe the process, "Dependency injection". But they differ as,

Resource Reference  
1) It uses annotation to reference objects that the container has access to but are not necessarily directly associated with EJB. 
2) The resource could be a Database, a JMS destination such as queue or topic, or a timer service.
3) Example, "@Resource" annotation is used to indicate resource reference.
4) Sample Code  
  @Resource(name=”jdbc/OracleDS”, resourceType=”javax.sql.DataSource”)  
Resource Injection

1) It uses other annotations to inject references to objects directly related to EJB.
2) EJB specific data.
3) Examples include "@EJB" and "@PersistenceContext".
4) Sample Code
  @EJB( name=”ejb/calci”, beanInterface=Calculator.class,
  beanName=”calci”, description=”This bean is used for calculations.”)
  private Calculator calci;
You want to set up multiple resource references at once at the class level. Here’s a sample:
public class Test{


Post a Comment