HTML and CSS Reference
In-Depth Information
Listing 2-5. The @PostConstruct and the @ PostDestroy Annotations
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
public class User implements Serializable {
private String name;
private String password;
// The setters and the getters ...
private void initialize() {
System.out.println("Bean is initialized with the following user name: " + name);
private void cleanUp() {
System.out.println("You can do the cleanup here");
The @PostConstruct method is called when the User managed bean is instantiated and initialized. This means
that the output console will show the following message when the initialize method is called:
"Bean is initialized with the following user name: anonymous"
as you notice, the @PostConstruct and the @PreDestroy methods return types are void and take no
arguments. the methods can also be private , public , protected , or package .
The @PreDestroy method is called before the User managed bean is destroyed. In this method, you can put the
required cleanup and de-initialization code of the managed bean.
There are also many other Java EE annotations that are related to the data access and can be used perfectly with
the JSF managed beans, such as @Resource , @PersistenceUnit , @PersistenceContext , @EJB , and @WebServiceRef .
We will cover most of these annotations in the next chapters.
Adding to the ability of initializing simple attributes, you can also initialize the managed bean complex types
such as lists and maps. Listing 2-6 introduces two new properties to the User managed bean: the favoriteSports List
and spokenLanguages Map.
Search WWH ::

Custom Search