首页 > 解决方案 > 来自 Spring 应用程序上下文的 Servlet 上下文属性

问题描述

我正在尝试摆脱真正旧的 servlet 并在它们的位置添加临时服务。

这是否可以从应用程序上下文中获取与我之前从 servlet 上下文中获取的数据相同的数据?

更具体地说,我试图servletContext.getAttribute("org.directwebremoting.ContainerList")从应用程序上下文中获取属性,但ApplicationContext没有getAttribute方法。

标签: javaspringspring-mvcservletsapplicationcontext

解决方案


Spring 使用Servlets 作为支持技术。因此,您只需要获取ServletContext与之相关的内容Servlet并检索属性。

对于根上下文,使用WebApplicationContext. 该接口getServletContext()向通用 ApplicationContext 接口添加了一个方法。

Object attribute = webApplicationContext
                .getServletContext()
                .getAttribute("org.directwebremoting.ContainerList");

推荐阅读