ConfigSlurper

原文转载至:http://groovy.codehaus.org/ConfigSlurper

ConfigSlurper

ConfigSlurper is a utility class within Groovy for writing properties file like scripts for performing configuration. Unlike regular Java properties files ConfigSlurper scripts support native Java types and are structured like a tree.

Below is an example of how you could configure Log4j with a ConfigSlurper script:

log4j.appender.stdout = "org.apache.log4j.ConsoleAppender"
log4j.appender."stdout.layout"="org.apache.log4j.PatternLayout"
log4j.rootLogger="error,stdout"
log4j.logger.org.springframework="info,stdout"
log4j.additivity.org.springframework=false

To load this into a readable config you can do:

def config = new ConfigSlurper().parse(new File('myconfig.groovy').toURL())

assert "info,stdout" == config.log4j.logger.org.springframework
assert false == config.log4j.additivity.org.springframework

As you can see from the example above you can navigate the config using dot notation and the return values are Java types like strings and booleans.

You can also use scoping in config scripts to avoid repeating yourself. So the above config could also be written as:

log4j {
    appender.stdout = "org.apache.log4j.ConsoleAppender"
    appender."stdout.layout"="org.apache.log4j.PatternLayout"
    rootLogger="error,stdout"
    logger {
        org.springframework="info,stdout"
    }
    additivity {
        org.springframework=false
    }
}

Converting to and from Java properties files

You can convert ConfigSlurper configs to and from Java properties files. For example:

java.util.Properties props = // load from somewhere

def config = new ConfigSlurper().parse(props)

props = config.toProperties()

Merging configurations

You can merge config objects so if you have multiple config files and want to create one central config object you can do:

def config1 = new ConfigSlurper().parse(..)
def config2 = new ConfigSlurper().parse(..)

config1 = config1.merge(config2)

Serializing a configuration to disk

You can serialize a config object to disk. Each config object implements the groovy.lang.Writable interface that allows you to write out the config to any java.io.Writer:

def config = new ConfigSlurper().parse(..)

new File("指定或默认路径的文件名").withWriter { writer ->
     config.writeTo(writer)//writer是闭包参数,保持writer即可使用
}

Special "environments" Configuration

The ConfigSlurper class has a special constructor other than the default constructor that takes an "environment" parameter. This special constructor works in concert with a property setting called environments. This allows a default setting to exist in the property file that can be superceded by a setting in the appropriate environments closure. This allows multiple related configurations to be stored in the same file.

Given this groovy property file:

Sample.groovy
sample {
  foo = "default_foo"
  bar = "default_bar"
}

environments {
  development {
    sample {
      foo = "dev_foo"
    }
  }
  test {
    sample {
      bar = "test_bar"
    }
  }
}

Here is the demo code that exercises this configuration:

def config = new ConfigSlurper("development").parse(new File('Sample.groovy').toURL())

assert config.sample.foo == "dev_foo"
assert config.sample.bar == "default_bar"

config = new ConfigSlurper("test").parse(new File('Sample.groovy').toURL())

assert config.sample.foo == "default_foo"
assert config.sample.bar == "test_bar"

Note: the environments closure is not directly parsable. Without using the special environment constructor the closure is ignored.

The value of the environment constructor is also available in the configuration file, allowing you to build the configuration like this:

 switch (environment) {
  case 'development':
    baseUrl = "devServer/"
    break
  case 'test':
    baseUrl = "testServer/"
    break
  default:
    baseUrl = "localhost/"
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值