ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Using Dependency Injection in Java EE 5.0
Pages: 1, 2, 3, 4, 5

Using Dependency Injection in Java EE

As I outlined earlier, you can either use metadata annotations or deployment descriptor elements to declare dependencies upon resources. The Common Metadata annotations for Java Platform under JSR 250 that was recently proposed as a final draft defines two annotations (javax.annotation.Resource and javax.annotation.Resources) for defining dependencies on resources. EJB 3.0 under JSR 220 defines javax.ejb.EJB for injection of EJBs, while the Java API for XML web services 2.0 defines the javax.xml.ws.WebServiceRef annotation for injecting web services references.

The Resource annotation can be used on a class or methods or fields of a managed class, such as an EJB or servlet. You can use the Resource annotation to define dependencies on any type of resource, such as DataSource, JMS, Mail, URL, or environment entries.

Here's the definition of the javax.annotation.Resource interface:

public @interface Resource {    
public enum AuthenticationType {CONTAINER,APPLICATION
String name() default '';    
Class type() default Object.class;    
AuthenticationType authenticationType() 
            default  AuthenticationType.CONTAINER;    
boolean shareable() default true;
String mappedName default ''; 
description() default '';}

The following table defines the parameters that can be specified with javax.annotation.Resource:







The JNDI name of the resource being used. If the type isn't specified, it's derived from the name of the field or property being injected.




The type of resource being used. If the type isn't specified, it's derived from the type of field or property being injected.



Enum AuthenticationType { CONTAINER, APPLICATION }

The type of authentication needed to use the resource.




Indicates the resource is shareable.




Brief description of this resource.




A product-specific name to which the resources should be mapped. The mappedName is vendor-dependent; you cannot port it across containers.


Types of Injection

Java EE 5.0 supports two types of injection: field and setter. Field injection lets you inject a resource to a field, while setter injection lets you inject a resource by invoking a setter method.

To use a field injection, simply define a field and annotate it to be a resource reference. If you don't define the resource's name and type, the container will derive this information from the field's name and type. For example, you can inject a DataSource to a field as follows:

 private javax.sql.DataSource AdventureDB;

In this example, a DataSource with JNDI name AdventureDB should be configured; if not, the container will throw an exception. The injection must be completed before any methods of the managed class can accessed by a client.

Conversely, setter injection (or "property injection") lets you define a set...() method using JavaBeans rules, and annotate it as a resource reference. The application doesn't need to invoke the setter method; the Java EE container method will invoke the setter method before it invokes the business methods to which the resources are being injected. For example, you must have a setter method named setAdventureDB if you have a field named adventureDB. If you don't define the name or type of a setter injection, it will be derived from the setter method's name and parameter type. For example, here is a setter method with a resource reference:

private void setAdventureDB(javax.sql.DataSource ds) 
 adventureDB = ds; 
private DataSource adventureDB;

Pages: 1, 2, 3, 4, 5

Next Pagearrow