我正在尝试将一些旧版(基于 struts2)Web 应用程序从 Jboss 迁移到 Open-Liberty 服务器,我想知道是否有一种方法可以将 context-params(或过滤器 init-params)的值外部化web.xml,就像可以使用 server.xml 中的 ${} 语法或使用 eclipse microprofile 的 mpConfig 功能一样。 在原始项目中,参数值在构建时使用占位符替换注入到 web.xml 中,但根据 12 因素第三条建议,我更愿意在软件外部(例如在环境变量中)设置此值。 在我的具体情况下,我需要配置一个 servlet 过滤器和一个具有环境相关参数值的自定义标记库。
我已经尝试在 web.xml 中使用 ${} 语法,但没有成功:
...
<context-param>
<param-name>remincl.resource.provider</param-name>
<param-value>${remincl.resource.provider}</param-value>
</context-param>
...
上下文参数的运行时值为:“${remincl.resource.provider}”,而不是存储在环境变量中的实际值。
我认为 JEE 规范不允许这种行为,但我想知道 open-liberty 是否提供了一些额外的功能来解决这个问题。否则我必须在构建时继续注入值(或更改过滤器和标签库的配置策略)。
实现此目的的 JavaEE 标准方法是使用
javax.servlet.ServletContextListener
。
例如:
@WebListener
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
// Get the context value from wherever is most convenient:
// System.getProperty(), System.getenv(), MP Config API, etc
String value = System.getProperty("remincl.resource.provider");
event.getServletContext().setInitParameter("remincl.resource.provider", value);
}
@Override
public void contextDestroyed(ServletContextEvent event) {}
}
我尝试了类似的方法来修改上下文参数,但 setInitparameter() 不包含新参数。
试验 1:我尝试删除 web.xml 中已配置的参数而不是抛出异常 无法打开 ServletContext 资源 [/WEB-INF/applicationContext.xml]"}}"
试验 2:从 web.xml 中删除 ContextLoaderListener,然后抛出 java.lang.IllegalStateException