I've encountered strange behavior with a domain class in my project: services that should be injected were null. I've became suspicious as why is that? Services are injected properly in other domain classes so why this one is different?
I've created an experiment. I've created empty LibraryService that should be injected and Book domain class like this:
Book has 4 explicit constructors. I want to check which constructor is injecting dependecies. This is my method that constructs Book objects and I called it in controller:
Output looks like this:
What do we see?
Empty constructor injects dependencies.
Constructor that invokes empty constructor explicitly injects dependencies.
Constructor that invokes parent's constructor explicitly does not inject dependencies.
Constructor without any explicit call declared does not call empty constructor thus it does not inject dependencies.
Constructor provied by Grails with a map as a parameter invokes empty constructor and injects dependencies.
Always explicitily invoke empty constructor in your Grail domain classes to ensure Dependency Injection! I didn't know until today either!