我正在尝试使用方法上的 @ModelAttribute Annontation 初始化对象。当调用 URL "/p/PPP/scope" 时会发生奇怪的事情。ProjectService 在调用@ModelAttribute 方法时似乎没有被实例化,但在调用 show() 方法时它就在那里。有人知道这有什么问题吗?
以下是日志语句:
12:32:19 [DEBUG] ScopeController - getProject() - loading project for 'PPP'
12:32:19 [DEBUG] ScopeController - getProject() - projectService initialized? null
12:32:21 [DEBUG] ScopeController - show() - projectService initialized? ...project.ProjectService@20f2442e
和来源:
@Controller
@RequestMapping("/p/{abbr}/scope")
@SessionAttributes("project")
public class ScopeController {
public static final String SHOW_PROJECT_PAGE = "/projects/scope/show";
private static final Logger log = LoggerFactory.getLogger(ScopeController.class);
@Autowired
private ProjectService projectService;
@ModelAttribute("project")
private Project getProject(@PathVariable(value = "abbr") String abbr) {
log.debug("getProject() - loading project for '{}'", abbr);
log.debug("getProject() - projectService initialized? {}", projectService);
// should call this method:
// return projectService.find(abbr);
return new Project();
}
@RequestMapping(method = RequestMethod.GET)
@Transactional
public String show() throws BindException {
log.debug("show() - projectService initialized? {}", projectService);
return SHOW_PROJECT_PAGE;
}
}