Yagami Light
Yagami Light

Reputation: 1796

Can't use EJB in class that use interface

I am working in a JavaEE Project and i use an interface to implement method but when i want to use an EJB it say that my EJB is null
Interface.java

public interface Interface {  public void methodTest ();  }

SubInterface.java

public class SubInterface implements Interface {
@EJB
private myEJB myejb
@Override
public void methodTest(){
if( myejb == null ){
System.out.println("He is null");
}
} 
}

myEJB.java

@Stateless
public class myEJB extends SomeAbstractFacade {
@PersistanceContext
private EntityManager em ; 

....

}

UserController.java

@ManagedBean
@ViewScoped
public class UserController extends SomeAbstractController implements         Seializable{
private interface myInterface ;

public void method (){
myInterface  = new SubInterface();
myInterface.methodTest();

}

}

when i execute methodTest we can read in the output he is null.
thank you

Upvotes: 1

Views: 472

Answers (2)

Yagami Light
Yagami Light

Reputation: 1796

Dependency injection is supported only on managed classes such as Session Beans or Interceptors and not on regular POJO. Hence you cannot use injection on helper classes. we can make a simple example Java Class is classified in a level and EJB is classified in a superior level that mean that they can't communicate easily , the solution is to use lookup that mean to go search for the EJB with his own path.

Upvotes: 0

Bob Brinks
Bob Brinks

Reputation: 1392

You will need to inject your interface instance. When you use 'new' all the annotations (@EJB etc.) will be ignored as the dependency manager won't know about the creation of that instance.

So in your managed bean annotate the private interface field and the ejb will be injected for you.

@ManagedBean
@ViewScoped
public class UserController extends SomeAbstractController implements         Seializable{

   @Inject
   private interface myInterface ;

   public void method (){
      myInterface.methodTest();

   }

}

Upvotes: 3

Related Questions