【问题标题】:Reading System Properties in Web.XML在 Web.XML 中读取系统属性
【发布时间】:2018-03-25 15:43:40
【问题描述】:

为了从 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);
        }
    }

}

然后我将相应的监听器添加到 web.xml:

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

代码被正确调用,我可以通过调试来验证系统变量是否设置正确。但是,我的 web.xml 的属性似乎没有被替换/解释。即使在重新启动服务器和/或重新发布之后,以下参数仍会评估为 ${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>

关于这个问题的所有其他主题都没有用,因为没有解决方案对我有用。如何将 web.xml 参数替换为存储在属性文件中的值?

【问题讨论】:

  • 嗨 Knight 有很多方法可以解析 xml,如 JaxB、DocumentBuilders、Jsoup 等,您可以使用 DocumentBuilder 读取 xml 并从 java 设置值
  • 请通过此链接
  • 嗨,Pradeep,谢谢,但我认为这个网站没有任何帮助;我不想解析任意 XML 代码并替换值。 web.xml 是 Web 应用程序的配置文件,应该通过在单独的 .properties 文件中找到的值进行某种“参数化”。

标签: java xml jsf jboss web.xml


【解决方案1】:

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

【讨论】:

  • 更准确地说,参数是&lt;jboss-descriptor-property-replacement&gt;true&lt;/jboss-descriptor-property-replacement&gt;
猜你喜欢
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
  • 2011-02-26
  • 1970-01-01
  • 2014-12-15
  • 2015-03-02
  • 1970-01-01
相关资源
最近更新 更多