Isai Ulate Sancho Isai Ulate Sancho - 1 month ago 14
Groovy Question

Craftercms: How to define a singleton service bean (written in groovy) in Spring?

I wrote a service (ZipService) which is being called from the Rest controllers.
I need the service to be instantiated only once and reuse it in every call.

Is there a way to define a singleton service bean written in groovy in the configuration and access it using the applicationContext object.

Answer

Sure, you can use the site's own Spring application context to define singleton beans. Then in your script you can do applicationContext.mybean (mybean being your bean name) to retrieve the singleton. You can get more information on the applicationContext variable in http://docs.craftercms.org/developers/custom-services-and-controllers.html and on defining Spring beans in http://docs.craftercms.org/site-administrators/engine-site-configuration.html.