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 availa开发者_JAVA技巧ble 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
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.
精彩评论