java 读取 .conf结尾文件
.conf文件结尾的一般都是使用HOCON(Human-Optimized Config Object Notation)格式,这种格式如下
mongo{
mongo_url="http://localhost"
mongo_db="tga"
mongo_task="task"
mongo_settings="settings"
}
如果需要从resources目录下加载,则必须命名为 application.conf
maven依赖如下
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.3.0</version>
</dependency>
读取如下
// 如果在resource目录下 则不需要指定路径
Config conf=ConfigFactory.load();
String url = conf.getString("mongo.mongo_url")
自定义路径读取
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Mongo {
private Map<String,Object> mongo;
}
class TestConf{
public static void main(String[] args) {
Config config=ConfigFactory.parseFile(new File("src/main/resources/test.conf"));
Mongo mongo = ConfigBeanFactory.create(config, Mongo.class);
System.out.println(mongo);
}
}
conf文件
mongo{
mongo_url="http://localhost"
mongo_db="tga"
mongo_task="task"
mongo_settings="settings"
}