gpt4 book ai didi

grails - springSecurityService 在基本 Controller 中为空

转载 作者:行者123 更新时间:2023-12-05 00:02:51 24 4
gpt4 key购买 nike

这是一个相当奇怪的问题,我已经解决了一段时间,所以我要疯了。

我有一个 Controller 扩展另一个 Controller ,所以我可以让多个 Controller 继承一个方法,它们是这样的:

class EventController extends EventAwareController {

def springSecurityService

def edit = {
// this line prints out principal id
println springSecurityService.principal.id
def eventInstance = getAuthorizedEventById(params.id)
if (!eventInstance) {
flash.message = "${message(code: 'event.not.found.message')}"
redirect(action: "list", controller: "event")
return false
}
}

class EventAwareController {
def eventService
def springSecurityService

def getAuthorizedEventById(def eventId) {
def event
if (eventId) {
// this springSecurityService is null and throws an error
event = eventService.findAuthorizedEvent(eventId, springSecurityService.principal.id)
if (event) {
session.eventId = eventId
}
}
return event
}

}

EventAwareController 正在抛出:

java.lang.NullPointerException: Cannot get property 'principal' on null object at com.ticketbranch.EventAwareController.getAuthorizedEventById(EventAwareController.groovy:14)



但是我在 EventController 中的 prinln 语句打印主体 ID 没有任何问题?!?那么springSecurityService在EventAwareController中被注入(inject)为null?

有任何想法吗?建议?谢谢。

最佳答案

您在两个类中都有该字段,这是使用 Groovy 时的一个问题。 Grails 中的依赖注入(inject)通常像您正在做的那样完成,使用 def <beanname> .这是一个公共(public)字段,因此 Groovy 为其创建了一个公共(public) getter 和 setter,并将该字段设为私有(private)。没有使用 getter,但是 Spring 看到了 setter,并且由于 bean 被配置为按名称(而不是按类型)连接,因此 bean 被注入(inject),因为 setter 名称( setSpringSecurityService )和 bean 名称之间存在匹配.

因为你有两次,你有两个二传手和一个胜利,所以你将在一个类中的私有(private)字段有一个空值。

但是像任何公共(public)(或 protected )属性一样,依赖注入(inject)是继承的,因此只需将其从所有子类中删除并将其保留在基类中。

关于grails - springSecurityService 在基本 Controller 中为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7605827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com