配置文件的读取

版权声明:免费免费 请勿收费 欢迎传播 https://blog.csdn.net/m0_37987402/article/details/85631370

在这里插入图片描述
在这里插入图片描述

db.connectStr = jdbc:oracle:thin:@172.20.32.11:1521:tt
db.driverName = oracle.jdbc.OracleDriver
db.usr = 
db.password = ffffff
default.areacode=080000

在这里插入图片描述

package com.haiyisoft.config

import java.io.{InputStream, IOException}
import java.util.Properties
/**
  * Created by jia on 2016/1/13.
  */
object ConfigManager {
  private var properties:Properties = null
  private val service = new ConfigServiceByFileImpl();//此处需要实现ConfigService接口
  try {
    this.properties = service.getConfig()
  } catch {
    case e: IOException =>
      e.printStackTrace()
  }
  def getConfigValue(configName:String):String={
    if(properties!=null){
      properties.getProperty(configName)
    }else{
      null
    }
  }
}
trait ConfigService{
  def getConfig():Properties;
}
class ConfigServiceByFileImpl extends ConfigService{
  val fileName:String = "com/haiyisoft/config/config.properties"
  def getConfig():Properties = {
    var properties:Properties = null
    var inputStream:InputStream = null
    properties = new Properties()
    inputStream = this.getClass.getClassLoader().getResourceAsStream(fileName)
    try {
      properties.load(inputStream)
    } catch {
      case e: IOException =>
        e.printStackTrace()
    } finally {
      (inputStream == null) match {
        case false => inputStream.close()
        case true =>
      }
    }
    properties
  }
}

猜你喜欢

转载自blog.csdn.net/m0_37987402/article/details/85631370