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, 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: