This discussion is archived
8 Replies Latest reply: Dec 4, 2012 4:26 AM by 977825 RSS

Initialize @EJB fields

977825 Newbie
Currently Being Moderated
I am newbie in EJB.

I have a class with following fields

*@Stateless*
*@TransactionManagement(TransactionManagementType.CONTAINER)*
*@TransactionAttribute(TransactionAttributeType.REQUIRED)*
*public class MyServiceFacadeBean implements MyServiceFacadeLocal, MyServiceFacadeRemote {*

*@EJB(name = "ejb/CatalogService")*
private CatalogService catalogService = null;
*....*
*}*

I have instantiated the object while injecting it implementation in Spring IOC:

*<bean id="contestServiceFacade" class="my.company.service.facade.contest.ejb.MyServiceFacadeBean">*
*</bean>*

but after invoke it methods in my controllers I get NullPointerException on catalogService field. So how should be my facade correctly instantiated?
  • 1. Re: Initialize @EJB fields
    977825 Newbie
    Currently Being Moderated
    Please help!

    Edited by: 974822 on Dec 4, 2012 1:44 AM
  • 2. Re: Initialize @EJB fields
    gimbal2 Guru
    Currently Being Moderated
    The EJB annotation will be processed if the EJB is managed through the JEE container. Likely the way you configure Spring now, it is manually constructing an instance of the EJB class which is no good.

    You should check the Spring documentation how to properly integrate EJB technology into the Spring framework. You likely need some piece of configuration.
  • 3. Re: Initialize @EJB fields
    977825 Newbie
    Currently Being Moderated
    Thanks for your answer!
    I have tried this already but was failed.
    I use JBoss and this is mine configuration bean in my applicationContext.sml:

    *<bean id="myServiceFacade" class="org.springframework.jndi.JndiObjectFactoryBean">*
    *<property name="jndiName" value="MyServiceFacadeBean/remote"/>*
    *<property name="cache" value="true"/>*
    *<property name="lookupOnStartup" value="true"/>*
    *<property name="jndiEnvironment">*
    *<props>*
    *<prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory*
    *</prop>*
    *<prop key="java.naming.provider.url">localhost:1599</prop>*
    *<prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces*
    *</prop>*
    *</props>*
    *</property>*
    *<property name="proxyInterface" value="my.company.service.facade.contest.MyServiceFacade"/>*
    *</bean>*

    but this is failed while deploying:

    ++Error creating bean with name 'myServiceFacade' defined in ServletContext resource [WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: MyServiceFacadeBean/remote+

    What can be the problem?

    Edited by: 974822 on Dec 4, 2012 1:57 AM

    Edited by: 974822 on Dec 4, 2012 1:58 AM

    Edited by: 974822 on Dec 4, 2012 2:23 AM
  • 4. Re: Initialize @EJB fields
    Kayaman Guru
    Currently Being Moderated
    974822 wrote:
    What can be the problem?
    Didn't you read the error message? Name not found. Use the correct name.
  • 5. Re: Initialize @EJB fields
    977825 Newbie
    Currently Being Moderated
    OK, sorry I just misspelled i my stack trace. I am sure my names are correct.
    The question is: how the bean can be bound to JNDI port. I mean is there some specific JBoss settings should be made?
  • 6. Re: Initialize @EJB fields
    Kayaman Guru
    Currently Being Moderated
    974822 wrote:
    OK, sorry I just misspelled i my stack trace. I am sure my names are correct.
    Well, not according to the lookup. It can't find the name you're giving, so it sounds like you're looking in the wrong place.
    The question is: how the bean can be bound to JNDI port. I mean is there some specific JBoss settings should be made?
    Check your JBoss version and read the part about JNDI binding. There was at some irregularity, but I think that didn't affect EJBs.
  • 7. Re: Initialize @EJB fields
    gimbal2 Guru
    Currently Being Moderated
    Kayaman wrote:
    974822 wrote:
    OK, sorry I just misspelled i my stack trace. I am sure my names are correct.
    Well, not according to the lookup. It can't find the name you're giving, so it sounds like you're looking in the wrong place.
    The question is: how the bean can be bound to JNDI port. I mean is there some specific JBoss settings should be made?
    Check your JBoss version and read the part about JNDI binding. There was at some irregularity, but I think that didn't affect EJBs.
    Standard JBoss JNDI naming prior to JEE6 was (if you don't override it):

    earname/EjbBeanName/localremote

    or when you don't have an ear:

    EjbBeanName/localremote

    And starting from JBoss 6 its the standard global JNDI naming scheme as defined by the JEE 6 spec. I think the old way also still works, but why bother when you have a standard.
  • 8. Re: Initialize @EJB fields
    977825 Newbie
    Currently Being Moderated
    Thanks for your answer but unfortunately our company has mandatory rules with using JBoss 5

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points