Grails 如何从域 class 的静态方法调用服务?

Grails How to call service from static method of domain class?

我在我的域 class 上有一个静态方法,想将所有业务逻辑从域 class 定义中取出到服务中,但我无法在中调用该服务域 class 静态方法,因为服务本身是在实例上定义的,而不是域 class.

最好的解决方法是什么?

例如

class Foo {
   def fooService
   Integer count =0
   String name

   static void updateFoo(String name) {
      def foo = FindByName(name)
      fooService.beforeUpdateProcess(foo)   //fooService unavailable here  
      foo.count+=1
      foo.save()
   }

}

由于服务是 beans,因此您可以像访问应用程序上下文中的任何 bean 一样访问它们。 Grails 有一个 Holders 助手。:

FooService fooService = grails.util.Holders.applicationContext.getBean('fooService') as FooService