SpringSecurityService function is null in the base controller

This is a rather strange problem, and I have been on it for a while, so I'm going crazy.

I have a controller that extends another controller, so I can have several controllers inheriting the method, and they go something like this:

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 throws:

java.lang.NullPointerException: cannot get property 'main' on null object in com.ticketbranch.EventAwareController.getAuthorizedEventById (EventAwareController.groovy: 14)

but my prinln operator in EventController prints the main id without any problems?!? So, is SpringSecurityService introduced as null in the EventAwareController?

Any ideas? offers? Thanks.

+4
source share
1 answer

You have a field in both classes, and this is a problem when using Groovy. Injection injection in Grails is usually done the same way as you do with def <beanname> . This is a public field, so Groovy creates a public getter and setter for it and makes the field private. A getter is not used, but Spring sees the installer, and since beans are configured to connect by name (as opposed to type), a bean is entered, since there is a match between the name setter ( setSpringSecurityService ) and bean.

Since you have this twice, you have two setters and one wins, so you will have an empty value for the private field in the same class.

But, like any public (or protected) property, dependency injection is inherited, so just remove it from all your subclasses and leave it in the base class.

+7
source

All Articles