Craftercms:如何在Spring中定义单例服务bean(写在groovy)?

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

我编写了一个从 Rest 控制器调用的服务 (ZipService)。 我只需要将服务实例化一次并在每次调用中重用它。

有没有办法在配置中定义一个写在groovy中的单例服务bean,并使用applicationContext对象访问它。

当然,您可以使用站点自己的 Spring 应用程序上下文来定义单例 bean。然后在您的脚本中,您可以执行 applicationContext.mybean (mybean 是您的 bean 名称)来检索单例。您可以在 http://docs.craftercms.org/en/latest/developers/custom-services-and-controllers.html and on defining Spring beans in http://docs.craftercms.org/en/latest/site-administrators/engine-site-configuration.html.

中获得有关 applicationContext 变量的更多信息