Grails执行每个环境的代码

时间:2012-09-28 12:57:46

标签: grails

是否有办法执行Config.groovy文件的操作,但在代码执行期间... ... 类似的东西:

class AController{
    def method(){
        withEnvironments{
            development{
               println 'This is execute just on development'
            }
            production {
               log.debug 'This is execute just on production'
            }
         }
     }
}

我知道我可以使用if (Environment.current == 'development')达到同样的效果,但是有什么东西与sintax ???

1 个答案:

答案 0 :(得分:16)

找到this blog post,其中使用Environment.executeForCurrentEnvironment显示了一种可能的解决方案:

import grails.util.Environment

class AController {
  def method() { 
    Environment.executeForCurrentEnvironment {
      development {
        println 'This is execute just on development'
      }
      production {
        log.debug 'This is execute just on production'
      }
    }
  }
}