简体   繁体   English

读取Web.XML中的系统属性

[英]Reading System Properties in Web.XML

in order to access global values stored in the file src/resources/settings.properties from web.xml on a JBoss EAP 7 Server, I implemented the following class from a similar Stack Overflow topic: 为了从JBoss EAP 7服务器上的web.xml访问存储在文件src / resources / settings.properties中的全局值,我从类似的Stack Overflow主题实现了以下类:

public class ConfigurationWebFilter implements ServletContextListener {
        protected static final Properties properties = new Properties();

   @Override
   public void contextInitialized(final ServletContextEvent event){
        try {
            try (InputStream stream = new FileInputStream("/settings.properties")) {
                properties.load(stream);
            }

             for (String prop : properties.stringPropertyNames())
                {
                 if (System.getProperty(prop) == null)
                 {
                     System.setProperty(prop, properties.getProperty(prop));
                 }
              }
        } catch (IOException ex) {
            logger.error("Failed loading settings from configuration file for web.xml", ex);
        }
    }

}

Then I added the according listener to web.xml: 然后我将相应的侦听器添加到web.xml:

  <listener>       
  <listener-class>
     com.product.util.ConfigurationWebFilter
  </listener-class>
  </listener>  

The code gets called properly and I can verify by debugging that the system variables get set correctly. 代码被正确调用,我可以通过调试来验证系统变量是否已正确设置。 However, the properties of my web.xml do not seem to be replaced/interpreted. 但是,我的web.xml的属性似乎没有被替换/解释。 The following parameter does still evaluate to ${serverName}, even after restarting the server and/or republishing: 即使重新启动服务器和/或重新发布后,以下参数的结果仍为$ {serverName}:

<filter>
  <filter-name>CAS Authentication Filter</filter-name>
  <filter-class>(...)</filter-class>
  <init-param>
    <param-name>serverName</param-name>
    <param-value>${serverName}</param-value>
  </init-param>
</filter>

All the other topics on this issue were of no use because no solution worked for me. 关于此问题的所有其他主题都没有用,因为没有解决方案对我有用。 How can I replace web.xml parameters by values stored in a properties file? 如何用存储在属性文件中的值替换web.xml参数?

现在可以工作了,我不得不在Wildfly standalone.xml中将与变量替换相关的参数设置为true(是false)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM