0

次のコントローラーからのコード スニペットでは、springSecurityService.currentUser にアクセスしようとすると、null ポインター例外が発生します。def 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
4

1 に答える 1

5

コントローラーまたはサービスへの他の Spring Bean の注入は、メソッド内ではなく、クラス レベルで行われます。

たとえば、コードは次のようになります。

@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
于 2016-11-08T03:17:36.207 に答える