[wp_ad_camp_1]
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
.
1 2 3 4 5 6 7 8 | package com.turreta.groovy @Singleton class SingletonPerson { String lastName String firstName } |
When we try to instantiate SingletonPerson
class:
[wp_ad_camp_2]
def sPerson = new SingletonPerson(firstName: "John", lastName: "Doe")
A run-time exception is thrown:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | xception in thread "main" java.lang.RuntimeException: Can't instantiate singleton com.turreta.groovy.SingletonPerson. Use com.turreta.groovy.SingletonPerson.instance at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83) at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247) at com.turreta.groovy.SingletonPerson.<init>(SingletonPerson.groovy) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83) at org.codehaus.groovy.runtime.callsite.ConstructorSite$NoParamSite.callConstructor(ConstructorSite.java:123) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247) at com.turreta.groovy.Main.main(Main.groovy:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) |
Sample Usage
[wp_ad_camp_3]
1 2 3 4 5 6 7 8 9 10 | SingletonPerson samePeron = SingletonPerson.instance println "${samePeron.firstName}" println "${samePeron.lastName}" samePeron.firstName = "Jim" samePeron.lastName = "Ramos" println "${samePeron.firstName}" println "${samePeron.lastName}" |
Outputs:
1 2 3 4 | null null Jim Jim |
References
[wp_ad_camp_4]