This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Dependency INjection through Annotations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Dependency INjection through Annotations" Watch "Dependency INjection through Annotations" New topic

Dependency INjection through Annotations

amarshi mohanty
Ranch Hand

Joined: Jul 01, 2008
Posts: 110
Hi All,

I have an EJB Module project which has:

And below is the web module:

But i am getting the bewlo exception:
at com.stateless.DepInjHelloWorld.main(

Line 19:System.out.println(hello.getMessage("Using Annotations Instead Of JNDI LookUp"));

PLease tell me if i need to configure anywhere,as i am not using any xml file.


SCJP (97%), SCWCD (95%))
Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30050

That's not a web module; it is a main method. Are you running it as an application client or a regular Java class? If the later, you need to do a JNDI lookup and not dependency injection.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
amarshi mohanty
Ranch Hand

Joined: Jul 01, 2008
Posts: 110
I have also tried using a servlet instead of the standalone class, but the same exception comes.

pulkit mehra

Joined: Jun 05, 2008
Posts: 12
Hi Amrish,

Hope this help
This is Stand Alone Client for EJB 3.0 in JBoss :
amarshi mohanty
Ranch Hand

Joined: Jul 01, 2008
Posts: 110

Thanks pulkit, i have tried this way its working.
But i want to use dependency injection instead of jndi lookup.
I have googled a couple of examples (using dependency injection for jndi look up instead of using initial context for lookup), but for me the business interface instance comes as null.
Jaikiran Pai

Joined: Jul 20, 2005
Posts: 9912

You actually have 2 questions here.

1) Injection into standalone java clients - This isn't possible unless you use an application client container. See this previous discussion

2) Injection into servlets - This is allowed. If you are not able to inject into servlet, then see if the issue is related to this

[My Blog] [JavaRanch Journal]
pulkit mehra

Joined: Jun 05, 2008
Posts: 12
See..according to my experience dependency injection gets failed due to following reason :

1)You are using server which does not support DI in web JBoss 4.x(DI work in EJB not in WEB)
2)You must have giving wrong JNDI name.Please refer to JNDI console of your server and see naming convention they follow.
I agree. Here's the link:
subject: Dependency INjection through Annotations
Similar Threads
jndi name in ejb3?
Can't access EJB from remote client
Getting Class cast exception while Lookup EJB2.1 session bean using JNDI name in WAS6.1
Call EJB from Application Client
EJB3 @EJB annotation not loading Session bean