IntelliJ IDEA java maven项目读取配置文件信息 java.util.ResourceBundle 方式

一、在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 + "  ---------- ");

 

posted @ 2024-07-02 11:35  海乐学习  阅读(9)  评论(0编辑  收藏  举报