springMVC继承了spring的servletcontext上下文,
所以, controller里的@Resource注入可以用以下替代
@Resource private IUserService userService;
替代:
WebApplicationContext ac1 = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); WebApplicationContext ac2 = RequestContextUtils.getWebApplicationContext(request); //IUserService userService = (IUserService)ac1.getBean("userService"); IUserService userService = (IUserService)ac2.getBean("userService"); List<User> users = userService.getAllUser(); request.setAttribute("users", users);