在 Grails PluginDescriptor.groovy 文件中注入服务?
Injecting a service in Grails PluginDescriptor.groovy file?
我正在尝试 transform/Convert 我的 Grails 应用程序变成 Grails 插件。这个 post 对我这样做很有帮助:
http://burtbeckwith.com/blog/?p=1973
在复制粘贴文件时,按照上面的 link 我被 Bootstrap.groovy 文件卡住了。虽然我必须将我的 Bootstrap.groovy 的 init() 代码粘贴到 ".doWithApplicationContext" 中,我已经这样做了,但我在服务注入方面遇到了问题。这是我的应用程序的 Bootstrap 文件:
class BootStrap {
//Injecting voice recordign service
def processRecordingVoiceRecognizitonService
//Injecting Service to Connect to AMQ Server to Send Recording
def AMQConnectionManagementService
//Injecting AMQ Publisher to Publish Voice Recognition Results
def messagePublisherService
//Injecting AMQ Consumer to Consume Voice Model Creation Notifications
def messageConsumerService
.
.
.
.
.
.
.
可以看到我正在我的 Bootstap 中注入服务。
我在 PluginDesciptor.groovy 的开头粘贴了那段代码,但是 Intellij Idea 没有显示注入标志,这意味着没有注入服务。
是否真的可以在插件描述符中注入服务?如果不是,那么在服务文件中初始化和建立必要连接的方法是什么?
我找到了一个相关问题,但没看懂。这是 link 以防万一。 inject service into instance of src/groovy class
插件描述符生命周期中 BootStrap 的等效点是 doWithApplicationContext
,它接收对 ApplicationContext
的引用,您可以从中获取所需的任何服务。您不能以正常方式将服务注入描述符,因为描述符在 ApplicationContext
设置之前被实例化(并调用了它的几个关键方法)。
def doWithApplicationContext = { applicationContext ->
applicationContext.messagePublisherService.someMethodName()
或者,插件可以通过将其命名为 MyPluginBootStrap.groovy
来为应用程序提供 bootstrap 工件 - grails-app/conf
中任何 Groovy class 其名称 以 结尾 BootStrap 将被视为 bootstrap 人工制品,只有插件中的普通 BootStrap.groovy
被排除在 "contributed" 到依赖插件的应用程序。
我正在尝试 transform/Convert 我的 Grails 应用程序变成 Grails 插件。这个 post 对我这样做很有帮助: http://burtbeckwith.com/blog/?p=1973
在复制粘贴文件时,按照上面的 link 我被 Bootstrap.groovy 文件卡住了。虽然我必须将我的 Bootstrap.groovy 的 init() 代码粘贴到 ".doWithApplicationContext" 中,我已经这样做了,但我在服务注入方面遇到了问题。这是我的应用程序的 Bootstrap 文件:
class BootStrap {
//Injecting voice recordign service
def processRecordingVoiceRecognizitonService
//Injecting Service to Connect to AMQ Server to Send Recording
def AMQConnectionManagementService
//Injecting AMQ Publisher to Publish Voice Recognition Results
def messagePublisherService
//Injecting AMQ Consumer to Consume Voice Model Creation Notifications
def messageConsumerService
.
.
.
.
.
.
.
可以看到我正在我的 Bootstap 中注入服务。 我在 PluginDesciptor.groovy 的开头粘贴了那段代码,但是 Intellij Idea 没有显示注入标志,这意味着没有注入服务。 是否真的可以在插件描述符中注入服务?如果不是,那么在服务文件中初始化和建立必要连接的方法是什么?
我找到了一个相关问题,但没看懂。这是 link 以防万一。 inject service into instance of src/groovy class
插件描述符生命周期中 BootStrap 的等效点是 doWithApplicationContext
,它接收对 ApplicationContext
的引用,您可以从中获取所需的任何服务。您不能以正常方式将服务注入描述符,因为描述符在 ApplicationContext
设置之前被实例化(并调用了它的几个关键方法)。
def doWithApplicationContext = { applicationContext ->
applicationContext.messagePublisherService.someMethodName()
或者,插件可以通过将其命名为 MyPluginBootStrap.groovy
来为应用程序提供 bootstrap 工件 - grails-app/conf
中任何 Groovy class 其名称 以 结尾 BootStrap 将被视为 bootstrap 人工制品,只有插件中的普通 BootStrap.groovy
被排除在 "contributed" 到依赖插件的应用程序。