当我将springSecurityService注入Grails用户域类时,它始终为null:
我试过了:
def springSecurityService
static transients = ['springSecurityService']
或
def transient springSecurityService
static transients = ['springSecurityService']
但是一样,所以我不能使用springSecurityService.encodePassword(密码)
有什么想法吗?为什么不注射?
我找到了一个决定。我在参数化构造函数中注释掉了对this()的调用。
User(String name, String email) {
//this()
this.name = name
this.email = email
}
这打破了依赖注入。
答案 0 :(得分:2)
这是记录here(参见以&#34开头的注释;在可行的情况下,生成的域类包括参数化构造函数。")。
如果需要,可以删除参数化构造函数,但是如果不this()
调用生成的默认构造函数而不丢失依赖项注入,则无法使用它们。
IntelliJ错误地将对this()
的调用标记为无效,我将其报告为问题跟踪器中的错误。它将在即将发布的15版本中修复。
答案 1 :(得分:0)
到目前为止,在intellij 15 EAP中没有解决任何问题!
但可以通过简单地添加默认构造函数
来解决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
}
...
}
然后Intellij不再提出该错误。