Using Annotations to Configure Managed Beans (original) (raw)

Note: In JSF 2.3, managed bean annotations are deprecated; CDI is now the preferred approach.

JavaServer Faces support for bean annotations is introduced inChapter 7, "JavaServer Faces Technology". Bean annotations can be used for configuring JavaServer Faces applications.

The @Named (javax.inject.Named) annotation in a class, along with a scope annotation, automatically registers that class as a resource with the JavaServer Faces implementation. A bean that uses these annotations is a CDI managed bean.

The following shows the use of the @Named and @SessionScopedannotations in a class:

@Named("cart")
@SessionScoped
public class ShoppingCart ... { ... }

The above code snippet shows a bean that is managed by the JavaServer Faces implementation and is available for the length of the session.

You can annotate beans with any of the scopes listed in the next section, Using Managed Bean Scopes.

All classes will be scanned for annotations at startup unless thefaces-config element in the faces-config.xml file has themetadata-complete attribute set to true.

Annotations are also available for other artifacts, such as components, converters, validators, and renderers, to be used in place of application configuration resource file entries. These are discussed, along with registration of custom listeners, custom validators, and custom converters, in Chapter 15, "Creating Custom UI Components and Other Custom Objects".

Using Managed Bean Scopes

You can use annotations to define the scope in which the bean will be stored. You can specify one of the following scopes for a bean class.

You may want to use @Dependent when a managed bean references another managed bean. The second bean should not be in a scope (@Dependent) if it is supposed to be created only when it is referenced. If you define a bean as @Dependent, the bean is instantiated anew each time it is referenced, so it does not get saved in any scope.

If your managed bean is referenced by the binding attribute of a component tag, you should define the bean with a request scope. If you placed the bean in session or application scope instead, the bean would need to take precautions to ensure thread safety, becausejavax.faces.component.UIComponent instances each depend on running inside of a single thread.

If you are configuring a bean that allows attributes to be associated with the view, you can use the view scope. The attributes persist until the user has navigated to the next view.