Monday, August 16, 2010

xdoclet and AbstractStatelessSessionBean

I am working on a very old project to fix some bugs. It is using ejb 2.1 generated by xdoclet and spring 2.0.2.

To initial spring context from ejb, here is the code and the configuration

public abstract class CustomerServiceBean extends AbstractStatelessSessionBean implements SessionBean {
...
protected void onEjbCreate() throws CreateException {
customerService = (CustomerService) getBeanFactory().getBean("customerService");
}
...
}

ejb-jar.xml

<session > 
         <ejb-name>CustomerServiceSessionBean</ejb-name> 
         <home>com.telus.smis.authentication.ejb.CustomerServiceSessionBeanHome</home>
         <remote>com.telus.smis.authentication.ejb.CustomerServiceSessionBean</remote>
         <local-home>com.telus.smis.authentication.ejb.CustomerServiceSessionBeanLocalHome</local-home>
         <local>com.telus.smis.authentication.ejb.CustomerServiceSessionBeanLocal</local>
         <ejb-class>com.telus.smis.authentication.ejb.CustomerServiceSessionBeanSession</ejb-class>
         <session-type>Stateless</session-type>
         <transaction-type>Container</transaction-type> 
         <env-entry>
            <env-entry-name>ejb/BeanFactoryPath</env-entry-name>
            <env-entry-type>java.lang.String</env-entry-type>
            <env-entry-value><![CDATA[/smis-services-ejb-spring.xml]]></env-entry-value>
         </env-entry> 
</session>


The env-entry point to the spring configuration file to load the spring context.



Here is the xdoclet tags to generate the configuration above



/** 
* …
*
* @ejb.env-entry name="ejb/BeanFactoryPath"
*  type="java.lang.String"
*  value="/smis-services-ejb-spring.xml"
*/


Then we run the build script, deploy it and run the test. But “NullPointException” and the onEjbCreate() method is never executed. Why.. After look into the code generated by xdoclet, it generates an empty ejbCreate() which makes the onEjbCreate() method never be executed.



To avoid xdoclet doing that, explicitly create ejbCreate() method and it fixes this issue.



/**
     * Don't remove this method, otherwise xdoclet generates a empty ejbCreate method
     * which makes onEjbCreate never executed
     *
     * @ejb.create-method
     */
    public void ejbCreate() throws CreateException {
        super.ejbCreate();
    }

No comments:

Post a Comment