wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes EJB Dependency Injection problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB Dependency Injection problem" Watch "EJB Dependency Injection problem" New topic
Author

EJB Dependency Injection problem

Jim Petersson
Ranch Hand

Joined: May 28, 2008
Posts: 48
Hi
I just started experimenting with EJB 3.0 and tried to get a very simple program to work.

I have an Interface "IMyTest" marked with @Remote. It has a single method "printHello()". I also have a class MyTestBean (marked @Stateless) which implements IMyTest.

I have declared a static variable IMyTest and annotated it with @EJB,
which I try to invoke the print-method on from my main-method. But my IMyTest-object doesn't get initialized. So I guess it's some problem with dep. injection here. Is it even supposed to work from a "regular" java application?


Thanks, Jim


SCJP 5<br />SCJD
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30929
    
158

Jim,
You can call an EJB from a regular class, but automatic injection will not occur. You have three choices:
1) Look up the JNDI name for the bean and inject it yourself
2) Call the EJB as a regular Java class (and mock everything out)
3) Write a test servlet which can automatically inject resources.


[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
Jim Petersson
Ranch Hand

Joined: May 28, 2008
Posts: 48
Hi Jeanne!
Thanks for your quick reply.
A few more questions:

"Look up the JNDI name for the bean and inject it yourself"
- This is pretty much the EJB2 style right? Since it's pretty "verbose" I would like to stay away from it.

"Call the EJB as a regular Java class"
- I'm not sure I get exactly what you mean? Are you suggesting that I just create the EJB-object with new? Seems like cheating

"Write a test servlet which can automatically inject resources."
- This is what I hoped not having to do, since my knowledge in servlets are rather limited.


In Ejb3 in action it says someting (page 55), that you should be able to use dep.inj. from a standalone client as long as it's running in the "Application Client Container". Question is then, how do I get it to run in the ACC? Btw, nice quote on the back of the book

Thanks, Jim
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30929
    
158

Originally posted by Jim Petersson:
"Look up the JNDI name for the bean and inject it yourself"
- This is pretty much the EJB2 style right? Since it's pretty "verbose" I would like to stay away from it.

Yes, this is EJB style. It is verbose, but you can write a factory and deal with it in one place. Note that a lot of EJB 3 code is still going to have callers using the "old style." For example, a Struts action cannot inject the EJB reference since it is not a servlet. (Even though it is called from a servlet.) I guess I'm saying the "old style" isn't bad.


"Call the EJB as a regular Java class"
- I'm not sure I get exactly what you mean? Are you suggesting that I just create the EJB-object with new? Seems like cheating

Yes, I meant call with "new." It's not cheating - it's a different use. Say you want to test a method in the EJB that doesn't access the database. It makes sense to just create the bean as a class to test that method. This approach doesn't help with your problem of testing the real EJB. I was mentioning it more to be thorough.


"Write a test servlet which can automatically inject resources."
- This is what I hoped not having to do, since my knowledge in servlets are rather limited.

I would lean towards the JNDI call or app client rather than the servlet as it is clearer why the class exists.


In Ejb3 in action it says someting (page 55), that you should be able to use dep.inj. from a standalone client as long as it's running in the "Application Client Container". Question is then, how do I get it to run in the ACC?

That is correct. An application client is a type of JEE component. (just like an ejb-jar or war is.) This Sun page has some details on it.


Btw, nice quote on the back of the book

Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB Dependency Injection problem