testway

java项目需要一些配置,配置放置目录如:/src/main/resources;

如果没有这个文件夹,需要右键项目>new>source folder > Folder name 填写/src/main/resources 点击确定,就生成了该目录。

在该目录下面添加一个文件:system-config.properties

在该文件里面配置如:

userName = nameabc
password = a0a0a0

 

添加一个常量类,关联配置文件

public class Constants {

	public final static String PROPS_FILE_PATH = "/system-config.properties";

	
}

  

项目中新建一个PropsUtil.java 类,类里面设置全局变量和添加方法:

//全局变量

static private String props_file_path = Constants.PROPS_FILE_PATH ;

static private Properties props;


// 根据key读取value public static String readValue(String key) throws IOException { if (props == null) { init(); } String value = props.getProperty(key); return value; }

  项目中读取配置userName 就需要这样读取

PropsUtil.readValue("userName")


分类:

技术点:

相关文章:

  • 2021-11-10
  • 2021-11-04
  • 2021-04-15
  • 2021-04-07
  • 2021-12-10
  • 2022-01-26
  • 2021-06-05
猜你喜欢
  • 2021-10-16
  • 2021-07-11
  • 2022-12-23
  • 2021-11-17
  • 2022-12-23
  • 2021-12-12
  • 2021-11-18
相关资源
相似解决方案