martes, 6 de octubre de 2009

LifeCycle Support

LifeCycle Support

A requirement for IoC is handling lifecycle support. In fact JSF's managed bean facility handles lifecycle support extremely well in that it is specifically geared toward defining lifecycle scope for HTTP requests as opposed to a more generic form which is common in other IoC containers. Similar to the scope parameters as specified by the JSP UseBean tag, the JSF scope sets the lifecycle with the following:

* none: A managed bean with this scope is not stored anywhere, it is created "on demand" whenever it is needed. * request: A bean with a request scope will have it's value stored only for the duration of a single request. Upon subsequent requests on the same object, a new clean version will be instantiated. * session: A bean with session scope will be stored onto the session meaning that the bean's properties will stay alive during multiple requests. A shopping cart bean would be set to a session scope. Objects stored onto the session are naturally threadsafe and will expire when the session times out if not cleared by the application explicitly. You wouldn't want anyone else looking in your shopping cart! * application: Objects created with application scope will exist during the entire lifetime of the container. An example of when usage makes sense could be for a JDBC datasource object.

You may notice that the "page" scope is not available in JSF. This is by design as managed beans are not tied specifically to pages whereas in traditional JSP, you would define the scope of a bean using a UseBean tag, and in this sense it makes sense to offer a page scope since the bean originated from the page.

As you can define the lifecycle of the beans using the scope setting, you must take care to only refer to other beans which exist either the same scope or larger scope. You can not refer to another managed bean with a smaller scope.Managed beans registered with scope: Can only refer to other managed beans with these scopes:none nonerequest none, request, session, applicationsession none, session, applicationapplication none, application