2016-11-08 1 views
0

컨트롤러의 다음 코드 단편에서 springSecurityService.currentUser에 액세스하려고 할 때 널 포인터 예외가 발생합니다. 나는 def springSecurityService가 자동으로 서비스를 삽입 할 것을 기대할 것이다. 나는 무엇을 놓치고 있는가?springSecurityService의 주입이 작동하지 않습니다.

@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 

답변

5

컨트롤러 나 서비스에 다른 스프링 빈을 주입하는 것은 메소드가 아닌 클래스 레벨에서 수행됩니다.

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

예를 들어, 코드가 같아야합니다

관련 문제