QuickTip (Grails): Grails LinkGenerator

The Problem

Recently, I was using the Grails Link Generator in a service.  I was using IntelliJ so as I added the dependency at the top of the class using the same name as the class. It looked something like this:

package com.example

import grails.web.mapping.LinkGenerator

class ExampleService {

    LinkGenerator linkGenerator
    String doSomething() {

It took me a long time to debug this. Luckily, I had written a test. There was no compilation error, but the test picked up that linkGenerator was null. Of course, I was stumped. I tried checking the mocks in the service and various other debugging strategies but had no luck.  I looked through tons of other examples online until I noticed the one small difference between my code here and theirs... the name!

The Solution

To make the LinkGenerator work correctly, it needs to be called grailsLinkGenerator exactly.

Hopefully, if you come across this post because you made the same error, it will save you some time. The same is true for some of the other special injectable services like grailsApplication and dataSource so it makes sense, but it was a little strange to me that the name of the injected service is not the same as the class.