Even it's technically possible to do that (JSF allows you to inject beans which are at the same or wider scope) I don't see the point of doing that with @ViewScoped
beans. From my point of view, a well designed JSF web application should have a single @ViewScoped
bean tied to each specific view. Then, how to solve your issue? You can do it in two ways:
- If
SharableManagedBean
is a utility bean which contain static methods which are not tied to JSF, just define this class asabstract
and statically invoke its methods everytime you need. - If
SharableManagedBean
itself has to be a managed bean which access theFacesContext
and has common code that is shared along all the view beans, just create anabstract
class and make your@ViewScoped
beans extend it.
For your last question (SharableManagedBean
being @RequestScoped
), JSF doesn't allow you doing that. You'll get an exception because of trying to inject a narrower scoped managed bean.
According to the Oracle docs:
Another important point about managed beans referencing each other is that a managed bean can only refer to other beans provide their scope is equal or has a longer lifespan than the calling object.
Update
If using CDI, it's possible to inject a @RequestScoped
bean into a @ViewScoped
one too, using the Proxy pattern. Have it a look.