Groovy   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Groovy Goodness: Using ConfigSlurper with Configuration Scripts Groovy读取配置文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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.hostname
assert 'secret' == config.mail.user
assert 'secret' == config.mail.password
assert '1.0-2009_10_10' == config.app.version

大佬总结

以上是大佬教程为你收集整理的Groovy Goodness: Using ConfigSlurper with Configuration Scripts Groovy读取配置文件全部内容,希望文章能够帮你解决Groovy Goodness: Using ConfigSlurper with Configuration Scripts Groovy读取配置文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。