Groovy – @Singleton example

This post shows a simple example of Groovy class that uses the @Singleton annotation.

Using @Singleton

@Singleton is a Groovy annotation in groovy.lang package. We do not need to import it as it is already implicitly imported by Groovy.

When we try to instantiate SingletonPerson class:

def sPerson = new SingletonPerson(firstName: "John", lastName: "Doe")

A run-time exception is thrown:

Sample Usage




Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert