Injection

The EJB 3.0 spec allows different types of injections such as dependency injection and simple environment entry injections. We will discuss how the EJB3 cartridge handles these injections differently.

Dependency Injection

To inject dependencies using the EJB3 cartridge, simply model a dependency from the source class to the injected target class where each class has a suitable stereotype associated with it.

You can inject a session bean instance into a session or a message-driven bean by drawing a dependency from the source session bean to the target session or message-driven bean. The source bean will include the @EJB annotation on the declared field. Upon creation of the source bean instance, the EJB container will set the instance of the injected destination bean using the JNDI name of the referenced EJB.

The injected session bean instance name is simply the name of the target session bean with lower case first character.

The EJB 3.0 spec allows resource injections using the @Resource annotation and the EJB3 cartridge uses this feature in a few ways.

Firstly, all session beans automatically inject the SessionContext using the @Resource annotation into the instance context using a protected visibility. You can then use this SessionContext in the session bean implementation.

If a session bean is marked for bean managed transaction demarcation by modeling the andromda_ejb_transaction_management tagged value set to BEAN, then the EJB3 cartridge will automatically inject an instance of UserTransaction into the variable userTrans using a protected visibility. You can then use this UserTransaction instance in the session bean implementation.

The EJB3 cartridge will also use the @Resource annotation to setup an instance of the appropriate ConnectionFactory and Destination objects for sending messages to JMS queues/topics where you have servicing message-driven beans. By modeling a dependency from the source session bean to the target message-driven bean, be it a MDB servicing a Topic or Queue, all the necessary injections are generated. You can find an example in the message-driven howto.

Simple Environment Entry Injection

Setting up an environment entry configuration parameter is very simple using the EJB3 cartridge. The environment entry value may have one of the following Java types:

  • String
  • Character
  • Integer
  • Boolean
  • Double
  • Byte
  • Short
  • Long
  • Float

To inject an environment entry resource value, model a service or message-driven bean attribute with classifier scope and set the attribute stereotype to <<EnvEntry>>. The environment entries will be defined in the session or message-driven bean class and configured in the ejb-jar.xml deployment descriptor. You must define a default value for these attributes in the model.

The following example illustrates how you can inject environment entries into session beans.

  • Auto-generated source that does not need manual editing
  • Auto-generated source that should be edited manually
  • File that is affected by the modifications applied in this section
  • CarEmbeddable.java
  • Car.java
  • CarType.java
  • CarDao.java
  • CarDaoBase.java
  • CarDaoImpl.java
  • CarDaoException.java
  • Person.java
  • PersonEmbeddable.java
  • PersonDao.java
  • PersonDaoBase.java
  • PersonDaoImpl.java
  • PersonDaoException.java
  • RentalServiceBean.java
  • RentalServiceRemote.java
  • ServiceDelegateBase.java
  • RentalServiceDelegate.java
  • RentalServiceBase.java
  • RentalServiceException.java
  • ejb-jar.xml
  • jboss.xml

The two important files to look at are RentalServiceBean.java and ejb-jar.xml. The RentalServiceBean.java injects the resource using the @Resource annotation and specifies the name property corresponding to the env-entry-name in ejb-jar.xml for the environment entry.

Notice here that the Person entity has 2 lifecycle callbacks. We have modeled then using stereotypes on instance scoped operations. Click on lifecycle callbacks to find out more.

Next

To find out how service delegates work, click here.