How is the return-value of SessionContext.getBusinessObject() different from 'this' keyword used in the bean?

前端 未结 1 1637
暗喜
暗喜 2020-12-19 09:55

The SessionContext.getBusinessObject() is described in the docs as follows,

Obtain an object that can be used to invoke the current bean through the g

相关标签:
1条回答
  • 2020-12-19 10:33

    The motivation here is that most EJB implementations work on proxies. You wouldn't be too far off in thinking of it as old-school AOP. The business interface is implemented by the EJB container, quite often via a simple java.lang.reflect.Proxy, and this object is handed to everyone in the system who asks for the ejb via @EJB or JNDI lookup.

    The proxy is hooked up to the container and all calls on it go directly to the container who will preform security checks, start/stop/suspend transactions, invoke interceptors, etc. etc. and then finally delegate the call to the bean instance -- and of course do any clean up required due to any exceptions thrown -- then finally hand the return value over through the proxy to the caller.

    Calling this.foo() directly, or passing 'this' to a caller so they can make direct calls as well, will skip all of that and the container will be effectively cut out of the picture. The 'getBusinessObject(Class)' method allows the bean instance to essentially get a proxy to itself so it can invoke its own methods and make use of the container management services associated with it -- interceptors, transaction management, security enforcement, etc.

    0 讨论(0)
提交回复
热议问题