web.xml读取config.properties

来源:互联网 发布:怎样做软件开发 编辑:程序博客网 时间:2024/05/20 19:32

因为项目需要,一个特别诡异的需要。

    <context-param>
        <param-name>xxxxxx</param-name>
        <param-value>${yyyyyyy}</param-value>
    </context-param>


想在web.xml这么玩, 结果打死获取不到数据,网上搜下,只见问题不见答案。

在sping.xml中经常这么玩,不明白到 web.xml中怎么就取不到了。


问题还是要解决啊,换个思路,在ContextLoaderListener中实现吧。


重写 ContextLoaderListener,初始化的时候 往上下文中塞参数

    public void contextInitialized(ServletContextEvent event) {
        this.contextLoader = createContextLoader();
        if (this.contextLoader == null) {
            this.contextLoader = this;
        }
        this.contextLoader.initWebApplicationContext(event.getServletContext());
       
        event.getServletContext().setInitParameter("xxxxxxx", PropertyUtil.getString("yyyyyyy"));
    }


测试下,居然也是可以的,至于有没隐患,嗯,再观察观察吧。


原创粉丝点击