A developer writes a stateful session bean FooBarBean with two local business interfaces Foo and Bar. The developer wants to write a business method called getBar for interface Foo that returns a Bar reference to the same session bean identity on which the client invokes getBar. Which code, when inserted on Line 12 below, implements the getBar method with the wanted behavior? 10. @Resource SessionContext sessionCtx; 11. public Bar getBar() 12. 13. } A. return (Bar) this; B. return (Bar) new FooBarBean(); C. return (Bar) sessionCtx.lookup("FooBarBean") D. return (Bar) sessionCtx.getBusinessObject(Bar.class); E. lnitialContext ic = new lnitialContext(); return (Bar) ic.lookup("java:comp/env/ejb/FooBarBean");
given answer is D,I want know why A,C,E are wrong?
SCJP 5.0<br />SCWCD1.4<br />SCBCD5
posted 8 years ago
JEE servers use Proxy mechanisms to obtain references on EJB objects through business interface. With proxy, container can react on some properties thirled to business methods like @TransactionAttribute or @RolesAllowed.
Even if you return reference on Bar interface it will not be business interface connected to EJB object, so probably you get an exception when you invoke some method from it. That is why answer A) is wrong. Try java.lang.reflect.Proxy for more details. It is great fun.
C) and E) are wrong because lookup method will return reference to the new EJB object, not the same used for method invocation.
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads