2017-10-13 138 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