Grails plugin dependency injection failing in domain class and controller

403 views Asked by At

| Grails Version: 3.0.7

| Groovy Version: 2.4.4

| JVM Version: 1.8.0_51

I am trying to install a grails plugin : Slug Generator 0.5 (https://grails.org/plugin/slug-generator)

However, the service dependency injection is not working correctly in either domain classes or services and is always a null reference.

This is not the first time I have experienced plugins not successfully injecting services : Grails Geocode plugin dependency injection issue

Basically, it seems I'm at a point where I cannot use some plugins within my application as I cannot count on service dependency injection working.

If anybody could offer some insight, i'd be most appreciative.

I'm following the following example code :

class Dummy {
    def slugGeneratorService

    String name
    String slug = ""

    def beforeInsert() {
        this.slug = slugGeneratorService.generateSlug(this.class, "slug", name)
    }

    def beforeUpdate() {
        if (isDirty('name')) {
            this.slug = slugGeneratorService.generateSlug(this.class, "slug", name)
        }
    }
}

Here is an example error log :

Caused by: java.lang.NullPointerException: Cannot invoke method generateSlug() on null object

One thing I've noticed is that at the command line, the following returns nothing :

$ grails list-plugins | grep slug

However, If I search for another plugin, I do get a result :

$ grails list-plugins | grep joda

joda-time

I can clearly see (via IntelliJ) that the slug generator plugin is in the classpath and I can actually access all the source via the External libraries.

Maybe that's a hint to the problem?

1

There are 1 answers

0
Robin M On

OK, this seems to be down to me stupidly trying to use a grails 2.x plugin in a grails 3.x plugin.

There are various steps to go through to upgrade a plugin from 2.x to 3.x all detailed within the grails documentation.

My immediate solution was to simply create a new service and copy the code from the plugin into my application. Worked just fine.

Grails 3.x plugins : https://bintray.com/grails/plugins Grails 2.x plugins : https://grails.org/plugins/

It's not obvious unless you navigate via the grails site. If you come in for example from Google directly to a plugin page, compatibility is shown as 2.5.x >

However, this actually seems to mean greater than 2.5.x but less than 3.x

Hope this helps should anyone else encounter this.