在src/main/resources目录下
1. 使用java.util.Properties
def loadProperties():Unit = { val properties = new Properties() val path = Thread.currentThread().getContextClassLoader.getResource("test.properties").getPath //文件要放到resource文件夹下 properties.load(new FileInputStream(path)) // println(properties.getProperty("ddd"))//读取键为ddd的数据的值 // println(properties.getProperty("ddd","没有值"))//如果ddd不存在,则返回第二个参数 // properties.setProperty("ddd","123")//添加或修改属性值 println(properties.getProperty("db1")) }
2. 使用 com.typesafe.config
libraryDependencies += "com.typesafe" % "config" % "1.3.0 import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigFactory
val conf = ConfigFactory.load( ) //默认加载配置文件
//ConfigFactory.parseFile( new File("test.properties") )
println( conf.getString( "db2.pass" ) )
ref: http://n3xtchen.github.io/n3xtchen/scala/2016/07/14/scala-typesafe-config
https://github.com/jasonqu/reading-notes/blob/master/_content/_drafts/typesafe-config.md
https://github.com/typesafehub/config#scala-port