@PostConstruct & @PreDestroy annotation in JSF

Quick JSF tip-

I found these two annotation really useful and very tricky too.It should be used at right time otherwise you will see error like me. 🙂

@PostConstruct as per Oracle Doc-
The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. This method MUST be invoked before the class is put into service. This annotation MUST be supported on all classes that support dependency injection. The method annotated with PostConstruct MUST be invoked even if the class does not request any resources to be injected. Only one method can be annotated with this annotation. The method on which the PostConstruct annotation is applied MUST fulfill all of the following criteria – – The method MUST NOT have any parameters except in the case of EJB interceptors in which case it takes an InvocationC ontext object as defined by the EJB specification. – The return type of the method MUST be void. – The method MUST NOT throw a checked exception. – The method on which PostConstruct is applied MAY be public, protected, package private or private. – The method MUST NOT be static except for the application client. – The method MAY be final. – If the method throws an unchecked exception the class MUST NOT be put into service except in the case of EJBs where the EJB can handle exceptions and even recover from them.

ok .Quite good description and confusing too.

To Initialize a Managed Bean we use the @PostConstruct Annotation.As we see that @PostConstruct Annotation is mostly used to initialize the resources that are Context specific. The method which you marked with @PostConstruct Annotation will be called immediately by the Container as soon as an instance of bean is created. There are few certain guidelines to be followed, while creating the PostConstruct method such as-

– The method should not be marked as static.
– Return type should be void.
– It should not throw any checked Exceptions and so on….

I did a mistake of calling a bean and i got some null on the bean.Not sure what is problem. After spending my few hours on it 🙁 .
I found the problem.I am calling before the instantiation of the bean.after using this annotation, it resolved the issue , so sharing with everyone.

I have a method which is clearing all the value .i used annotation like this

@PostConstruct
public void clear() {
    this.userName = "";
    this.userBal= 0;
    this.maximum = maxNumber;
    this.number = randomInt.get(); 
}


@PreDestroy-
counter-part to @PostConstruct Annotation is the @PreDestroy Annotation. As the name specify, we know that the method that is marked with this Annotation will be called before object is going to be removed or destroyed by the Container. Like the @PostConstruct Annotation, this is also a method-Level Annotation and i used like this

@PreDestroy()
    public void releaseConnection()
    {
 
        // Close the Connection.
     
    }

method releaseConnection() will call by the container, before the object is going to be destroyed. In Jee 6 ,CDI calls this method before starting to destroy the bean.