
The ConfigSlurper class can be used to parse Groovy scripts with configuration information. This way we can use a real script instead of a propertIEs file to define configuration information in our applications. In a previous post we saw how to use the ConfigSlurper from Java code and in this post we focus on using it in Groovy code.
Using the ConfigSlurper we can parse Groovy scripts into a ConfigObject. The ConfigObject is a subclass of linkedHashMap and contains the configuration information. A configuration script contains information defined using dot notation or closures. Because it is a script we can use all Groovy constructs,or use any other Groovy and Java class we want.
To support different configuration settings per environment (for example development,test and production) we can define a special environments section in our script. When we create a new ConfigSlurper instance and use a environment name in the constructor,the environment section is used to determine values. If we don't specify the environment in the constructor the environments section is skipped.
Okay that is a lot of explaining,let's see some code:
// Configuration script as String,but can also be URL,file.def mail = '''// Dot notation.mail.hostname = 'localhost' // Scoped closure notation.mail { // Using Groovy constructs. ['user','password'].each { this."${it}" = 'secret' }}// Environments section.environments { dev { mail.hostname = 'local' } test { mail.hostname = 'test' } prod { mail.hostname = 'prod' }}'''// Another configuration script.def app = '''app { version = version() // Use method in script.}// define method to build version info.def version() { "1.0-${releasedate.format('yyyy_MM_dd')}"}'''// Read mail configuration script for the prod environment.def mailConfig = new ConfigSlurper('prod').parse(mail)// We can pass information to the configuration with// the setBinding method.def appSlurper = new ConfigSlurper()appSlurper.setBinding([releasedate: new Date(109,9,10)])def appConfig = appSlurper.parse(app)// Both configurations are merged into one.def config = mailConfig.merge(appConfig)assert 'prod' == config.mail.hostnameassert 'secret' == config.mail.userassert 'secret' == config.mail.passwordassert '1.0-2009_10_10' == config.app.version 总结 以上是内存溢出为你收集整理的Groovy Goodness: Using ConfigSlurper with Configuration Scripts Groovy读取配置文件全部内容,希望文章能够帮你解决Groovy Goodness: Using ConfigSlurper with Configuration Scripts Groovy读取配置文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)