1

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

Yagami Light
  • 1,756
  • 4
  • 19
  • 39
  • How are you instantiating the class that has the myejb field? It needs to be instantiated by the dependency injection framework of your EE provider otherwise EJB's will not be injected. Also please try to start ClassNames with capital letters (CamelCase). The rest of the java community will appreciate it. – Bob Brinks Aug 09 '16 at 13:40
  • i will update my question to add myEJB class – Yagami Light Aug 09 '16 at 13:42
  • Can you show how you instantiate the SubInteface class? – Bob Brinks Aug 09 '16 at 14:00
  • I updated my question – Yagami Light Aug 09 '16 at 14:06
  • Not what i meant. Can you show where you have an instance (variable) of the SubInterface type? And also show how you create that instance. – Bob Brinks Aug 09 '16 at 14:12
  • I updated my code thank you for your time – Yagami Light Aug 09 '16 at 14:16
  • Next time please post an [mcve] directly. It should not be needed to ask al these questions. Waste of your waiting time and also the time of the people that try to help. And please format your code a little better. – Kukeltje Aug 11 '16 at 18:04

2 Answers2

3

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();

   }

}
Bob Brinks
  • 1,372
  • 1
  • 10
  • 19
  • doesn't work, i don't understand why the problem is in the SubInterface it's very weird if you can use EJB in a java class – Yagami Light Aug 09 '16 at 14:27
  • 2
    The problem is that you're using 'new'. When you use the 'new' keyword no EJB's will be created. – Bob Brinks Aug 09 '16 at 14:31
  • When you need to call it for the first time you use new this why i use new SubInterface – Yagami Light Aug 09 '16 at 14:34
  • 2
    No, the framework that does your dependency injection should create a 'new SubInterface' instance for you and inject it. When you call 'new' yourself you circumvent the dependency injection and all EJB's will be NULL. – Bob Brinks Aug 09 '16 at 14:36
  • even if you have 3 or 4 different subInterface ?!? – Yagami Light Aug 09 '16 at 14:38
  • Yes. Then you should add qualifiers (annotations) to specify what implementation (subinterface) you would like to inject. http://docs.oracle.com/javaee/6/tutorial/doc/gjbck.html – Bob Brinks Aug 09 '16 at 14:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120526/discussion-between-bob-brinks-and-yagami-light). – Bob Brinks Aug 09 '16 at 14:54
  • I am sorry but i found a solution and when i read your comment and answer i see that your answers isn't correct i think that my question isn't clear enough i will post an answer later thank you for your time – Yagami Light Aug 09 '16 at 14:55
  • @YagamiLight: I am very curious as to what solution you found and then why this answer was not good. Your question was, in the end, clear and this answer is valid (look at the upvotes!). If your question is not what you meant to ask but something different, then ask a new question. The answer that you give yourself is 75% identical to what is stated in this answer only in less words (both in number and quality). And your last part, is totally unclear to me. – Kukeltje Aug 11 '16 at 18:11
0

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.

Yagami Light
  • 1,756
  • 4
  • 19
  • 39
  • _"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. "_ This is totally unclear to me and I cannot see why you accepted this answer (with a downvote) and not the 3 times upvoted one. – Kukeltje Aug 11 '16 at 18:11
  • because the solution that you upvote for it is far from what you think of a solution, every problem is a new problem for the personne how ask for it, and if three people upvote for it doesn't mean it's the right answer for the question, and you know that even if i made it as a right answer i will not have any rewards (reputation) about it that mean something i think – Yagami Light Aug 11 '16 at 18:43
  • Remarkable isn't it that more than one person thinks that the question and upvoted answer are a match (yes, including me). If it is not by a long shot as you suggest, then there can only be one thing wrong. The question you posted is not what your actual problem/question was and you solved the real problem in a completely different way. That by itself would be remarkable as well, since most of your answer is about the same thing as the upvoted answer (only the other way around). Since you did not post code on how you solved it, this Q/A combination (your A) is totally useless to others imo. – Kukeltje Aug 11 '16 at 18:59
  • waw you can use google i found what is a lookup and how to work with it after 2 clicks that was so hard to found, and if you think that the max upvote mean the best solution you are wrong need to think about it – Yagami Light Aug 11 '16 at 19:07
  • I can find anything in Google if I know what to look for. Since that is the hard part here (I have some idea, but if you went that way/choose that solution then I think there is either a design flaw in your code or indeed a different problem than you posted here) . But since you won't share the solution in code, I can only guess and guessing is dangerous... Still this Q/A (your A) combination is not helpful. – Kukeltje Aug 11 '16 at 19:17
  • Something like this, right : http://stackoverflow.com/questions/8166187/can-i-and-how-lookup-cdi-managed-beans-using-javax-naming-contextlookup-in-ej – Kukeltje Aug 11 '16 at 19:26
  • In the current state of the question, this answer is roughly correct although I completely do not understand the part that Kukeltje highlighted either (consider rewriting that a bit); the code as presented means that `SubInterface` is not any kind of bean managed by the JEE container and thus an `@EJB` injection is not going to work in it. – Gimby Aug 12 '16 at 08:43