Is there any situation where it's better to use JNDI than to inject a stateless session bean using the @EJB
annotation?
We're using JSF 1.2 with Sun Application Server 9.0_01.
Our team is debating which approach is better when using SLSBs in a Managed Bean.
I've read the following questions, but was wondering if there was a situation where lookup is preferred.
EJB3 - obtaining bean via injection vs lookup - what are the differences, implications, gotchas?
@EJB injection vs lookup - performance issue
Is there any situation where it's better to use JNDI than to inject a stateless session bean using the @EJB annotation?
There's no situation where it's better--but situations where it's necessary:
If the name to look up is constant and injection is possible, prefer @EJB
annotations:
JNDI lookup might be important in case of SFSB (be sure to access the same instance all the time), but in case of SLSB I am not aware of any cases where JNDI would be "better" in any way.
I'd definitely go with @EJB
. It's easier to read (less error-prone code), easier to maintain (you don't care about JNDI namespace location of the bean) and easier to test (no nasty lookup code you need to cut off when performing unit tests).
When talking about performance reasons - I am not 100% sure but I wouldn't be surprised if it'd occur that the application server is in fact doing a JNDI lookup behind the scenes when you use annotations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With