In my web application I have a presentation Layer consisting of several jsf-viewscoped and some requestscoped beans. Normally I only want to have presentation logic in these, so whenever there's a task which goes beyond presentation logic, I use another Bean or an EJB. So in some viewscoped Beans I have an EJB injected. My question now is as follows : Should I avoid situations in which I have injected an EJB directly into a Bean used for presentation, by creating another (for example application scoped) bean which only delegates the method calls to the respective EJB ? And : When is the injected EJB initialized ? If it's initialized during the initialization of the bean it is injected into, then this would mean that for every visitor of my application there's always also an EJB reserved? I don't think thats the case but that EJBs are only used when one of its methods is called. But how should I separate business and presentation layers properly ?
Asked
Active
Viewed 373 times
1 Answers
2
Injecting an EJB into a view scoped backing bean is common and a best practice.
The (view scoped) backing bean takes care of any view related concerns, and it delegates to the EJB bean for business logic.
Often (but not necessarily) a view bean loads data from an EJB bean in its PostConstruct, which it then holds on to during postbacks. This can be very benificial for both performance and consistency.

Mike Braun
- 3,729
- 17
- 15
-
ah ok so I think I got something wrong there. One could say then that the viewscoped beans don't belong to the presentation layer but to the application layer? The presentation layer would then consist of the jsf pages ? – nico1510 May 16 '13 at 09:34
-
2Yes, the presentation layer consist only of Facelets. Check [Understanding JSF as a MVC framework](http://stackoverflow.com/questions/10111387/understanding-jsf-as-a-mvc-framework). Regards, – Rodmar Conde May 16 '13 at 11:52
-
What about serialization? When I inject a EJB in a viewscoped bean like that I get serialization errors... – salihcenap Jul 20 '13 at 09:44
-
@salihcenap good remark and worth a new top level question! In theory the proxy would be serialized and the proxy should only be a string like (jndi) reference. It should serialized perfectly well. I know that in practice it doesn't always work like that :( – Mike Braun Jul 24 '13 at 18:06