spring controller中获取webApplicationContext

来源:互联网 发布:网络销售茶叶好做吗 编辑:程序博客网 时间:2024/06/05 10:58

可以在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);

                                             
0 0
原创粉丝点击