Grails中的测试/开发模式上的自定义初始化

时间:2010-12-15 16:52:11

标签: groovy grails

我想在BootStrap类中运行特定的登录,具体取决于当前使用的开发或测试模式。

我该怎么做?

3 个答案:

答案 0 :(得分:14)

来自the documentation

class Bootstrap {
    def init = { ServletContext ctx ->
        environments {
            production {
                // prod initialization
            }
            test {
                // test initialization
            }
            development {
                // dev initialization
            }
        }
    }
    ...
}

答案 1 :(得分:6)

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 = {
     }
}

答案 2 :(得分:0)

来自官方文档

程序化环境检测

在您的代码中,例如在Gant脚本或引导类中,您可以使用GrailsUtil类检测环境:

import grails.util.GrailsUtil
...
switch(GrailsUtil.environment) {
    case "development":
       configureForDevelopment()
    break
    case "production":
       configureForProduction()
    break