Lifecycle Callbacks for Stateless Session Beans
The J2EE server (like Glassfish or Oracle Weblogic) manages the life cycle of a stateless session bean unlike regular Java classes. The server decides when to create and remove bean instances, so the application never knows how many instances of a session bean class that is created, nor does it know when they are created or destroyed.
Since the server itself needs to initialize services for the bean before the business logic is invoked, and the bean itself most likely will need to make some initializations of itself,
the constructor is not a good place for that bean initialization code.
To allow both the server and the enterprise bean to make their initializations, EJBs support something called "lifecycle callback methods" that are annotated by the programmer and invoked by the server at various points in th beans life cycle.

For stateless session beans there are two lifecycle callbacks:



The server will invoke the method that is annotated with the @PostConstruct annotation as soon as the server is finished with its initialization tasks,
and the server will also invoke the method annotated with the @PreDestroy annotation immediately before the bean instance is released for garbage collection.

A good use of the @PostConstruct annotation could be to get hold of a Logger instance.

package com.javagenious.examples;

import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;

 * @author

public class PasswordBeanImpl implements PasswordBeanLocal {

    private Logger logger;
    public void initBean() {
        logger = Logger.getLogger(PasswordBeanImpl.class.getName());

    public String getGeneratedPassword() {

        String generatedPassword = null;

        //Code to generate password here

        return generatedPassword;

Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on ; I would like to hear from you :)