代码之家  ›  专栏  ›  技术社区  ›  Kimble

Grails插件的配置

  •  9
  • Kimble  · 技术社区  · 17 年前

    2 回复  |  直到 17 年前
        1
  •  7
  •   Chii    17 年前

    details in the manual

    作为额外的好处,还可以在标准Java属性文件中或grails.config位置中指定的其他配置文件上定义设置。

    // This will be the default value...
    myPlugin.url=http://somewhe.re/test/endpoint
    environments {
      production {
        // ...except when running in production mode
        myPlugin.url=http://somewhe.re/for-real/endpoint
      }
    }
    

    稍后,在您的插件提供的服务中

    import org.codehaus.groovy.grails.commons.ConfigurationHolder
    class MyPluginService {
      def url = ConfigurationHolder.config.myPlugin.url
      // ...
    } 
    
        2
  •  13
  •   codehead    17 年前

    如果它只是一个小的(读:一个项目)配置选项,那么在属性文件中使用它可能会更容易。如果有一些配置选项,其中一些应该是动态的,我建议做Acegi Security插件所做的事情——也许在/garils app/conf/plugin_name_config.roovy中添加一个文件。

    额外的好处是,用户可以执行groovy代码来计算他们的配置选项(比使用属性文件要好得多),并且能够轻松地执行不同的环境。

    http://groovy.codehaus.org/ConfigSlurper ,这就是grails内部用来抓取像config.groovy这样的配置的东西。

    //e.g. in /grails-app/conf/MyWebServicePluginConfig.groovy
    somePluginName {
       production {
          property1 = "some string"
       }
       test {
          property1 = "another"
       }
    }
    
    //in your myWebServicePlugin.groovy file, perhaps in the doWithSpring closure
    GroovyClassLoader classLoader = new GroovyClassLoader(getClass().getClassLoader())
    ConfigObject config
    try {
       config = new ConfigSlurper().parse(classLoader.loadClass('MyWebServicePluginConfig'))
    } catch (Exception e) {/*??handle or what? use default here?*/}
    assert config.test.property1.equals("another") == true
    
    推荐文章