@PostConstruct
An annotated method with noformat("@PostConstruct") is required to be called after dependencies injection (resolution of noformat("@Resource"), ...) and before being used (business methods called).
The
@PostConstruct annotation is defined inside the Bean class :
@PostConstruct
private void post() {
System.out.println("PostConstruct called by EasyBeans");
}The output is displayed in the next section.
@PreDestroy
An annotated method with noformat("@PreDestroy") is required to be called before instance removal. Once the annotated method has been called, the bean is unavailable for futurre uses.
The
@PreDestroy annotation is defined in a separate class (re-using the
@Interceptors annotation).
So the annotated Method goes inside the GreeterInterceptor class :
public class GreeterInterceptor { ... /**
* Called before Bean destruction.
* @param ic InvocationContext
* @return proceeded method
* @throws Exception if something wrong occurs.
*/
@PreDestroy
public void destroy(InvocationContext ic) throws Exception {
System.out.println("PreDestroy invoked.");
}
}
|
Simply annotate a method with @PreDestroy to release resources that may have been locked/used by the Bean. |
The output for
@PreDestroy and
@PostConstruct :
PostConstruct called by EasyBeans <----------- Here . . .
Method 'greetMe' invoked 1 times.
Entering method : greetMe
Message received: GuillaumeLeaving method : greetMe
PreDestroy called by EasyBeans. <----------- . . . and here