Skip to content

How to use @EJB annotation

Michael O'Cleirigh edited this page Apr 12, 2011 · 1 revision

Stateless example

NOTE: The source code on this page is only Wicket 1.4-compatible.

  1. Create your EJB, i.e.
@Local
public interface ContactService {
  //.....
}

@Stateless
public class ContactServiceImplementation implements ContactService {
  //....
}
  1. Package your EJB in a JAR file.
  2. Declare your EJB in the web.xml descriptor of the webapplication, i.e.
    <ejb-local-ref>
    	<ejb-ref-name>ejb/contacts</ejb-ref-name>
    	<ejb-ref-type>Session</ejb-ref-type>
    	<local>wicket.javaee.service.ContactService</local>
    </ejb-local-ref>
  1. Create your wicket page using the @EJB annotation to reference the developed EJB, i.e.
public class ListContacts extends WebPage {

  @EJB(name="ejb/contacts") private ContactService contactService;

  public ListContacts() {
    List<Contact> contacts = contactService.getContacts();
    //....
  1. Add the line
addComponentInstantiationListener(new JavaEEComponentInjector(this));

inside the init() method of your Wicket WebApplication, like in the example:

public class WicketJavaEEApplication extends WebApplication {

   protected void init() {
      addComponentInstantiationListener(new JavaEEComponentInjector(this));
   }
}
  1. Package your web application in a WAR file.
  2. Package the EJB module and the webapplication in an EAR file. Stateful EJB's

By default you're using always the same EJB instance (it's cached), but if you want to cache your EJB for Wicket Sessions, then use the following code in your Wicket component:

  @EJB(name="ejb/contacts", description="stateful") private ContactService contactService;

So whenever you want to use stateful beans in your component, you need to put the stateful description in the @EJB, so the injector could separate them from stateless beans. Note: for no-interface view stateful session beans you don't need to use the description attribute. Support for Global JNDI names

Since Java EE Inject 1.4.10, you can use the Java EE 6 based Global JNDI names, such as:

java:global[/<app-name>]/<module-name>/<bean-name>[!<fully-qualified-interface-name>]
java:app/<module-name>/<bean-name>[!<fully-qualified-interface-name>]
java:module/<bean-name>[!<fully-qualified-interface-name>]

The java:global namespace can be used globally (for example accessing other applications remote beans), the java:app namespace is for application-scope JNDI access and the java:module namespace is for modules, which contains the lookupable element.

The app-name can be configured in the EAR files application.xml ( tag), and the module name can be configured with the tags in both web.xml and ejb-jar.xml. These elements were not available in previous XSD's, so you need to check, whether your XML uses the latest schema. If you want to use Global JNDI names, then in your WicketApplication init method use one of the following codes:

addComponentInstantiationListener(new JavaEEComponentInjector(this,
        new GlobalJndiNamingStrategy(APP_NAME, EJB_MODULE_NAME)));
addComponentInstantiationListener(new JavaEEComponentInjector(this,
        new AppJndiNamingStrategy(EJB_MODULE_NAME)));
addComponentInstantiationListener(new JavaEEComponentInjector(this,
        new ModuleJndiNamingStrategy()));
Clone this wiki locally