View Full Version : NullPointerException in getBeanFactory() (EJB + Spring)
abhilash
Oct 9th, 2004, 05:25 AM
Hi All,
I am trying to follow the Spring documentation and have defined a bean as below. I use Xdoclet to generate the session bean and Home and Remote interfaces. I am having a problem where getBeanFactory() always raises a NullPointerException. Looking at the AbstractEnterpriseBean code, it looks like Spring can't find the configuration xml file. What am I doing wrong? Any help is greatly appreciated.
thanks
Abhilash
/**
* @ejb.bean name="Login"
* display-name="Name for Login"
* description="Description for Login"
* jndi-name="ejb/Login"
* type="Stateless"
* view-type="remote"
*
* @ejb.home extends="javax.ejb.EJBHome"
*
* @ejb.interface extends="javax.ejb.EJBObject"
*
* @ejb.env-entry name="ejb/BeanFactoryPath" type="java.lang.String" value="/beanRefContext.xml"
*
*/
public class LoginBean extends AbstractStatelessSessionBean implements SessionBean, LoginBI {
private LoginBI myImpl = null;
protected void onEjbCreate() throws CreateException {
myImpl = (LoginBI) getBeanFactory().getBean(RNCSApplicationContext.LO GIN_BEAN);
}
/**
* @see com.united.rncs.interfaces.LoginBI#login(java.lang .String, java.lang.String)
*
* @ejb.interface-method
*/
public RncsUser login(String user, String password) {
return myImpl.login(user, password);
}
}
Rod Johnson
Oct 11th, 2004, 11:44 AM
Have you checked that the XML file is included in your EJB JAR file? Have you checked the generated ejb-jar.xml to make sure that the environment key appears correctly there? What exact log messages lead you to believe Spring can't find the file?
abhilash
Oct 12th, 2004, 08:46 AM
Thanks for the response. We checked both the env-entry and the config file in the EJB Jar file. Below are the details.
Thanks for the help
Abhilash
In ejb-jar.xml file :
<env-entry>
<env-entry-name>ejb/BeanFactoryPath</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value><![CDATA[/beanRefContext.xml]]></env-entry-value>
</env-entry>
jar tvf <ejb jar file> :
1281 Tue Oct 12 18:31:56 IST 2004 com/united/rncs/util/PasswordEncryptor.class
2912 Tue Oct 12 18:46:04 IST 2004 META-INF/ejb-jar.xml
774 Tue Oct 12 18:46:04 IST 2004 META-INF/jboss.xml
7645 Fri Oct 08 12:08:24 IST 2004 SpringApplicationContext.xml
7224 Fri Oct 08 12:08:40 IST 2004 beanRefContext.xml
0 Thu Sep 30 11:15:54 IST 2004 lib/
210908 Tue Aug 10 15:04:26 IST 2004 lib/postgres-7.4.213.jdbc3.jar
982921 Tue Aug 10 15:04:26 IST 2004 lib/spring.jar
300056 Wed Aug 11 11:45:38 IST 2004 lib/cglib-full-2.0.1.jar
165119 Wed Aug 11 11:45:38 IST 2004 lib/commons-collections-2.1.jar
63980 Wed Aug 11 11:45:38 IST 2004 lib/commons-lang-1.0.1.jar
31605 Wed Aug 11 11:45:38 IST 2004 lib/commons-logging-1.0.3.jar
486522 Wed Aug 11 11:45:38 IST 2004 lib/dom4j-1.4.jar
42069 Wed Aug 11 11:45:38 IST 2004 lib/ehcache-0.7.jar
925307 Wed Aug 11 11:45:38 IST 2004 lib/hibernate2-1-4.jar
262152 Wed Aug 11 11:45:38 IST 2004 lib/hsqldb.jar
6727 Wed Aug 11 11:45:38 IST 2004 lib/jdbc2_0-stdext.jar
8812 Wed Aug 11 11:45:38 IST 2004 lib/jta.jar
13091 Wed Aug 11 11:45:38 IST 2004 lib/odmg-3.0.jar
Here is the stack trace on the server side:
java.lang.NullPointerException
at org.springframework.ejb.support.AbstractEnterprise Bean.getBeanFactory(AbstractEnterpriseBean.java:13 8)
at com.united.rncs.impl.ejb.LoginBean.login(LoginBean .java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.ejb.StatelessSessionContainer$ContainerI nterceptor.invoke(StatelessSessionContainer.java:6 83)
Andreas Senft
Oct 20th, 2004, 06:07 AM
This is strange, since the login() Method as you provided it does not access the BeanFactory at all.
I mean, if you call a business method on the EJB, that bean must have been created before (involving a call to onEjbCreate). If the configuration could not be found, an exception should be raised there.
Maybe you could add some log output in your onEjbCreate method and your login method to gather some more details.
As a side note: You did non override the ejbCreate() Method, did you (maybe through XDoclet generation)? That would actually prevent the initialization of the bean factory.
Regards,
Andreas
abhilash
Oct 20th, 2004, 08:22 AM
Hi Andreas,
Thanks for the reply. Yes it does seem strange.. onEJBCreate never gets called (we placed a breakpoint, println etc. there). The LoginBean extends StatelessSessionBean whose code is as follows (the idea was we could have common code like setBeabFactoryLocator there, but eventually we commented out everything there) :
We will try to post the barebones BeanClass, spring xml file etc. here. Meanwhile if you have some code that you can share it'll help us trenmendously.
thanks
Abhilash
---------------
package com.united.rncs.impl.ejb;
import javax.ejb.CreateException;
import javax.ejb.SessionContext;
import org.springframework.beans.factory.access.Singleton BeanFactoryLocator;
import org.springframework.ejb.support.AbstractStatelessS essionBean;
public abstract class StatelessSessionBean extends AbstractStatelessSessionBean {
protected void onEjbCreate() throws CreateException {
}
public void setSessionContext(SessionContext sessionContext) {
super.setSessionContext(sessionContext);
// setBeanFactoryLocator(SingletonBeanFactoryLocator. getInstance("classpath:test.dat"));
// setBeanFactoryLocatorKey(ServicesConstants.PRIMARY _CONTEXT_ID);
}
}
Andreas Senft
Oct 21st, 2004, 12:30 AM
Hi Abhilash,
I have no special code, which could shed light on that issue.
When using the EJB-base types, I do it the simple way like this:
1. extend from AbstractStatelessSessionBean
2. provide the name of the config file in the ejb-jar.xml in the default environment entry
3. Ensure the availability of the config file in the root of the ejb-jar
4. Access my handler bean from within the onEjbCreate method, using the inherited getBeanFactory() method
Maybe you could try to build a minimal example which reproduces the behaviour.
The only guess I can make now is that somehow the ejbCreate() method is overriden, thus onEjbCreate() never gets called.
I have not worked so far with the combination Spring/ XDoclet. So I cannot tell if there might be something generated by XDoclet which should not be there.
Regards,
Andreas
abhilash
Oct 28th, 2004, 05:11 AM
Hi Andreas, thanks for your suggestion. We found the solution to our problem. XDoclet generates an ejbCreate in LoginSession which was not doing anything. In our LoginBean we implemented ejbCreate and called super.ejbCreate() and that fixed the problem.
thanks
Abhilash
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.