background image

Life-Cycle Callback Methods

<< Session Bean Class | Business Methods >>
<< Session Bean Class | Business Methods >>

Life-Cycle Callback Methods

throw new BookException(
"Invalid id: " + id);
}
contents = new ArrayList<String>();
}
public void addBook(String title) {
contents.add(title);
}
public void removeBook(String title) throws BookException {
boolean result = contents.remove(title);
if (result == false) {
throw new BookException(title +
" not in cart.");
}
}
public List<String> getContents() {
return contents;
}
@Remove
public void remove() {
contents = null;
}
}
Life-Cycle Callback Methods
Methods in the bean class may be declared as a life-cycle callback method by annotating the
method with the following annotations:
javax.annotation.PostConstruct
javax.annotation.PreDestroy
javax.ejb.PostActivate
javax.ejb.PrePassivate
Life-cycle callback methods must return void and have no parameters.
@PostConstruct
methods are invoked by the container on newly constructed bean instances
after all dependency injection has completed and before the first business method is invoked on
the enterprise bean.
@PreDestroy
methods are invoked after any method annotated @Remove has completed, and
before the container removes the enterprise bean instance.
@PostActivate
methods are invoked by the container after the container moves the bean from
secondary storage to active status.
The cart Example
The Java EE 5 Tutorial · September 2007
660