EJB 3.1 @LocalBean vs no annotation

I understand the difference between a local view, a remote view, and a view without an interface. I just don’t understand what is the difference between β€œno view” (without annotation) and a view without an interface. And also why should I annotate my interface using @Local ? What if I don’t annotate the interface at all, is there any difference?

+60
java java-ee ejb
Jun 04 '12 at 23:00
source share
4 answers

Rules (from memory):

  • Bean has annotation @LocalBean bean has a view without an interface
  • Bean has annotation @Local bean has a local view
  • Bean has annotation @Remote bean has a remote view
  • A Bean does not have view annotations, but directly implements an interface that has an @Local annotation -> a bean has a local representation
  • Bean does not have view annotations, but directly implements an interface that has @Remote β†’ bean annotation, has remote viewing
  • Bean does not have view annotations, but directly implements an interface that does not have view annotations -> bean has a local view
  • Bean does not have view annotations and does not implement interfaces -> bean has a view without interface

So using @LocalBean and not using annotation at all are both ways to get a view without an interface. If you just need a view without an interface, then the easiest way is not to comment. If you do not implement any interfaces.

Part of the reason @LocalBean exists to add a view without an interface to the bean, which also takes the form of an interface. I believe that the script topmost in the minds of the spec authors was one that has a bean like:

 @Stateless public class UserPreferences { public String getPreference(String preferenceName); public Map<String, String> getPreferences(); } 

If you want to expose both methods locally, but only the getPreferences() remotely. This can be done by declaring the remote interface with this particular method, and then simply pressing @LocalBean in the bean class. Without this, you would have to write a meaningless local interface to expose both methods locally.

Or, to look at it differently, @LocalBean exists, because there is such a thing as a view without an interface, and the no-annotation option exists as a convenient shortcut.

+114
Jun 05 '12 at 11:22
source share
  • Remote EJBs: can be accessed from remote clients (clients running on a different JVM, such as Swing or JavaFX clients that run on a user machine)
  • Local EJBs: there can only be access from other "components" running on the same JVM, for example. Web interfaces, other EJBs
  • View without interface: same as Local, but without specifying a business interface
  • Without annotation: simple POJO but not EJB

Local / No interface representations are more efficient than remote EJBs, as object references can be passed.

+14
Jun 04 '12 at 23:12
source share

I think the confusion you / we feel is the result of comparability of history / back (so to speak). I cannot distinguish any difference (except that the specification requires implementations to create an interface if we use a local view)

A view without an interface has the same behavior as the local view of EJB 3.0, for example, it supports features such as call-by-reference semantics and transaction propagation and security. However, the no-interface view does not require a separate interface, that is, all public methods of the bean class are automatically exposed to the caller. By default, any bean session that has an empty tool and does not define any other local or remote client views gives a view without an interface.

Oracle Blog Before EJB 3.1

+6
Jun 05 2018-12-12T00:
source share

If you are interested in more technical details, let me tell you what is really happening ... You do not have direct access to the EJB, this means that you do not have a link (address) of the actual EJB. When you browse or enter your EJB, the container provides the object as a client for that EJB (we can call a proxy or Wrapper), and you call your business methods on this proxy object. (This is why you should not use the new keyword to create an object of the EJB class)

Now for each type of annotation, the container generates different types of proxies with different methods and functions.

@LocalBean (or without annotation) Your proxy object has:

  • setOptionalLocalIntfProxy()
  • getSerializableObjectFactory()

@Local You use a proxy object for a local call and type com.sun.proxy So, it has:

  • getSerializableObjectFactory()
  • isProxyClass()
  • getProxyClass()
  • getInvocationHandler()
  • newProxyInstance()

@Remote The Wrapper object uses a remote call, and it has:

  • readResolve()
  • writeReplace()
  • getStub()
  • getBusinessInterfaceName()
0
Dec 21 '15 at 10:22
source share



All Articles