IntelliJ IDEA java maven專案讀取配置檔案資訊 java.util.ResourceBundle 方式

海乐学习發表於2024-07-02

一、在main目錄下 新建 resources 目錄 並將其設為 資原始檔目錄

建立config.properties檔案

二、在pom.xml 中新增 下面程式碼 只這樣 打包後 jar 才能有配置檔案

    <resources>
      <resource>
        <filtering>true</filtering>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/**.properties</include>
          <include>**/**.xml</include>
        </includes>
        <!-- <targetPath>/resources</targetPath> -->
      </resource>
    </resources>

三、具體實現的程式碼

import java.util.ResourceBundle
//讀取引數
ResourceBundle resource = ResourceBundle.getBundle("config");
String strPbxIp = resource.getString("pbxIp");//
String strPbxPort = resource.getString("pbxPort");
String strApiPwd = resource.getString("apiPwd");
//System.out.println( "pbx "+strPbxIp+" " + strPbxPort +" "+ strApiPwd +"---------- ");
logger.info("pbx 引數: " + strPbxIp + " " + strPbxPort + " " + strApiPwd + "  ---------- ");

相關文章