Adam Bien's Weblog

Simplest Possible, Annotation-Less EJB 3.1

The Bean is just a POJO - without any dependencies to libraries etc: 


public class MessageFacade {
public String hello(){
return "Working without annotations";
}
}

You will need to specify everything in XML:

<ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee"
         version = "3.1"
         xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
   <enterprise-beans>
    <session>
        <ejb-name>MessageFacade</ejb-name>
        <local-bean/>
        <ejb-class>com.abien.patterns.business.annotationless.boundary.MessageFacade</ejb-class>
        <session-type>Stateless</session-type>
    </session>
   </enterprise-beans>
</ejb-jar>

Now you can use already the no-interface view Bean and inject it into a servlet (JSF backing beans, interceptors, other beans):


public class FrontController extends HttpServlet {
@EJB
MessageFacade facade;
//...

With XML (ejb-jar.xml) you could develop the whole application without even having the EJB 3.X API in your classpath...

This sample was deployed as a WAR. The ejb-jar.xml has to be placed into WEB-INF.

The project (AnnotationLessEJB31) was pushed into: http://kenai.com/projects/javaee-patterns/. The code was tested with Netbeans 6.7 and Glassfish v3 Prelude. Netbeans 6.7, however,  does not support EJB 3.1 directly. You will have to copy the ejb-jar.xml from src/java/conf into build/web/WEB-INF/ manually or change the ANT-script.

[Its one of the introductory samples (Chapter 1) from the "Real World Java EE Patterns" book]


NEW workshop: Microservices with Java EE 7 and Java 8, January 26th, 2015, Airport Munich

A book about rethinking Java EE Patterns

Comments:

Hi,
Thank you, It was really helpful.

Posted by Mehdi on July 20, 2011 at 12:10 PM CEST #

Hi Adam,

remember that it's better to add the attribute metadata-complete="false" to the element <ejb-jar>.

Just with this it is possible to mix XML based and annotation based declaration of a bean.

It's a pity that it's still not possible to define a MDB as an endpoint for a (inbound) resource adapter without the neccessary XML deployment descriptor :-(

Looking forward to Java EE 7/8 to completely avoid XML's at all (even beans.xml as well as ra.xml)...

Regards and enjoy SF+JavaOne!
Robert

Posted by Robert on October 01, 2011 at 05:11 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License