Groovy Grails中测试/开发模式的自定义初始化
我想根据当前使用的开发或测试模式,在引导类中运行特定的登录 我该怎么做?来自官方文件Groovy Grails中测试/开发模式的自定义初始化,groovy,grails,Groovy,Grails,我想根据当前使用的开发或测试模式,在引导类中运行特定的登录 我该怎么做?来自官方文件 import grails.util.Environment class BootStrap { def init = { servletContext -> def currentEnv = Environment.current if (currentEnv == Environment.DEVELOPMENT) { // do
import grails.util.Environment
class BootStrap {
def init = { servletContext ->
def currentEnv = Environment.current
if (currentEnv == Environment.DEVELOPMENT) {
// do custom init for dev here
} else if (currentEnv == Environment.TEST) {
// do custom init for test here
} else if (currentEnv == Environment.PRODUCTION) {
// do custom init for prod here
}
}
def destroy = {
}
}
编程环境检测
在代码中,例如在甘特脚本或引导类中,可以使用GrailsUtil类检测环境:
import grails.util.GrailsUtil
...
switch(GrailsUtil.environment) {
case "development":
configureForDevelopment()
break
case "production":
configureForProduction()
break
发件人:
这是老办法。Grails现在支持BootStrap.groovy中的
environments
块,就像Config.groovy和DataSource.groovy中一样-请参阅本文档第3.2节中的“每环境引导”。
class Bootstrap {
def init = { ServletContext ctx ->
environments {
production {
// prod initialization
}
test {
// test initialization
}
development {
// dev initialization
}
}
}
...
}