可以在controller中使用WebApplicationContext获取
可以获取到web.xml中param-name的值
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param>
WebApplicationContext webApplicationContext =ContextLoader.getCurrentWebApplicationContext(); String contextConfigLocation = webApplicationContext.getServletContext().getInitParameter(" contextConfigLocation");
使用ServletContext获取
ServletContext servletContext=request.getSession().getServletContext(); WebApplicationContext webApplicationContext = (WebApplicationContext)servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);