2

I am reading on JSF and I have a question about managed beans. Does "managed" only refer to the fact that the JSF implementation instantiate and manage their lifecycle? And the new thing now is to use CDI that is a different implementation that handles the instantiation and their lifecycle? Is that what it means by managed?

Is CDI an implementation capable of instantiating beans and manage them outside JSF, is that why it is better? If so, what was the option outside of JSF before? Did each container or implementation had their own way of managing the beans? And is CDI the thing that unifies this?

LuckyLuke
  • 47,771
  • 85
  • 270
  • 434
  • 1
    Theres an excellent answer here that I think will help you - http://stackoverflow.com/questions/2930889/are-managedbeans-obsolete-in-javaee6-because-of-named-in-cdi-weld. – Perception Jan 07 '12 at 17:17
  • @Perception Hi, thank you. It did almost answer my question but one still remains: If `@ManagedBean` was an annotation for saying to the implementation to take care of it (and this only applied for the beans supporting the view), then what did you do with the beans in the business layer? Was there no possibility to leave this responsibility to the EJB container? – LuckyLuke Jan 07 '12 at 20:19

1 Answers1

2

Before JSF2 annotations, in JSF1 the developers have to register the beans as <managed-bean> in faces-config.xml. The support is still there in JSF2, but it became optional and could in some cirsumstances be the only way if you want to override the annotations of a managed bean which is packaged in a 3rd party JAR.

Long before JSF and CDI, in plain JSP/Servlet, developers have to manually create and manage beans itself by explicitly instantiating them and explicitly placing them in the request, session or application scopes by

request.setAttribute("requestScopedBean", requestScopedBean);
request.getSession().setAttribute("sessionScopedBean", sessionScopedBean);
getServletContext().setAttribute("applicationScopedBean", applicationScopedBean);

(prechecks omitted, the bean is of course only placed in the scope if it isn't in the scope yet, else it will be reused)

Note that the above is exactly what JSF does "under the covers".

Then various MVC frameworks were invented to abstract this away by declaring the beans in XML file or even by annotations. Every MVC framework had its own way of managing beans. Then there was Spring, which tried to unify it by providing a framework-independent approach. Then there was Java EE 6 CDI which standardized it.

This all was always been container-independent. It's framework-specific.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555