Using the @PostConstruct and @PreDestroy Annotations With CDI Managed Bean Classes
CDI managed bean classes and their superclasses support the annotations for initializing and for preparing for the destruction of a bean. These annotation are defined in JSR 250: Common Annotations for the Java platform.
To Initialize a Managed Bean Using the @PostConstruct Annotation
Initializing a managed bean specifies the lifecycle callback method that the CDI framework should call after dependency injection but before the class is put into service.
- In the managed bean class or any of its superclasses, define a method that performs the initialization that you require.
- Annotate the declaration of the method with the javax.annotation.PostConstruct annotation.
When the managed bean is injected into a component, CDI calls the method after all injection has occurred and after all initializers have been called.
Note - As mandated in JSR 250, if the annotated method is declared in a superclass, the method is called unless a subclass of the declaring class overrides the method.
The UserNumberBean managed bean in The guessnumber CDI Example uses @PostConstruct to annotate a method that resets all bean fields:
@PostConstruct public void reset () { this.minimum = 0; this.userNumber = 0; this.remainingGuesses = 0; this.maximum = maxNumber; this.number = randomInt.get(); }
To Prepare for the Destruction of a Managed Bean Using the @PreDestroy Annotation
Preparing for the destruction of a managed bean specifies the lifecycle call back method that signals that an application component is about to be destroyed by the container.
- In the managed bean class or any of its superclasses, prepare for the
destruction of the managed bean.
In this method, perform any cleanup that is required before the bean is destroyed, such as releasing a resource that the bean has been holding.
- Annotate the declaration of the method with the javax.annotation.PreDestroy annotation.
CDI calls this method before starting to destroy the bean.