Инъекция springSecurityService не работает
В следующем фрагменте кода из контроллера я получаю исключение нулевого указателя при попытке доступа к springSecurityService.currentUser. Я ожидаю, что 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
1 ответ
Внедрение других bean-компонентов Spring в ваши контроллеры или службы выполняется на уровне класса, а не внутри метода.
Например, ваш код должен выглядеть так:
@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