Amit Goyal
Amit Goyal

Reputation: 444

How to add context parameter after loading spring context

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

Upvotes: 2

Views: 5561

Answers (1)

cjstehno
cjstehno

Reputation: 13994

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.

Upvotes: 9

Related Questions