【问题标题】:Grails springSecurityService injection in domain class (NULL)域类中的 Grails springSecurityService 注入(NULL)
【发布时间】:2015-12-08 21:12:42
【问题描述】:

当我将 springSecurityService 注入 Grails 用户域类时,它始终为空:

我试过了:

def springSecurityService
static transients = ['springSecurityService']

def transient springSecurityService
static transients = ['springSecurityService']

但它是一样的,所以我不能使用 springSecurityService.encodePassword(password)

有什么想法吗?为什么不注入?

我找到了一个决定。我在参数化构造函数中注释掉了对 this() 的调用。

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

这会破坏依赖注入。

【问题讨论】:

    标签: grails groovy spring-security grails-domain-class


    【解决方案1】:

    这已记录在 here(请参阅以“在实际情况下,生成的域类包含参数化构造函数”开头的注释。)。

    如果需要,您可以删除参数化构造函数,但如果没有对生成的默认构造函数的 this() 调用,则无法使用它们,而不会丢失依赖注入。

    IntelliJ 错误地将对 this() 的调用标记为无效,我在他们的问题跟踪器中将其报告为错误。它将在即将发布的第 15 版中修复。

    【讨论】:

    • 链接已损坏,请重新加载
    【解决方案2】:

    到目前为止,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 不再显示该错误。

    【讨论】:

    • 是的,我也是。我们正在等待 intellij 15 发布。
    猜你喜欢
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多