Quick Look at EJB 3.1 on JBoss AS 6

DZone 's Guide to

Quick Look at EJB 3.1 on JBoss AS 6

· Java Zone ·
Free Resource

Now that JBoss AS 6.0 has been released with full support for the Java EE 6 Web Profile, lets take a quick look at some of the new features of EJB 3.1 that are available.

Three of the main features of EJB 3.1 are:

  1. EJB’s can be deployed as part of a WAR file and do not need to be is a separate EJB JAR file.
  2. EJB’s can be developed with no business interface.
  3. EJB’s can be deployed as Singletons.

Let’s take a look at each of these in turn.

1. EJB’s can be deployed as part of a WAR file.

Not much to look at here I’m afraid! The thing to notice is that now with NetBeans (I’m using NB 7 Beta), you can create a simple Java EE 6 Web Project and create EJB’s within the web project.  If you are developing a web project, there is no longer any need for an additional JAR to deploy any EJB’s that you use.

2. EJB’s can be developed with no Business Interface

Creating an EJB without a Business Interface is as simple as creating a POJO, which in fact the EJB is!  In it’s simplest form, an EJB could be defined as:

package com.davidsalter.ejb31test;
import javax.ejb.Stateless;
public class HelloSessionBean {
    public String sayHello() {
        return "Hi there !!";

The only difference here from a POJO is that the class is annotated with @Stateless, declaring it as a Stateless Session Bean.

When the WAR file containing this EJB is deployed to JBoss, we get a message on the JBoss console stating that the EJB has been deployed:

21:08:29,653 INFO  [org.jboss.ejb3.nointerface.impl.jndi.AbstractNoInterfaceViewBinder] Binding the following entry in Global JNDI for bean:HelloSessionBean

        HelloSessionBean/no-interface -> EJB3.1 no-interface view

Invoking an EJB within a web project is similarly straightforward.  The EJB can be injected into a Servlet using the @EJB annotation.

public class HelloServlet extends HttpServlet {
    HelloSessionBean helloSessionBean;

    PrintWriter out = response.getWriter();

3. EJB’s can be deployed as Singletons

Finally, EJB’s can easily be declared as Singletons via use of the @Singleton annotation. Used in conjunction with the @PostConstruct and @PreDestroy annotations, any initialization and clean up can easily be performed on the Singleton. The method annotated with @PostConstruct is invoked after the singleton is first constructed, for example when a calling servlet is first loaded or when a JSF backing bean that calls the singleton is first instantiated. The method annotated with @PreDestroy is called immediately before the Singleton is destroyed and is therefore a perfect place to perform any tidy up operations.

Defining Singletons in this way can be a useful technique for storing data that may be required across multiple clients of an application.

package com.davidsalter.ejb31test;

import java.util.Date;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;

public class SingletonSessionBean {

private String creationDate;

public void init() {
System.out.println("Initialize singleton");
creationDate = new Date().toString();

public void shutdown() {
System.out.println("Shutting down singleton");

public String getDate() {
return creationDate;

Again, invoking the Singleton is as easy as invoking the Session Bean we defined earlier.

SingletonSessionBean singletonSessionBean;

The sample code and application used for this post are available from my Subversion repository on Google Code at:


To run the sample application, check the code out from subversion, build and deploy in NetBeans to JBoss AS 6 and access http://localhost:8080/EJB31Test/HelloServlet

From http://davidsalter.co.uk/blog/?p=476


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}