Creating a singleton bean

We will use a singleton to support a game application. A PlayerBean will be created to support the attributes of a player. We will assume that our game will never have more than one player so a singleton is an appropriate choice for this type of bean.

Getting ready

The process of creating a singleton bean uses two steps:

  1. Use the @Singleton annotation to designate the bean as a singleton
  2. Add appropriate methods to the class

    These methods will reflect the functionality desired for the singleton.

How to do it...

Begin this recipe by creating a Java EE application called SingletonExample. Within the EJB module, create a package called packt and add the singleton PlayerBean. In the WAR module, create a servlet package and add a

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.