scala的maven项目读取配置文件
来源:互联网 发布:443端口攻击 编辑:程序博客网 时间:2024/06/05 00:11
scala的maven项目读取src/main/resources目录下的资源文件该如何读取呢?
下面提供一种默认的和一种自定义的:
对于application.conf配置文件,是默认的配置文件:
application.conf内容如下:
mysql {
url : "jdbc:mysql://192.168.76.14:3306/ibigdata?useUnicode=true&characterEncoding=UTF-8",
dbtable: "record_test",
user: "root",
password:"root"
}
url : "jdbc:mysql://192.168.76.14:3306/ibigdata?useUnicode=true&characterEncoding=UTF-8",
dbtable: "record_test",
user: "root",
password:"root"
}
SparkConfig.scala的读取配置文件内容的代码如下:
package com.iflytek.rwresourcefile
import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigFactory
object SparkConfig extends java.io.Serializable{
val config:Config=ConfigFactory.load()
def getString(path:String):String={
return config.getString(path)
}
def getInteger(path:String):Integer={
return config.getString(path).toInt
}
def getDouble(path:String):Double={
return config.getString(path).toDouble
}
}
Test.scala的测试代码如下:
package com.iflytek.rwresourcefile
import org.apache.spark.SparkConf
object Test {
def main(args: Array[String]): Unit = {
val url=SparkConfig.getString("mysql.url")
val dbtable=SparkConfig.getString("mysql.dbtable")
val user=SparkConfig.getString("mysql.user")
val password=SparkConfig.getString("mysql.password")
println("url="+url)
println("dbtable="+dbtable)
println("user="+user)
println("password="+password)
}
}
def main(args: Array[String]): Unit = {
val url=SparkConfig.getString("mysql.url")
val dbtable=SparkConfig.getString("mysql.dbtable")
val user=SparkConfig.getString("mysql.user")
val password=SparkConfig.getString("mysql.password")
println("url="+url)
println("dbtable="+dbtable)
println("user="+user)
println("password="+password)
}
}
截图:
对于mysql.conf这种自定义的配置文件的读取方式如下:
url=jdbc:mysql://192.168.76.14:3306/ibigdata?useUnicode=true&characterEncoding=UTF-8
dbtable=record_test
user=root
password=root
dbtable=record_test
user=root
password=root
PropertieUtil.scala的读取自定义配置文件的代码如下:
package com.iflytek.rwresourcefile
import java.util.Properties
import java.io.InputStreamReader
import java.io.InputStreamReader
object PropertieUtil extends java.io.Serializable{
def getpropertie(path:String):Properties={
val properties:Properties=new Properties
val in:InputStreamReader=new InputStreamReader(PropertieUtil.getClass.getClassLoader.getResourceAsStream(path),"utf-8")
properties.load(in)
return properties
}
val propertie=getpropertie("mysql.conf")
def getStrign(path:String):String={
return propertie.getProperty(path)
}
}
def getpropertie(path:String):Properties={
val properties:Properties=new Properties
val in:InputStreamReader=new InputStreamReader(PropertieUtil.getClass.getClassLoader.getResourceAsStream(path),"utf-8")
properties.load(in)
return properties
}
val propertie=getpropertie("mysql.conf")
def getStrign(path:String):String={
return propertie.getProperty(path)
}
}
test111.scala的测试代码如下:
package com.iflytek.rwresourcefile
object test111 {
def main(args: Array[String]): Unit = {
val url=PropertieUtil.getStrign("url")
val dbtable=PropertieUtil.getStrign("dbtable")
val user=PropertieUtil.getStrign("user")
val password=PropertieUtil.getStrign("password")
println("url="+url)
println("dbtable="+dbtable)
println("user="+user)
println("password"+password)
}
}
def main(args: Array[String]): Unit = {
val url=PropertieUtil.getStrign("url")
val dbtable=PropertieUtil.getStrign("dbtable")
val user=PropertieUtil.getStrign("user")
val password=PropertieUtil.getStrign("password")
println("url="+url)
println("dbtable="+dbtable)
println("user="+user)
println("password"+password)
}
}
结果截图:
1 0
- scala的maven项目读取配置文件
- scala+Maven工程读取jar包外的配置文件
- Maven 项目 Java读取配置文件
- sbt/maven构建scala项目配置文件
- maven项目 子模块的myabtis配置文件读取失败
- 构建Scala的Maven项目
- Maven项目通过配置文件读取证书问题
- Scala读取*.properties配置文件
- Intellij IDEA15:建立Scala的Maven项目
- maven对scala项目的编译
- maven对scala项目的编译
- IDEA构建scala的maven项目
- Intellij IDEA15:建立Scala的Maven项目
- Maven项目中读取src/main/resources目录下的配置文件
- Idea创建maven的scala项目,没有scala class选项
- 读取Web项目外的配置文件
- web项目 读取src下的配置文件
- Java项目中读取配置文件的值
- Maven依赖机制 工作原理 (分析maven项目和非maven项目的差别)
- 做点面试题,写写总结,提升下基础知识
- mqtt.fx 简介
- RSA加密算法Java应用解析-目前地球上最有影响力的加密算法
- 重写ajax实现session超时跳转到登陆页面
- scala的maven项目读取配置文件
- JDK、JRE和JVM的关系
- 常用技术网站收集
- NVMe之热拔插事件
- Java中Date,Calendar,Timestamp的区别以及相互转换与使用
- 计算机图形学之漫发射和镜面反射分离——基于颜色空间的双色模形法By Klinker
- Android系统中的Application和四大组件一些方法的启动顺序和一些坑
- Angularjs2报错的概览(数据为json格式)
- tester