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

advertisement

AddThis Social Bookmark Button

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

Using EJBs with Dependency Injection

In J2EE 1.4, EJB is very complex and it inherits some of the complexities of JNDI. In EJB 3.0, the dependency on another session bean is expressed using the javax.ejb.EJB annotation or the ejb-ref element deployment descriptor element.



Here's the definition of the javax.ejb.EJB annotation:

 @Target({TYPE, METHOD, FIELD}) 
 @Retention(RUNTIME) 
 public @interface EJB { 
 String name() default ""; 
 String beanName() default ""; 
 String mappedName() default ""; 
 String description() default ""; 
 Class beanInterface() default Object.class; 
 }

The following table shows the parameters for javax.annotation.EJB :

Parameter

Type

Description

Default

name

String

The JNDI name of the EJB that's being used. If it's not specified, it's derived from the name of the field or property that's being injected.

""

beanName

String

The name of the EJB, defined either by using the ejb-name element or the name parameter of the bean class.

""

beanInterface

Class

The name of the interface implemented by the object returned by lookup.

mappedName

String

A product-specific name to which the service should be mapped.

""

description

String

Brief description of the EJB.

""

If you want to use an EJB named ProcessManager from a servlet or another EJB, you can use dependency injection to get an instance of the ProcessManager EJB and invoke a method:

@EJB(name="ProcessManager") 
 private ProcessManager pm;
pm.submitOrder(order);

Here, the name is the JNDI name of the EJB being injected. The interface exists for cases when an EJB is used on a class and there is no way of knowing which interface is being used by the component. The beanName is used to disambiguate a bean from another if they both implement the same interface.

Using Web Services with Dependency Injection

Developing and invoking web services is very complex in J2EE. Java EE 5.0 simplifies the development and invocation of XML-based web services by using web services metadata (JSR-181) and the Java API for XML web services 2.0 (JSR-224). You can use the javax.xml.ws.WebServiceRef annotation to define dependency and injection targets for a web service.

The following table describes the parameters that can be passed to the WebServiceRef annotation:

Parameter

Type

Description

Default

name

String

The JNDI name of the service being used. If it's not specified, then it's derived from the name of the field or property into which it's being injected.

""

type

Class

The type of resource being used. If the type is not specified, then it's derived from the type of field or property into which it's being injected.

Object.class

Value

Class

The service class type must extend the javax.xml.ws.Service class.

Object.class

WsdlLocation

String

The URL that points to the location of the WSDL of the web service that's being referred to.

""

mappedName

String

A product-specific name to which the service should be mapped.

""

Here's an example of a WebService annotation. The values for type and name are derived from the injection target:

 @WebServiceRef 
 public PurchaseOrderService poService;

Pages: 1, 2, 3, 4, 5

Next Pagearrow