Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using EJB injection in a POJO

I know that injection using the @EJB annotation is only possible in an EJB class, a servlet, or a JSF managed bean, but in the same time I need to have an instance of a some injected business interface in a POJO class, so I thought of doing the following:

in my JSF managed bean

@EJB BusinessInterfaceLocal businessInterface;

private void someMethod(){
    PojoInterface pojo = new PojoClass(this.businessInterface);
}

and in my POJO class I have this constructor

BusinessInterfaceLocal businessInterface;    

public PojoClass(BusinessInterfaceLocal businessInterface){
   this.businessInterface = businessInterface;

   //The following throws a Null Pointer Exception
   this.businessInterface.someMethodCall();
}

Shouldn't the above work correctly? but it doesn't, the businessInterface object at the PojoClass is evaluated to null, and thus throwing a null pointer exception.

I was hoping if anyone could point me out, on what I'm doing wrong.

Thanks in advance.

like image 280
Sabry Shawally Avatar asked Nov 06 '12 11:11

Sabry Shawally


2 Answers

verification

Is it possible you create the PojoClass before the EJB gets injected. By that I mean, where do you invoke "someMethod"? Is it in the constructor of the managed bean? A variable simply does not lose its referenced value.

You said you could see the BusinessInterfaceLocalbean isn't null in the Managed bean, can you verify you create the Pojo after that check?

Alternative solutions:

solution 1 You can use the POJO as a stateless bean, I don't see any problems in doing that, unless of course you are trying to use the POJO outside of your EE container, which is, by the looks of it not the case.

Making the POJO stateless would make you able to inject the EJB.

solution 2 OR a JNDI lookup, implemented as followed:

@Stateless(name="myEJB")
public class MyEJB {

  public void ejbMethod() {
  // business logic
  }

}

public class TestEJB {

  public static void main() {
  MyEJB ejbRef = (MyEJB) new InitialContext().lookup("java:comp/env/myEJB");
  ejbRef.ejbMethod();
  }
}
like image 50
Qkyrie Avatar answered Oct 28 '22 23:10

Qkyrie


With the code you show above, the only way that a NullPointerException could be thrown at the line indicated is if the businessInterface field in the managed bean is null. References cannot mysteriously become null when passed from one object to another, and the PojoClass doesn't do anything which would cause the variable to become null. I suggest you do some debugging or logging to definitively determine the value of the field in the managed bean at the point at which the constructor is called. If it is null, then the problem is with injection into the bean, and is not anything to do with the POJO, and you should fix that.

If the exception being thrown is actually from deeper than the line you show, then this could be a problem with the use of an EJB proxy in the wrong context. As you may know, a reference to an EJB is typically not a reference to the EJB itself, but rather to some sort of proxy which passes method calls on to the EJB; the proxy exists so that the container can step in and do things like start transactions, check authorization, and so on. The proxy may need to call on certain contextual resources to do its work which are available when the EJB is accessed from the managed bean but not, for some non-obvious and twisted reason, from the POJO. The unavailability of those resources could lead to a NullPointerException. Now, i think it is highly unlikely that simply passing a reference from a managed bean to a POJO would get you into that situation; this would only happen if you did something like accessing the managed bean from a different thread. So, it's probably not this!

like image 1
Tom Anderson Avatar answered Oct 29 '22 00:10

Tom Anderson