开发者

How to add context parameter after loading spring context

开发者 https://www.devze.com 2023-01-02 19:49 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消