在以下来自控制器的代码片段中,我在尝试访问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
答案 0 :(得分:5)
将其他Spring bean注入您的控制器或服务是在类级别而不是在方法中完成的。
例如,您的代码应如下所示:
List<String> weekdays = Arrays.asList("sun", "mon", "tue");
long sunTemp = 0;
long monTemp = 0;
for(String day : weekdays){
if(day.equals("sun"){
sunTemp = getSunTemp();
}else if(day.equals("mon")){
monTemp = getMonTemp();
// use sunTemp ex: totalTemp = sunTemp + monTemp
}
//...more code
}