views:

211

answers:

1

I want to add a servlet context parameter/attribute through spring configuration. I need this because the value I want to add in servlet context is available only after spring container loads. I'm adding the value inside the servlet context as I need the value in almost all my .jsp files.

Essentially I need a mechanism opposite to this

+1  A: 

Assuming you are using a properly configured Spring Web Application Context, you could try implementing a bean that implements org.springframework.web.context.ServletContextAware and org.springframework.beans.factory.InitializingBean so that you could add whatever you want to the ServletContext in the afterPropertiesSet method implementation.

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

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

    public void afterPropertiesSet(){
        servletContext.setAttribute( /* whatever */ );
    }
}

Hope this helps.

cjstehno
Thanks for the answer. This is exactly what I was looking for.
Amit Goyal