为什么Grails插件的BootStrap没有执行?

时间:2014-05-15 15:28:18

标签: grails grails-plugin bootstrapping

我创建了一个Grails插件(grails create-plugin myplugin1)并注意到在创建Grails应用时,通常没有创建myapp1/grails-app/conf/BootStrap.groovy

我创建了一个这样的:

class BootStrap {
    def init = {
        println("Hello! The plugin is bootstrapping...")
    }

    def destroy = {
    }
}

然后我将插件包含在Grails应用中(通过在应用的myplugin1中添加BuildConfig.groovy作为插件)。当我发出grails run-app时,我看不到上面的println正在执行。

Grails插件不使用BootStrap.groovy吗?如果是这样,我应该在何处加载插件加载时需要执行的“bootstrap”代码?否则,如果我在上面这样做是正确的,为什么我可能没有看到“ Hello!插件是引导... ”消息打印出来了吗?

3 个答案:

答案 0 :(得分:2)

从插件包中删除了插件的BootStrap。您必须在插件描述符中执行init-phase,在以下一个或几个闭包中:

def doWithSpring = {
  def appName = application.metadata.'app.name'
}

def doWithDynamicMethods = { ctx ->
    // TODO Implement registering dynamic methods to classes (optional)
}

def doWithApplicationContext = { applicationContext ->
    // TODO Implement post initialization spring config (optional)
}

答案 1 :(得分:2)

与往常一样,从写得很好并保持良好的documentation开始。

插件不包含Bootstrap.groovy。以下内容不包含在插件中(取自文档)。

  • 的grails-app / CONF / BootStrap.groovy中
  • grails-app / conf / BuildConfig.groovy(虽然它用于生成依赖项.groovy)
  • 的grails-app / CONF / Config.groovy中
  • grails-app / conf / DataSource.groovy(以及任何其他* DataSource.groovy)
  • 的grails-app / CONF / UrlMappings.groovy
  • 的grails-app / CONF /弹簧/ resources.groovy
  • / web-app / WEB-INF
  • 中的所有内容
  • / web-app / plugins / **
  • 中的所有内容
  • / test / **
  • 内的所有内容
  • / .svn / / CVS /
  • 中的SCM管理文件

为了在启动插件时运行代码,您需要使用doWithSpringdoWithApplicationContext(取决于您需要执行的操作)挂钩插件的运行时配置。

documentation解释了所有这些以及更多内容。一个例子可能是:

// MyFancyPlugin.groovy
  ...
  def doWithApplicationContext = { appCtx ->
      def sessionFactory = appCtx.sessionFactory
      // do something here with session factory
  }
  ...

答案 2 :(得分:0)

需要在启动时运行的代码应该放在插件描述符(doWithApplicationContext)中的MyPlugin1GrailsPlugin.groovy闭包中。

或者,将其称为其他内容(例如MyPluginBootStrap.groovy),因为它只包含在打包插件时排除的特定类BootStrapUrlMappings,但< em>名称以BootStrap结尾的任何类被视为引导伪像。

相关问题