EJB 3.1 new features

Enterprise Java Beans version 3.1 is part of Java EE 1.6 specification. In this tutorial we will cover the major highlights available in this specification version which you can run on WildFly application server and JBoss EAP 6/7.

Feature #1 No interface view

EJB Interfaces are no longer needed. You need just to add a @Stateless or @Stateful annotation and that’s all. This avoid the burden of replicating some/all methods from the Bean class to the interface view.
Here’s an example:
package ejb;
 
import javax.ejb.Stateless;
 
@Stateless

public class NoInterfaceBean
{
   public String sayHello(String name)
   {
      return "Hello " + name;
   }
 
}
When you deploy this EJB, the container recognizes that is a no-interface bean and creates a Local interface automatically for you.

This of course means that if you need to expose your bean with remote interface, you need to provide another Bean which acts as Session Facade to your no-interface EJB.
As a consequence of this, the use case for no-interface EJB are mainly for application which are fronted by the Web tier, which in turn communicates with the EJB (local) tier.

Feature #2 You can pack EJB in Web application Archives (.war)

The most interesting news of this new release is the ability to deploy your EJB along with your Web application. This means that :
  • It will be easier to pack and deploy your application archives because you don’t need to create an Enterprise Application Archive (.ear) for applications made up just of Servlet and EJBs
  • Also, you don’t need to worry about separating common classes in a .jar file which will be deployed in your Enterprise Archive (And referenced as a Java Module in your application.xml)
The following Servlet has our no-interface Bean injected and deployed in the same Web Archive:

public class ServletController extends HttpServlet {
    @EJB(mappedName="NoInterfaceBean/no-interface")
    NoInterfaceBean ejb;
      
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
       

        try {
            out.println(ejb.sayHello("Frank"));         
            out.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}    
Notice how little coding we have saved from earlier EJB specifications! ( No interfaces needed, no Service Locator class, no need to create separate archives for application…)

You can pack your EJBs in two ways:
1) Simply add the EJBs in the classes folder, like a normal Bean/Servlet:  

WebApp.war
¦   index.jsp
¦
+—WEB-INF
¦   web.xml
¦
+—classes
+—ejb
¦       NoInterfaceBean.class
¦
+—servlet
ServletController.class

2) Pack your EJB classes in an archive and include it in the WEB-INF/lib folder: 

WebApp.war
¦   index.jsp
¦
+—WEB-INF
¦   web.xml
¦
+—classes
¦   ¦
¦   +—servlet
¦       ServletController.class
+—lib
¦
+—ejb-classes.jar  

That’s all for now. To learn more about EJB 3, we recommend checking the following tutorial: EJB 3 tutorial for Enterprise developers

Reference: EJB 3.1 specification