我创建了一个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吗?如果是这样,我应该在加载插件时将需要执行的“引导”代码放在哪里?否则,如果我正确地做到了以上所述,为什么我看不到打印出来的“你好!插件正在引导...”消息?
请您参考如下方法:
与往常一样,从编写和维护良好的documentation开始。
插件不包含Bootstrap.groovy。以下内容不包含在插件中(摘自文档)。
为了在插件启动时运行代码,您需要使用
doWithSpring或doWithApplicationContext(取决于您需要执行的操作)来挂接插件的运行时配置。所有这些以及更多内容在documentation中进行了说明。一个示例可能是:
// MyFancyPlugin.groovy
...
def doWithApplicationContext = { appCtx ->
def sessionFactory = appCtx.sessionFactory
// do something here with session factory
}
...




