Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using DI in Client

 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a doubt regarding injecting the Beans in the client code.
Following is the code:



In the above code, I want to get the MyBean instance using DI. I know how to get it through JNDI lookup. But I want to know in which cases I can use DI to get the Bean. I am getting nullpointerException if am using DI. [ ].

Thanks,
Krishna.
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the EJB container which implements resources injection for Session beans and MDB.
Everything else is simply ignored: an annotation has no logic at all. The ejb container looks at the annotations on ejbs and injects resources.
If you use annotations in a class of yours which is not explicitly managed by the container, it is simply ignored: for example, nothing prevents you from creating your own annotation class (there's a cool example on O'Reilly book)
 
Anish Mathur
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DI works only in container managed classes like EJB's, Servlets etc. Therefore, if you client is a managed class then only @EJB annotation can be used.
 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Look at the below code:


In the above code I am getting Null pointer exception. What I am doing is, I have a jar file consists of Ejb bean, I have a war file consists of this servlet. I am making a Ear file and deployed it in Jboss.

If am using JNDI look up for getting the bean, it is working fine but if i am using @EJB, i am getting Null pointer. Can anyone helps me where I am doing mistake?
 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't think you need the remote part in the @EJB(name="Calculator/remote") as you are referring from a servlet. Just try withouth the /remote part and see.


Dinuka
 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

No luck dude [ ]!!
I have tried every combination and with out giving the name as it is optional.
 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
One more thing. If your using JBoss as the app server then i think servlet DI was only supported from version 4.1.x... so if your using an older JBoss version this could be a problem. What is the JBoss app server version you are using?
 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using JBoss-4.2.2
 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
found you problem. found it at JBoss 4.2.2 it says:
However when we started using DI with the latest stable version of JBoss (4.2.2) we found that DI of EJB references has limited support. Namely it is only supported for objects inside the EJB container. This means that while beans can refer to each other with the @EJB annotation, other managed objects like servlets and non-EJB web services must still use JNDI lookup to access any Enterprise Beans.


hope this solves your problem
 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh..Thank you very much for your precious time....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic