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:
@PostConstruct
@PreDestroy
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;
@Stateless public class PasswordBeanImpl implements PasswordBeanLocal {
private Logger logger; @PostConstruct public void initBean() { logger = Logger.getLogger(PasswordBeanImpl.class.getName()); }
public String getGeneratedPassword() {
String generatedPassword = null;
return generatedPassword; } }
If you need any urgent assistance on Lifecycle_Callbacks_for_Stateless_Session_Beans, kindly email your requirement to us at : info@javagenious.com or Contact-an-Expert. Our experts will try their best to solve your problem.
Keyword Tags: Lifecycle_Callbacks_for_Stateless_Session_Beans tutorial,Lifecycle_Callbacks_for_Stateless_Session_Beans in java,Concepts of Lifecycle_Callbacks_for_Stateless_Session_Beans,Java,J2EE,Interview Questions,Lifecycle_Callbacks_for_Stateless_Session_Beans Examples
Most programming languages compile source code directly into machine code, suitable for execution on a particular microprocessor architecture. read more
|