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
ApplicationStateBean EJB and add the @Startup annotation to the EJB as shown below. This results in the singleton ...