Jetty and Spring EJB3 (Pitchfork)

Pitchfork is an add-on for the Spring Framework performing JSR-250 dependency injection, annotation processing and EJB 3.0-style interception, developed jointly by Interface21 and BEA Systems.

It is simple to use in conjunction with Jetty.

At the time of writing, the integration with Pitchfork is checked in to jetty svn trunk, but will be released with 6.1.0 final. Until then, it will be first necessary to checkout and build jetty.

Once jetty is built, make a new directory: $jetty.home/lib/ext/spring. Copy the jar from $jetty.home/extras/spring/target into it. Also copy all of the jars from the Pitchfork download into it.

Developing a webapp using EJB3

The JEE tutorial from Sun will take you through how to create EJB3 classes.

Here's an extremely simple example of a stateless session bean showing some of the JEE-style annotations:

package com.acme;

import java.util.Date;
import javax.annotation.PostConstruct;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;

public class EchoBean implements Echo
    public String echo()
        return "Hello "+new Date();

    public void init()
        System.err.println("EchoBean init called");

We would declare this class in a META-INF/spring-ejb-jar.xml file. That file can reside in a jar if you're packaging your ejbs into jars, or you can make a WEB-INF/classes/META-INF/ directory if your ejbs are just part of your webapp.

The spring-ejb-jar.xml file for the above bean looks like:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:xsi="" xmlns:aop=""

  <bean id="EchoBean" class="com.acme.EchoBean"></bean>


Next, you can make some classes that use @EJB notation to inject references to your beans. Here's another stupidly simple class that uses the com.acme.EchoBean ejb:

package com.acme;

import javax.ejb.EJB;

public class EchoTest
  @EJB private static EchoBean echoBean;

  public static String echo ()
      return echoBean.echo();

In order to have Pitchfork notice that the com.acme.EchoTest class wants a reference to the com.acme.EchoBean ejb, we declare it in the Spring WEB-INF/applicationContext.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="no"

  <bean id="echoTest" class="com.acme.EchoTest"/>

  <bean id="jeeEjbPostProcessor" class="org.springframework.jee.ejb.config.JeeEjbBeanFactoryPostProcessor"/>

Notice the very important second line with id jeeEjbPostProcessor. This line tells Pitchfork to look at all other <bean> declarations and perform any necessary resource injections on them.

To complete the webapp, here's a trivial jsp:

<%@ page import="com.acme.*" %>
<h1>Echo EJB</h1>

   String str = EchoTest.echo();

When you deploy it and surf to it, you'll see something like this:

Echo EJB

Hello Thu Dec 07 12:14:16 CET 2006

A maven project that builds a deployble webapp for the example we've been looking at is attached to this page.