迁移设置:在 context.xml 从 Tomcat 9 到 Wildfly 14.0.1 Final

Migrating setting: in context.xml from Tomcat9 to Wildfly 14.0.1Final

如何将某些配置从 Tomcat 应用程序服务器的 context.xml 移动到 Wildfly?我真的需要来自环境元素的数据。

context.xml 包含如下内容:

<Context>

    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>


    <Environment name="some.very.important.config.path" value="C:\path\to\the\config\folder"

         type="java.lang.String" />


</Context>

如何在 Wildfly 应用程序服务器中实现此功能?

更新:

我必须使用 JNDI(目前),因为它是别人编写的应用程序。

到达可注入配置路径的代码如下所示。

        env = (Context) new InitialContext().lookup("java:comp/env");
        configPath = (String) env.lookup("some.very.important.config.path");
  • <WatchedResource>WEB-INF/web.xml</WatchedResource>
    默认情况下,WildFly 会监视此文件中的更改

  • <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    在 WildFly 中不相关。 WEB-INF/jboss-web.xml文件用于类似用途,默认也被监视

  • <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
    在 WildFly

  • 中不相关
  • <Environment name="some.very.important.config.path" value="C:\path\to\the\config\folder" type="java.lang.String" />
    编写一个名为(例如)configure-wildfly.cli 的文本文件,内容如下

    # Execute offline
    embed-server --server-config=standalone.xml
    
    # Add system properties
    /system-property=some.very.important.config.path:add(value=C:\path\to\the\config\folder)
    /system-property=some.other.important.config.value:add(value=foobar)
    
    # Bind an entry into the naming service
    /subsystem=naming/binding=java\:global\/config\/important\/path:add(binding-type=simple, type=java.lang.String, value="C:\path\to\the\config\folder")
    
    stop-embedded-server
    

    然后 运行 它与:

    ${WILDFLY_HOME}/bin/jboss-cli.sh --file=configure-wildfly.cli
    

    像这样编写脚本可以很容易地从它的基本设置重建你的服务器。如果需要,您可以对该文件进行源代码控制。

访问系统属性:

 String configPath = System.getProperty("some.very.important.config.path");

在 JNDI 中查找值:

 Context ctx = new InitialContext();
 String configPath = (String)ctx.lookup("java:global/config/important/path");

或者注入它

 @Resource(lookup="java:global/config/important/path")
 private String configPath;