代码之家  ›  专栏  ›  技术社区  ›  Amit Goyal

如何在加载Spring上下文后添加上下文参数

  •  2
  • Amit Goyal  · 技术社区  · 15 年前

    我想通过Spring配置添加一个servlet上下文参数/属性。我需要这个,因为我想在servlet上下文中添加的值只有在Spring容器加载之后才可用。我在servlet上下文中添加了这个值,因为我几乎在所有的.jsp文件中都需要这个值。

    基本上我需要一个与 this

    1 回复  |  直到 10 年前
        1
  •  9
  •   cjstehno    15 年前

    假设您使用的是正确配置的SpringWeb应用程序上下文,您可以尝试实现一个实现org.springframework.web.context.servletContextAware和org.springframework.beans.factory.initialingbean的bean,这样您就可以在AfterPropertieset方法实现中向servletContext添加您想要的内容。

    public class ServletContextInjector implements ServletContextAware,InitializingBean {
        private ServletContext servletContext;
    
        public void setServletContext(ServletContext sc){ this.servletContext = sc; }
    
        public void afterPropertiesSet(){
            servletContext.setAttribute( /* whatever */ );
        }
    }
    

    希望这有帮助。