Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJB 3.1 @LocalBean vs no annotation

I understand the difference between local view, remote view and no-interface view. I just don't understand what is the difference between "no view" (no annotation) and no-interface view. And also why should I annotate my interface with @Local? What if I don't annotate the interface in at all, is there a difference?

like image 271
VaclavDedik Avatar asked Jun 04 '12 23:06

VaclavDedik


People also ask

Which annotation is required for remote view EJB?

Your EJB must now define all its business interfaces using @Local annotation so it's additional work for you. Not only you implement an interface but you need to remember to declare that your EJB is exposing it.

What does EJB annotation do?

It declares a reference in the component namespace. For example, @EJB(name="myEJB") creates a reference java:comp/env/myEJB . If you annotate a field and do not specify a name, then it creates a reference java:comp/env/com.

What is EJB LocalBean?

Annotation Type LocalBeanDesignates that a session bean exposes a no-interface view. This annotation is required if a session bean exposes any other client views (local, remote, no-interface, 2.

What is no-interface view?

No-interface view is just a convenience feature that allows a bean to expose a local client view without having to declare a separate interface.


1 Answers

The rules are (from memory):

  1. Bean has a @LocalBean annotation -> bean has a no-interface view
  2. Bean has a @Local annotation -> bean has a local view
  3. Bean has a @Remote annotation -> bean has a remote view
  4. Bean has no view annotations, but directly implements an interface which has a @Local annotation -> bean has a local view
  5. Bean has no view annotations, but directly implements an interface which has a @Remote annotation -> bean has a remote view
  6. Bean has no view annotations, but directly implements an interface which has no view annotations -> bean has a local view
  7. Bean has no view annotations, and implements no interfaces -> bean has a no-interface view

So, using @LocalBean and using no annotation at all are both ways of getting a no-interface view. If you just want a no-interface view, then the simplest thing is not to annotate. Provided you're not also implementing any interfaces.

Part of the reason @LocalBean exists to add a no-interface view to a bean which also has an interface view. I imagine the scenario uppermost in the spec authors' minds was one where you have a bean like:

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

Where you would want to expose both methods locally, but only the coarser-grained getPreferences() remotely. You can do that by declaring a remote interface with just that method, then just slapping @LocalBean on the bean class. Without it, you'd have to write a pointless local interface just to expose both methods locally.

Or, to look at it another way, the @LocalBean exists because there is such a thing as a no-interface view, and the no-annotation option exists as a handy shortcut.

like image 94
Tom Anderson Avatar answered Sep 22 '22 23:09

Tom Anderson