Using grails service in domain class

grails

Solution

That should work. Note that since you're using 'def' you don't need to add it to the transients list. Are you trying to access it from a static method? It's an instance field, so you can only access it from instances.

The typical use case for injecting a service into a domain class is for validation. A custom validator gets passed the domain class instance being validated, so you can access the service from that:

static constraints = {
   name validator: { value, obj ->
      if (obj.testService.someMethod(value)) {
         ...
      }
   }
}

Problem

I want to use a service in my Grails application. However, it is always null. I am using Grails version 1.1. How can I solve this problem? Sample code: ``` class A{ String name; def testService; static transients=['testService'] } ``` Can I use a service inside a domain class?

Original source