Domanda

I have a Java EE project that instantiates an instance of an object from a dependent project. That dependent project now wants to create and inject its own EJB into one of its classes. The dependent project doesn't reference the parent project, but should run inside of the parent project's container.

When I deploy the parent project, I see the message in the log that my service bean has been discovered along with the rest of the beans in the parent project:

2014-04-28T21:30:56.240-0400|INFO: EJB5181:Portable JNDI names for EJB DependentProjectServiceBean etc.

But then when I try to inject that bean in the dependent project using the @EJB annotation, nothing happens (it's just left as null).

I've been trying to figure out how to configure these projects properly so that I can use EJB and JPA in the dependent project, but I feel like I don't know the right search terms, since none of the things I find seem like they address this issue. I'm including the same Maven dependencies in the dependent project as I do in the parent project, and Eclipse seems cool with the annotations; they just don't seem to DO anything once deployed.

How do I enable EJB and JPA in my dependency project?

Edit: Here is a rough outline of the code I'm working with.

Parent

public class ParentProjectClass {
    @EJB
    ParentServiceBean psb;

    public void cat(){
        psb.doStuff(); //no problems
        SomeDependentClass sdc = new SomeDependentClass();
        sdc.baz();
    }
}

Dependent Project

public class SomeDependentClass {
    private AnotherDependentClass adc;

    public SomeDependentClass(){
        adc = new AnotherDependentClass();
    }
    public void baz(){
        adc.foo();
    }
}

public class AnotherDependentClass {
    @EJB
    DependentProjectServiceBean dpsb;

    foo(){
        dpsb.bar(); //NULL POINTER EXCEPTION
    }
}

@Stateless
public class DependentProjectServiceBean {
    public void bar(){  //bar   }
}
È stato utile?

Soluzione

you cannot inject EJB using @EJB annotation inside a simple class (that is not itself an EJB or JSF ManagedBean). The simplest solution is to convert your Dependent Project to EJB Project. If you cannot do so , you can try to inject it using manual JNDI lookup, here is code sample :

// Retrieve the initial context for JNDI.
Context context = new InitialContext();
// Retrieve the home interface using a JNDI lookup 
dpsb = (DependentProjectServiceBean) context.lookup("java:comp/env/ejb/DependentProjectServiceBean");

Replace java:comp/env/ejb/DependentProjectServiceBean by your JNDI path

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top