scala项目的全局配置实践

编程入门 行业动态 更新时间:2024-10-28 20:28:51

scala项目的<a href=https://www.elefans.com/category/jswz/34/1765343.html style=全局配置实践"/>

scala项目的全局配置实践

  • 定义一个 object 放一些常量名,相当于各种属性的key值
object MktEnvironmentContants {// 系统环境变量val KEY_UAP_ZK_SERVER = "uap.zk.server"val KEY_UAP_SERVICE_CONSOLE_PORT = "uap.service.console.port"val KEY_UAP_SERVICE_ANALYZE_PORT = "uap.service.analyze.port"val KEY_UAP_SERVICE_PREDICT_PORT = "uap.service.predict.port"}
  • 通过启动命令传递参数并通过 System.getProperty 获取
-Dspring.profiles.active=dev
  • 根据启动命令的类型加载不同的配置文件,并将加载到的内存放入系统配置里面。整个过程中都是java内置的代码
object PropertiesUtil extends RichLogging {/*** 加载properties文件,以classpath为根目录** @param fileName 文件名* @return*/def loadProperties(fileName: String): Properties = {val inputStream: InputStream = Thread.currentThread().getContextClassLoader.getResourceAsStream(fileName)val properties = new Properties()properties.load(inputStream)properties}/*** 加载属性到系统属性** @param fileName 文件名*/def loadProperteisToSystem(fileName: String): Unit = {val envProperties = PropertiesUtil.loadProperties(fileName)val envNames = envProperties.propertyNames()while (envNames.hasMoreElements) {val key = envNames.nextElement.asInstanceOf[String]System.getProperties.setProperty(key, envProperties.getProperty(key))}}/*** 显示系统属性*/def showSystemProperties(): Unit = {val properties = System.getPropertiesval ele = properties.propertyNameswhile (ele.hasMoreElements) {val key = ele.nextElement.asInstanceOf[String]logger.info(s"System Properties, key: $key, value: ${properties.getProperty(key)}")}}
}
  • 使用常量定义的key值去系统中取配置
 System.getProperty(MktEnvironmentContants.KEY_UAP_PROFILE_ACTIVE)

更多推荐

scala项目的全局配置实践

本文发布于:2023-07-03 16:26:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1008924.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:全局   项目   scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!