Reputation: 39887
In the following code snippet from a controler I get a null pointer exception when trying to access springSecurityService.currentUser. I would expect def springSecurityService to automatically inject the service, what am I missing?
@Transactional(readOnly = true)
@Secured('ROLE_USER')
class TaskController {
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
def springSecurityService
def user = springSecurityService.currentUser
Upvotes: 0
Views: 221
Reputation: 24776
Injecting other Spring beans into your controllers or services is done at the class level and not within a method.
For example, your code should look like:
@Transactional(readOnly = true)
@Secured('ROLE_USER')
class TaskController {
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
def springSecurityService // inject the bean here, at the class level
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
def user = springSecurityService.currentUser
Upvotes: 5