EJB 3.0 and "Legacy" POJOs - deploying Swing's TableModel as Session Bean 📎
Some of the reactions to my post "Are Plain Old WebContainers still appropriate in Java EE 5?" implied, that it could be hard to integrate "legacy" POJOs with plain EJB 3 technology. To clarify this issue, I tried to deploy a really "legacy" POJO as a Stateless / Stateful Session Bean. ...I tried to deploy an already existing class - the javax.swing.table.DefaultTableModel as a Session Bean (!!!), without changing the source code. The deployment (to Glassfish v2), as well as test worked (!!!). It's interesting, but many developers think, that EJB 3 can only works with annotation - or bloated deployment descriptors...
To clarify this point, I declared the DefaultTableModel as a Stateless Session Bean with the following Deployment Descriptor:
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee"
version = "3.0"
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_0.xsd">
<enterprise-beans>
<session>
<ejb-name>Table</ejb-name>
<business-local>javax.swing.table.TableModel</business-local>
<ejb-class>javax.swing.table.DefaultTableModel</ejb-class>
<session-type>Stateless</session-type>
</session>
</enterprise-beans>
</ejb-jar>
The ejb-jar archive is empty in this case - it only contains the deployment descriptor. However the declaration of this legacy POJO in xml allows it injection to an "usual", annotated session bean:
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.swing.table.TableModel;
@Stateless
@WebService
public class LegacyTestBean implements LegacyTest {
@EJB(beanName="Table")
private TableModel tableModel;
public String accessTable(){
return "Row count: " + tableModel.getRowCount();
}
}
The attribute "beanName" of the @EJB annotation refers to the name of the declared bean. As you can see, with just few lines of XML code it is possible to deploy almost every class as a session bean. The requirements are only:
- The existence of the default constructor
- The business interface is required
EJB 3 are really interesting - even for very small and trivial projects. I checked in the whole project into p4j5. The projects name is "LegacyPojo" - it was tested with Glassfish V2 and Netbeans 6b1.
[See also page 37 in the "Real World Java EE Patterns - Rethinking Best Practices" book]