Java - 设置上下文属性(ServletContextListener)

Ada*_*ham 0 java

我在webapplication启动阶段(contextInitialized())读取了一个属性文件,我开始考虑如何使这些设置对servlet"可见".我是否需要遍历键并将每个键添加到上下文中,如下所示

Iterator i = settings.keySet().iterator();
while (i.hasNext()) {
    key = (String) i.next();
    value = (String) settings.get(key);
    context.setAttribute(key, value);
}
Run Code Online (Sandbox Code Playgroud)

还是有更好的方法?

谢谢!

/亚当

too*_*kit 5

为什么不将整个内容存储在servlet上下文中?

context.setAttribute("mySettings", settings);
Run Code Online (Sandbox Code Playgroud)

setAttribute的签名是:

public void setAttribute(String name, Object object)
Run Code Online (Sandbox Code Playgroud)