加载spring上下文后如何添加上下文参数

Ami*_*yal 2 java spring-mvc

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

基本上我需要一个与相反的机制

cjs*_*hno 9

假设您正在使用正确配置的Spring Web应用程序上下文,您可以尝试实现一个实现org.springframework.web.context.ServletContextAware和org.springframework.beans.factory.InitializingBean的bean,以便您可以向ServletContext添加任何内容.在afterPropertiesSet方法实现中.

public class ServletContextInjector implements ServletContextAware,InitializingBean {
    private ServletContext servletContext;

    public void setServletContext(ServletContext sc){ this.servletContext = sc; }

    public void afterPropertiesSet(){
        servletContext.setAttribute( /* whatever */ );
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.