2017-10-13 59 views
0

为了从JBoss EAP 7服务器的web.xml中访问存储在文件src/resources/settings.properties中的全局值,我从类似的堆栈溢出中实现了以下类话题:在Web.XML中读取系统属性

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参数?

+0

嗨骑士有很多方法可以解析xml,比如JaxB,DocumentBuilders,Jsoup等,您可以使用DocumentBuilder读取xml并设置java的值 – Pradeep

+0

https://www.mkyong.com/java/如何修改xml-file-in-java-dom-parser/ – Pradeep

+0

请通过这个链接 – Pradeep

回答

0

现在,我必须在Wildfly standalone.xml中设置一个与变量替换为true的参数(假)相关的参数。

+0

更准确地说,参数是 true