Controlling the initialization process

In this recipe, we will examine how the initialization and termination of a singleton is controlled. We will use the @Startup annotation to force eager initialization of the singleton. The annotation does not have any arguments and is simple to use. We will use the @PostConstruct annotation to illustrate the process.

How to do it...

Create a Java EE application called ApplicationIntializationExample. In the EJB module add a package called packt and a singleton bean called ApplicationStateBean. In the WAR module create a package called servlet and add a servlet called ApplicationServlet.

Create the ApplicationStateBean EJB and add the @Startup annotation to the EJB as shown below. This results in the singleton ...

Get EJB 3.1 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.