【发布时间】:2011-02-13 04:54:12
【问题描述】:
我希望能够在启动 tomcat(apache commons 配置库)时为 webapp 加载我的配置,这是一种可能的方式:
public class MyAppCfg implements javax.servlet.ServletContextListener {
private ServletContext context = null;
@Override
public void contextInitialized(ServletContextEvent event) {
try{
this.context = event.getServletContext();
XMLConfiguration config = new XMLConfiguration("cfg.xml");
config.setReloadingStrategy(new FileChangedReloadingStrategy());
this.context.setAttribute("mycfg", config);
}
catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
this.context = null;
}
}
web.xml
<listener>
<listener-class>mypackage.MyAppCfg</listener-class>
</listener>
然后在 webapp 中通过
访问它们this.cfg = (XMLConfiguration) servletRequest.getAttribute("mycfg");
【问题讨论】:
标签: configuration tomcat web-applications startup