您的当前位置:首页正文

Hutool-读取配置文件中的配置

2024-07-11 来源:汇智旅游网
Hutool-读取配置⽂件中的配置

1、读取配置⽂件:example.setting

[demo]

ds.setting.path = config/other.settingdriver = com.mysql.jdbc.Driver

url = jdbc:mysql://fedora.vmware:3306/extractoruser = root${demo.driver}pass = 123456port = 3306

2、测试代码:

import cn.hutool.setting.Setting;

import cn.hutool.setting.dialect.Props;

public class SettingTest {

public static void main(String[] args) { //⽅式⼀、使⽤Setting类 //读取配置⽂件所有的内容

Setting setting = new Setting(\"example.setting\"); System.out.println(setting);

//{demo={ds.setting.path=config/other.setting, driver=com.mysql.jdbc.Driver, url=jdbc:mysql://fedora.vmware:3306/extractor, user=root${demo.driver}, pass=123456, port=3306}} //读取配置⽂件中键为driver的值

String driver1 = setting.getStr(\"driver\",\"默认值\"); System.out.println(driver1); //默认值 //读取分组为demo⾥⾯的键为driver的值

Object driver = setting.getByGroup(\"driver\",\"demo\"); System.out.println(driver); //com.mysql.jdbc.Driver //读取分组为demo⾥⾯的键为driver的值

String str = setting.getStr(\"driver\", \"demo\", \"默认值\"); System.out.println(str); //com.mysql.jdbc.Driver

String driver2 = setting.getWithLog(\"driver\"); System.out.println(driver2); //null

String byGroupWithLog = setting.getByGroupWithLog(\"driver\", \"demo\"); System.out.println(byGroupWithLog); //com.mysql.jdbc.Driver Setting demo = setting.getSetting(\"demo\"); System.out.println(demo);

//{={ds.setting.path=config/other.setting, driver=com.mysql.jdbc.Driver, url=jdbc:mysql://fedora.vmware:3306/extractor, user=root${demo.driver}, pass=123456, port=3306}} //在配置⽂件变更时⾃动加载 setting.autoLoad(true);

//⽅式⼆、使⽤Props类

//使⽤Props类,props继承了properties

Props props = new Props(\"example.setting\"); String driver3 = props.getProperty(\"driver\");

System.out.println(driver3); //com.mysql.jdbc.Driver String driver4 = props.getStr(\"driver\");

System.out.println(driver4); //com.mysql.jdbc.Driver }}

因篇幅问题不能全部显示,请点此查看更多更全内容