2012-09-28 42 views
5

Có cách nào để làm những gì các tập tin Config.groovy không, nhưng trong quá trình thực thi mã ...
Cái gì như:Grails thực thi mã mỗi môi trường

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

tôi biết rằng tôi có thể đạt được hiệu quả tương tự sử dụng if (Environment.current == 'development'), nhưng có cái gì đó với sintax ???

+1

chỉnh chỉ nhỏ if (. Environment.getCurrent() equals (Environment.DEVELOPMENT)) –

+1

@AramArabyan Một cách groovier nói đó là : 'if (Environment.current == Environment.DEVELOPMENT)' –

+0

Đối với nhà phát triển lười biếng lười biếng trở thành nếu (Environment.current == 'development') khi mã không phải làm việc :) – rascio

Trả lời

16

Tìm thấy this blog post trong đó cho thấy một giải pháp có thể sử dụng 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' 
     } 
    } 
    } 
} 
+0

Cảm ơn người đàn ông! Đây là những gì tôi đang tìm kiếm! – rascio

Các vấn đề liên quan