• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using DI in Client

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh..Thank you very much for your precious time....
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic