Alex Shwarc
Alex Shwarc

Reputation: 885

Grails springSecurityService injection in domain class (NULL)

when I inject springSecurityService into Grails User domain class it is always null:

I tried:

def springSecurityService
static transients = ['springSecurityService']

or

def transient springSecurityService
static transients = ['springSecurityService']

but it the same, so I cannot use springSecurityService.encodePassword(password)

Any ideas? Why it's not injected?

I found a decision. I commented out the call to this() in the parameterized constructor.

User(String name, String email) {
    //this()
    this.name = name
    this.email = email
}

and this breaks a Dependency Injection.

Upvotes: 1

Views: 952

Answers (2)

Pascal
Pascal

Reputation: 2164

So far nothing is solved in intellij 15 EAP!

But it could be solved by simply adding a default constructor

class User implements Serializable {

private static final long serialVersionUID = 1

transient springSecurityService

String username
String password
boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired

User() {
}

User(String username, String password) {
    this()
    this.username = username
    this.password = password
}
...
}

Then Intellij does not bring up that error anymore.

Upvotes: 0

Burt Beckwith
Burt Beckwith

Reputation: 75671

This is documented here (see the note starting with "Where practical, the generated domain classes include a parameterized constructor.").

You can remove the parameterized constructors if you want, but you can't use them without the this() call to the generated default constructor without losing dependency injection.

IntelliJ incorrectly flags the call to this() as invalid and I reported it as a bug in their issue tracker. It's due to be fixed for the upcoming version 15 release.

Upvotes: 2

Related Questions