aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Need clarifications on stateless beans, DI, lookups, static keyword 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 "Need clarifications on stateless beans, DI, lookups, static keyword" Watch "Need clarifications on stateless beans, DI, lookups, static keyword" New topic
Author

Need clarifications on stateless beans, DI, lookups, static keyword

lokesh sree
Ranch Hand

Joined: Oct 27, 2009
Posts: 100

Hi ranchers,

I have multiple questions on stateless beans and their usage through their interfaces in context of EJB3.0

If there are two beans with each one needing to invoke some function call on the other bean, then using dependency injection on both beans to inject one bean into another will create circular dependency . And, I see that there is a IgnoreDepedency annotation to overcome this issue. My question is what exactly is the functionality of this annotation? What will be the behavior if this annotation is used in both the beans while injecting the other?

If a stateless bean needs references to the local interfaces of multiple other beans, what is the best way to achieve this? I have seen multiple approaches being taken and would like to understand if there are any pros and cons for each of these.

1. Using @EJB, inject all those required beans. Though this is direct and simple, I believe this would become a little complex if there are circular dependencies.
And using lookups would avoid the circular dependencies.

2. Use static variables to hold the reference to local variables and initialize them by lookups in a static code block of the stateless bean

A couple of questions here - Is it safe to have static code blocks in the stateless beans?
I have seen the JEE spec saying that only final static variables should be used inside a stateless bean. But I have tried the above code and it works fine. Can someone explain the issues with this?

3. Use instance variables and do the initialization/lookup inside a private method and call it at start of each function call of BeanA.

Does var1 here go against the principle of stateless nature of this bean? will there be thread issues since initFunc() is not synchronized?
Will there be any difference with making var1 a static variable?

4. Can all this initialization/lookup be done some where else as well - Constructor?

Thanks for any inputs.


Lokesh
( SCBCD 5, CCENT, SCJP 5 )
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10288
    
168

lokesh chenta wrote:

If there are two beans with each one needing to invoke some function call on the other bean, then using dependency injection on both beans to inject one bean into another will create circular dependency . And, I see that there is a IgnoreDepedency annotation to overcome this issue. My question is what exactly is the functionality of this annotation?

This is a very specific JBoss EJB3 implementation issue. It was a workaround to the way JBoss EJB3 had implemented the @EJB injection. Ideally, you shouldn't be needing that @IgnoreDependency annotation. Infact, you don't need it in JBoss AS 6.x version.

1. Using @EJB, inject all those required beans. Though this is direct and simple, I believe this would become a little complex if there are circular dependencies.

Don't worry about those circular "dependencies". Like I said, it was just an issue with the way it was implemented in previous versions of JBoss AS/EJB3.

2. Use static variables to hold the reference to local variables and initialize them by lookups in a static code block of the stateless bean


Stateless beans aren't meant to hold state across multiple invocations.

[My Blog] [JavaRanch Journal]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need clarifications on stateless beans, DI, lookups, static keyword