WebApplicationContext webApplicationContext =
WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
由于spring是注入的對象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 對象:
WebApplicationContext webApplicationContext =
(WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
(WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
事實上WebApplicationContextUtils.getWebApplicationContext方法就是使用上面的代碼實現(xiàn)的,建議使用上面上面的靜態(tài)方法