Using Annotations to Configure Managed Beans (original) (raw)
2. Using the Tutorial Examples
3. Getting Started with Web Applications
4. JavaServer Faces Technology
7. Using JavaServer Faces Technology in Web Pages
8. Using Converters, Listeners, and Validators
9. Developing with JavaServer Faces Technology
10. JavaServer Faces Technology: Advanced Concepts
11. Using Ajax with JavaServer Faces Technology
12. Composite Components: Advanced Topics and Example
13. Creating Custom UI Components and Other Custom Objects
14. Configuring JavaServer Faces Applications
Application Configuration Resource File
Ordering of Application Configuration Resource Files
Using the managed-bean Element
Initializing Properties Using the managed-property Element
Referencing a Context Initialization Parameter
Initializing Array and List Properties
Initializing Managed Bean Properties
Registering Application Messages
Using FacesMessage to Create a Message
Registering a Custom Validator
Registering a Custom Converter
To Configure a Navigation Rule
Registering a Custom Renderer with a Render Kit
Registering a Custom Component
Basic Requirements of a JavaServer Faces Application
Configuring an Application with a Web Deployment Descriptor
Identifying the Servlet for Lifecycle Processing
To Specify a Path to an Application Configuration Resource File
To Specify Where State Is Saved
Including the Classes, Pages, and Other Resources
16. Uploading Files with Java Servlet Technology
17. Internationalizing and Localizing Web Applications
18. Introduction to Web Services
19. Building Web Services with JAX-WS
20. Building RESTful Web Services with JAX-RS
21. JAX-RS: Advanced Topics and Example
23. Getting Started with Enterprise Beans
24. Running the Enterprise Bean Examples
25. A Message-Driven Bean Example
26. Using the Embedded Enterprise Bean Container
27. Using Asynchronous Method Invocation in Session Beans
Part V Contexts and Dependency Injection for the Java EE Platform
28. Introduction to Contexts and Dependency Injection for the Java EE Platform
29. Running the Basic Contexts and Dependency Injection Examples
30. Contexts and Dependency Injection for the Java EE Platform: Advanced Topics
31. Running the Advanced Contexts and Dependency Injection Examples
32. Introduction to the Java Persistence API
33. Running the Persistence Examples
34. The Java Persistence Query Language
35. Using the Criteria API to Create Queries
36. Creating and Using String-Based Criteria Queries
37. Controlling Concurrent Access to Entity Data with Locking
38. Using a Second-Level Cache with Java Persistence API Applications
39. Introduction to Security in the Java EE Platform
40. Getting Started Securing Web Applications
41. Getting Started Securing Enterprise Applications
42. Java EE Security: Advanced Topics
Part VIII Java EE Supporting Technologies
43. Introduction to Java EE Supporting Technologies
45. Resources and Resource Adapters
46. The Resource Adapter Example
47. Java Message Service Concepts
48. Java Message Service Examples
49. Bean Validation: Advanced Topics
50. Using Java EE Interceptors
51. Duke's Bookstore Case Study Example
52. Duke's Tutoring Case Study Example
53. Duke's Forest Case Study Example
JavaServer Faces support for bean annotations is introduced in Chapter 4, JavaServer Faces Technology. Bean annotations can be used for configuring JavaServer Faces applications.
The @ManagedBean (javax.faces.bean.ManagedBean) annotation in a class automatically registers that class as a resource with the JavaServer Faces implementation. Such a registered managed bean does not need managed-bean configuration entries in the application configuration resource file.
An example of using the @ManagedBean annotation in a class is as follows:
@ManagedBean @SessionScoped public class DukesBday{ ... }
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 do not need to configure the managed bean instance in the faces-config.xml file. In effect, this is an alternative to the application configuration resource file approach and reduces the task of configuring managed beans.
You can also define the scope of the managed bean within the class file, as shown in the above example. You can annotate beans with request, session, application, or view scope.
All classes will be scanned for annotations at startup unless the faces-configelement in the faces-config.xml file has the metadata-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 13, 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:
- Application (@ApplicationScoped): Application scope persists across all users’ interactions with a web application.
- Session (@SessionScoped): Session scope persists across multiple HTTP requests in a web application.
- View (@ViewScoped): View scope persists during a user’s interaction with a single page (view) of a web application.
- Request (@RequestScoped): Request scope persists during a single HTTP request in a web application.
- None (@NoneScoped): Indicates a scope is not defined for the application.
- Custom (@CustomScoped): A user-defined, nonstandard scope. Its value must be configured as a java.util.Map. Custom scopes are used infrequently.
You may want to use @NoneScoped when a managed bean references another managed bean. The second bean should not be in a scope (@NoneScoped) if it is supposed to be created only when it is referenced. If you define a bean as @NoneScoped, 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, because javax.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.
Eager Application-Scoped Beans
Managed beans are lazily instantiated. That is, that they are instantiated when a request is made from the application.
To force an application-scoped bean to be instantiated and placed in the application scope as soon as the application is started and before any request is made, the eager attribute of the managed bean should be set to trueas shown in the following example:
@ManagedBean(eager=true) @ApplicationScoped
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Legal Notices