生活随笔
收集整理的這篇文章主要介紹了
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.propertyNames
while (ele.hasMoreElements
) {val key
= ele.nextElement.asInstanceOf
[String
]logger.info
(s
"System Properties, key: $key, value: ${properties.getProperty(key)}")}}
}
System.getProperty
(MktEnvironmentContants.KEY_UAP_PROFILE_ACTIVE
)
總結
以上是生活随笔為你收集整理的scala项目的全局配置实践的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。